我使用套接字异步回调(服务器和客户端),以及一些一次性资源(但同步)。
当我将它们关闭,我处置,这往往会导致我的应用程序崩溃,没有抛出任何异常。我怕,我做错了什么(嗯,真不错...)
我的问题是:
答:在什么情况下可以没有任何通知,我的假设是由于出售,或可能的异步尝试使用处置的对象,但我真的不知道。
一个C#应用程序崩溃
B.是否有一种方法来找出究竟在何处应用程序崩溃?这就像调试代码时,我似乎,一切都按计划进行。
C。能否请您给我提供一个体面的文章或解释如何创造一个良好的插座设计?我目前的设计是接收交替的数据包的大小(4个字节的命令长度(INT32),然后,指定长度的数据包)。
我的数据接收方法:
private void OnDataReceived(IAsyncResult asyn)
{
// Get the packet from the Async Result
SocketPacket packet = (SocketPacket)asyn.AsyncState;
try
{
// Complete the receive.
ConnectionSocket client = GetConnection(packet.ClientID);
// How many bytes were received.
int length = client.Socket.EndReceive(asyn);
// Received 0 bytes -> Connection has been disconnected.
if (length == 0)
{
client.Dispose();
m_clients[packet.ClientID - 1] = null;
return;
}
// Add the extra data that was received.
packet.AlreadyRead += length;
// If Message is complete, handle it.
if (packet.PacketDone)
{
// Was expecting the length of the next command
if (client.ExpectToReceive == ExpectToReceive.MessageLength)
{
// Expect the next packet to be of the specified length.
int cmdLength = BitConverter.ToInt32(packet.Data, 0);
// Advance - expect next packet to be data.
packet.NextMessage(cmdLength);
client.ExpectData();
}
else // Was expecting a command.
{
// Handle the data
// Next packet would be message length, and the length would be 4 bytes.
packet.NextMessage(4);
client.ExpectLength();
}
}
// Wait for the next packet.
WaitForData(packet.ClientID, packet);
}
catch (ObjectDisposedException)
{
//MessageBox.Show("OnDataReceived: Socket has been closed\n");
}
catch (SocketException se)
{
if (se.ErrorCode == 10054) // Error code for Connection reset by peer
{
m_clients[packet.ClientID - 1] = null;
}
else
MessageBox.Show(se.Message);
}
}
在我WaitForData的方法:
{C}
我敢肯定,这个问题是某处在这里...你可以指出可能的原因吗?