I'm trying to create a small udp server开发者_如何学Go and client. I'm coding the log off functionality now, but for some reason I get a ObjectDisposedException.
Data msgToSend = new Data ();
msgToSend.cmdCommand = Command.Logout;
msgToSend.strName = strName;
msgToSend.strMessage = null;
byte[] b = msgToSend.ToByte ();
clientSocket.SendTo(b, 0, b.Length, SocketFlags.None, epServer);
clientSocket.Close();
The server receives the message. And then does what it is supposed to, but when I reach the clientSocket.Close() I get the exception.
the error must be somewhere else because it's ok to call Close
after SendTo
, here a snippet from MSDN: http://msdn.microsoft.com/en-us/library/beez6ewa.aspx
public static void SendTo4()
{
IPHostEntry hostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], 11000);
Socket s = new Socket(endPoint.Address.AddressFamily,
SocketType.Dgram,
ProtocolType.Udp);
byte[] msg = Encoding.ASCII.GetBytes("This is a test");
Console.WriteLine("Sending data.");
// This call blocks.
s.SendTo(msg, 0, msg.Length, SocketFlags.None, endPoint);
s.Close();
}
精彩评论