Kermit
Kermit

Reputation: 34062

invalid argument passed to socket listener

I've been receiving this error continuously and I believe I narrowed down my issue to being that I'm using a 32-bit listener on a 64-bit machine. Is there any way to get this to run on a 64-bit system?

18      Socket sListener;
...
34          permission = new SocketPermission(NetworkAccess.Accept, TransportType.Tcp, "", SocketPermission.AllPorts);

36          //Listening Socket object
37          sListener = null;

39          //Ensure the code has permission to access the Socket
40          permission.Demand();

42          IPHostEntry ipHost = Dns.GetHostEntry("");
43          IPAddress ipAddress = ipHost.AddressList[2];
44          ipEndPoint = new IPEndPoint(ipAddress, 4510);

46          sListener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
...
71      sListener.Listen(10);
72
73      //Begins an asynchronous operation to accept an attempt
74      AsyncCallback aCallback = new AsyncCallback(AcceptCallback);
75      sListener.BeginAccept(aCallback, sListener);

I tried to follow the code in this relevant question, but it gave me the error:

Operator '==' cannot be applied to operands of type 'System.Net.Sockets.AddressFamily' and 'string'

Upvotes: 2

Views: 1770

Answers (3)

mtsiakiris
mtsiakiris

Reputation: 190

Maybe the following sample will help you too.

Socket clientSocket;
System.Net.IPEndPoint clientEndPoint;
System.Net.Sockets.NetworkStream networkStream;
IAsyncResult beginAcceptAsyncResult;
System.Threading.WaitHandle []waitHandleArray;
int waitHandleSignalled;

System.Net.IPEndPoint listenEndpoint = new System.Net.IPEndPoint("127.0.0.1", 45000); //make sure port is free (check with cmd: netstat -an)
System.Net.Sockets.TcpListener listener = new System.Net.Sockets.TcpListener(listenEndpoint);

waitHandleArray = new System.Threading.WaitHandle[1];

listener.Start();

do
{
    try
    beginAcceptAsyncResult = listener.BeginAcceptSocket(null, null);
    waitHandleArray[0] = beginAcceptAsyncResult.AsyncWaitHandle;
    waitHandleSignalled = System.Threading.WaitHandle.WaitAny(waitHandleArray);

    if (waitHandleSignalled != 0)
    {
        clientSocket = _TCPListener.EndAcceptSocket(BeginAcceptAsyncResult);

        clientEndPoint = clientSocket.RemoteEndPoint as System.Net.IPEndPoint;

        networkStream = new System.Net.Sockets.NetworkStream(clientSocket, true);

        //do more with NetworkStream...
    }
}
//...
//...

Upvotes: 0

Parimal Raj
Parimal Raj

Reputation: 20595

I think you are missing the Socket Binding, before a Socket can Listen at a port.

As per MSDN :

Use the Bind method if you need to use a specific local endpoint. You must call Bind before you can call the Listen method. You do not need to call Bind before using the Connect method unless you need to use a specific local endpoint. You can use the Bind method on both connectionless and connection-oriented protocols.

Before calling Bind, you must first create the local IPEndPoint from which you intend to communicate data. If you do not care which local address is assigned, you can create an IPEndPoint using IPAddress.Any as the address parameter, and the underlying service provider will assign the most appropriate network address. This might help simplify your application if you have multiple network interfaces. If you do not care which local port is used, you can create an IPEndPoint using 0 for the port number. In this case, the service provider will assign an available port number between 1024 and 5000.

70:    sListen.Bind(your IP end point)
71:    sListener.Listen(10);

P.S. : Always use a value greater then 4000 for listening port!

Upvotes: 3

Jim Mischel
Jim Mischel

Reputation: 134125

Have you checked the MaxConnections property as discussed in the documentation?

That documentation also describes how to check the ErrorCode for more detailed information. Perhaps you should explore that option.

Upvotes: 0

Related Questions