0

我正在使用来自 MSND 的服务器示例:http: //msdn.microsoft.com/en-us/library/fx6588te.aspx

并且由于某种原因,我的服务器拒绝以_listener.Bind(localEndPoint);错误的方式连接此功能Only one usage of each socket address (protocol/network address/port) is normally permitted

直到几分钟前我还没有遇到任何问题 Sarver 连接没有问题,突然它发生了

public static void StartListening(IPAddress ipAddress, int port)
{
    _isServerRunning = true;
    // 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 = IPAddress.Parse("192.168.0.100"); //ipHostInfo.AddressList[0];
    IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port);

    // Create a TCP/IP 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());
    }

    //Console.WriteLine("\nPress ENTER to continue...");
    Console.Read();

}

这个错误是什么意思?

4

2 回答 2

0

我想你可以在这里找到一些解决方案http://blogs.msdn.com/b/dgorti/archive/2005/09/18/470766.aspx

于 2013-09-15T12:48:08.503 回答
0

这很可能意味着本地端口仍然保留。这可能是因为另一个进程仍在运行并保持 sockewt 打开,或者因为 dSO_LINGER 套接字选项定义的超时期限未超时。我对 c# 不熟悉,但应该有一种方法来设置 elinger 超时。

您可以使用 netstat 命令来判断套接字的当前状态。

于 2013-09-15T12:41:14.023 回答