开发者

Does socket.sendTo close the socket? [duplicate]

开发者 https://www.devze.com 2023-04-01 00:10 出处:网络
This question already has an answer here: ObjectDisposeException when trying to send through a reopened socket
This question already has an answer here: ObjectDisposeException when trying to send through a reopened socket (1 answer) Closed 2 years ago.

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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消