200分求解异步服务端套接字异常
本题和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();
}
}
}