HostIP = IPAddress.Parse("127.0.0.1");
try
{
ChatServer = new IPEndPoint(HostIP, Int32.Parse("8089"));
ChatSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ChatSocket.Bind(ChatServer);
ChatSocket.Listen(50);
this.label3.Text = "Listening";
AcceptedSocket = ChatSocket.Accept();
Thread thread = new Thread(new ThreadStart(ChatProcess));
thread.Start();
MessageBox.Show("Success!");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
try
{
ChatServer = new IPEndPoint(HostIP, Int32.Parse("8089"));
ChatSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
ChatSocket.Bind(ChatServer);
ChatSocket.Listen(50);
this.label3.Text = "Listening";
AcceptedSocket = ChatSocket.Accept();
Thread thread = new Thread(new ThreadStart(ChatProcess));
thread.Start();
MessageBox.Show("Success!");
}
catch (Exception ee)
{
MessageBox.Show(ee.ToString());
}
解决方案 »
- 这两种绑定数据库的方式有什么不同?在线等。。。
- 求,C#网页下,如何根据客户端显示器分辨率的不同,设置层DIV1的宽度
- c#插入字符?
- 窗体编程经验丰富者请进:窗体刷新速度的问题!
- 文件中如何读取图片?
- SQLCE SQL语句
- 请问大家,在C#中怎么才能把dataGrid清空呢?谢谢了(急)
- 【求助】winform下怎么导入加密的excel文件?
- [小白求教]c# System.Data.OracleClient 如何批量插入数据
- 急,怎样将panel控件(包含其子控件)的图像 保存为Bitmap或Image对象,在线等待
- 偶是菜鸟,问个非常sb的问题
- 求助关于C#程序获取udp端口数据的问题
当我点击按钮,结果就死了
然后我debug一下:发现错误在这一行:
AcceptedSocket = ChatSocket.Accept();这行有错么?
我是这样定义的:
private Socket AcceptedSocket;另外还有一个方法: private void ChatProcess()
{
if (AcceptedSocket.Connected)
{
this.label3.Text = "Ready to chatting!";
while (flag)
{
Byte[] ReceivedByte = new Byte[64];
AcceptedSocket.Receive(ReceivedByte, ReceivedByte.Length, 0);
string ReceiveStr = System.Text.Encoding.BigEndianUnicode.GetString(ReceivedByte);
this.textBox1.AppendText(ReceiveStr+"\r\n");
//Byte[] SentByte = new byte[64];
//string SentStr = ReceiveStr;
//SentByte = System.Text.Encoding.BigEndianUnicode.GetBytes(SentStr.ToCharArray());
//AcceptedSocket.Send(SentByte, SentByte.Length, 0);
}
}
else
MessageBox.Show("The connection has not existed!");
}
这个时候你用客户端去连接因该还能够正确连接的
因为这里你没加入线程处理 Accept把主线程全占了界面就不重绘了