200分求解异步服务端套接字异常

Sniper 2003-10-20 05:48:36
本题和http://expert.csdn.net/Expert/topic/2375/2375377.xml?temp=.7517816为同一问题,合并给200分。

本段代码实现一个Windows Form,并用另外一个线程启动一个异步服务端套接字接收信息。现在问题是客户端发起一个连接,并发送消息后,本程序能正确接收到消息,但随后Form就没有响应了。调试时发现异步服务端套接字仍能正常接收信息,并送到Form进行处理,但Form的界面不再进行更新,也没有任何响应。
请问是什么问题?

服务器相关代码如下:
Form1.cs中
private void Form1_Load(object sender, System.EventArgs e)
{
alarmServer = new System.Threading.Thread(new System.Threading.ThreadStart(AlarmServer.StartListening));
alarmServer.Start();

AlarmServer.Alarm += new AlarmEventHandler(AlarmServer_Alarm);
}

AlarmServer.cs中
public class AlarmServer
{
public static event AlarmEventHandler Alarm;
public const int AlarmPackageLength = 63;
public const int DataIDPos = 6;

// Thread signal.
public static ManualResetEvent allDone = new ManualResetEvent(false);

public static void StartListening()
{
// Data buffer for incoming data.
byte[] bytes = new Byte[1024];

// Establish the local endpoint for the socket.
// The DNS name of the computer
// running the listener is "host.contoso.com".
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 22114);

// Create a TCP/IP socket.
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp );

// Bind the socket to the local endpoint and listen for incoming connections.
try
{
listener.Bind(localEndPoint);
listener.Listen(100);

while (true)
{
// Set the event to nonsignaled state.
allDone.Reset();

// Start an asynchronous socket to listen for connections.
Console.WriteLine("Waiting for a connection...");
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener );

// Wait until a connection is made before continuing.
allDone.WaitOne();
}

}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}

public static void AcceptCallback(IAsyncResult ar)
{
// Signal the main thread to continue.
allDone.Set();

// Get the socket that handles the client request.
Socket listener = (Socket) ar.AsyncState;
Socket handler = listener.EndAccept(ar);

// Create the state object.
StateObject state = new StateObject();
state.workSocket = handler;
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}

public static void ReadCallback(IAsyncResult ar)
{
String content = String.Empty;

// Retrieve the state object and the handler socket
// from the asynchronous state object.
StateObject state = (StateObject) ar.AsyncState;
Socket handler = state.workSocket;

// Read data from the client socket.
int bytesRead = handler.EndReceive(ar);

if (bytesRead > 0)
{
// There might be more data, so store the data received so far.
Buffer.BlockCopy(state.buffer, 0, state.receivedData, state.receivedDataLength, bytesRead);
state.receivedDataLength += bytesRead;

// 测试是否有完整报文
byte[] package = CheckForPackage(ref state);
while(null != package)
{// 有完整报文,检出 [9/9/2003]
Alarm(null, new AlarmEventArgs(package));

// 继续检出 [10/20/2003]
package = CheckForPackage(ref state);
}
// 继续接收 [9/9/2003]
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
new AsyncCallback(ReadCallback), state);
}
else
{// 客户端已关闭 [10/20/2003]
// handler.Close();
}
}
}
...全文
37 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sniper 2003-10-24
  • 打赏
  • 举报
回复
搞定,非常感谢gujunyan(ivy阿亮)!
并请至http://expert.csdn.net/Expert/topic/2375/2375377.xml?temp=.4201013领分
Sniper 2003-10-21
  • 打赏
  • 举报
回复
UP
michaelowenii 2003-10-21
  • 打赏
  • 举报
回复
我想,我只能对你说声:up -_-
yangbc 2003-10-20
  • 打赏
  • 举报
回复
up
顾君彦 2003-10-20
  • 打赏
  • 举报
回复
给你一个程序,学习学习。
http://asp.6to23.com/connect/socketagent.zip
AhBian 2003-10-20
  • 打赏
  • 举报
回复
AlarmServer_Alarm 的源码呢?

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧