private IPAddress HostIP = IPAddress.Parse("127.0.0.1");
private IPEndPoint ChatServer;
private Socket ChatSocket;
private bool flag = true;
private Socket AcceptedSocket;
public Form1()
{
InitializeComponent();
}
private void LoginBtn_Click(object sender, EventArgs e)
{
HostIP = IPAddress.Parse("127.0.0.1");
try
{
ChatServer = new IPEndPoint(HostIP,Int32.Parse("11000"));
ChatSocket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
ChatSocket.Bind(ChatServer);
ChatSocket.Listen(50);
label4.Text =textBox1.Text+ "已经登录,正在聊天中……";
AcceptedSocket = ChatSocket.Accept();
Thread thread = new Thread(new ThreadStart(ChatProcess));
thread.Start();
}
catch(Exception ee)
{
label4.Text = ee.Message;
}
}
private void ChatProcess()
{
if (AcceptedSocket.Connected)
{
label4.Text = "准备聊天……";
while (flag)
{
Byte[] ReceivedByte = new Byte[64];
AcceptedSocket.Receive(ReceivedByte, ReceivedByte.Length, 0);
string ReceiveStr = System.Text.Encoding.BigEndianUnicode.GetString(ReceivedByte);
textBox2.AppendText(ReceiveStr + "\r\n");
}
}
}
用断点尝试 ,在 AcceptedSocket = ChatSocket.Accept();处(大约在20几行)出的问题,执行到那里就不动了,就像是进了死循环,有谁明白,希望解答!谢谢!
|