关闭前发送套接字

时间:2015-07-21 12:34:44

标签: c# sockets

我有这段代码:

public static List<Socket> samp = new List<Socket>();

表单加载中的套接字连接/等。

IPHostEntry host = null;
Socket sock;
host = Dns.GetHostEntry(sending ip..);

foreach (IPAddress address in host.AddressList)
{
    IPEndPoint ipe = new IPEndPoint(address, 7777);
    sock = new Socket(ipe.AddressFamily, SocketType.Stream,  ProtocolType.Tcp);
    sock.Connect(ipe);
    if (sock.Connected)
    {
        sock.SendTo(Encoding.UTF8.GetBytes("App starts"), ipe);
        samp.Add(sock);
    }
}

表单关闭:

if (samp.Count > 0)
{
    foreach (Socket socket in samp)
    {
        socket.Send(Encoding.UTF8.GetBytes("App closing"));
        socket.Close();
    }
}

我收到错误:

  

无法访问已处置的对象。对象名称:&#39; System.Net.Sockets.Socket&#39;

提前致谢!

P.S 看起来像socket.Close()有效,但socket.Send nope :(

0 个答案:

没有答案