using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace test4_2
{
public partial class Form1 : Form
{
Socket connectSocket;
//Socket client;
byte[] bytes = new byte[1024];
delegate void listboxDel(string s);
listboxDel listboxdel;
public Form1()
{
InitializeComponent();
textBoxContent.Focus();
listboxdel = new listboxDel(listbox);
//为连接指派线程
Thread threadConnect = new Thread(new ThreadStart(Connect));
threadConnect.Start();
}
public void listbox(string str)
{
listBox1.Items.Add(str);
listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.ClearSelected();
}
//连接方法
public void Connect()
{
try
{
//建立连接socket
connectSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
//开始异步连接
connectSocket.BeginConnect(IPAddress.Parse("172.16.94.152"),
82,
new AsyncCallback(ConnectCallback), //定义回调函数代理
connectSocket); //传递给回调函数的状态
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}