日期:2014-05-17 浏览次数:20480 次
private void button1_Click(object sender, EventArgs e)
{
IPAddress ip = Dns.GetHostEntry(Dns.GetHostName()).AddressList[0];
//步骤1 创建网络端点IPEndPoint
IPEndPoint myServer = new IPEndPoint(ip, 888);
//步骤2 创建套接字Socket
s1 = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//步骤3 套接字绑定到网络端点
s1.Bind(myServer);
label1.Text = ip+": 888 \n等待客户端连接......";
th = new Thread(new ThreadStart(AA));
th.Start();
}
void AA()
{
//步骤4 监听
s1.Listen(5);
//步骤5 接受客户端连接
s2 = s1.Accept();
//步骤6 判断连接状态
if (s2.Connected)
{
label1.Text = "已有客户端连接!";
//步骤7 循环接收客户端消息
while (true)
{
byte[] bb = new byte[1024];
int i = s2.Receive(bb);
string removeMsg = Encoding.Unicode.GetString(bb, 0, i);
label1.Text = removeMsg;
&