刚开始接触socket编程,想做一个简单的服务器与客户端聊天窗口,在做服务器窗口时,窗口启动中添加如下代码后不能启动了,请大虾们帮帮忙,谈谈原因。
private void Form1_Load(object sender, EventArgs e)
{
IPEndPoint MyIpEndPoint = new IPEndPoint(IPAddress.Any,6001);
Socket MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
MySocket.Bind(MyIpEndPoint);
MySocket.Listen(10);
textBox1.Text = "等待客户端的信息中.....";
Socket client = MySocket.Accept();
IPEndPoint ClientEndPoint = (IPEndPoint)client.RemoteEndPoint;
int receive;
byte[] data=new byte[1024];
while (true)
{
receive = client.Receive(data);
if (receive == 0)
{
break;
}
//接收到的字符
string ReceiveString = Encoding.UTF8.GetString(data, 0, receive);
textBox1.Text ="\n"+ ReceiveString;
//发送信息到客户端
client.Send(data, receive, SocketFlags.None);
}
client.Close();
MySocket.Close();
}在Socket client = MySocket.Accept();这句时便不能动了,不知为什么?
private void Form1_Load(object sender, EventArgs e)
{
IPEndPoint MyIpEndPoint = new IPEndPoint(IPAddress.Any,6001);
Socket MySocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
MySocket.Bind(MyIpEndPoint);
MySocket.Listen(10);
textBox1.Text = "等待客户端的信息中.....";
Socket client = MySocket.Accept();
IPEndPoint ClientEndPoint = (IPEndPoint)client.RemoteEndPoint;
int receive;
byte[] data=new byte[1024];
while (true)
{
receive = client.Receive(data);
if (receive == 0)
{
break;
}
//接收到的字符
string ReceiveString = Encoding.UTF8.GetString(data, 0, receive);
textBox1.Text ="\n"+ ReceiveString;
//发送信息到客户端
client.Send(data, receive, SocketFlags.None);
}
client.Close();
MySocket.Close();
}在Socket client = MySocket.Accept();这句时便不能动了,不知为什么?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货