locked
TcpListener getting 10042 error code

    Question

  • When creating a TcpListener object I am getting error code 10042. Below is the code I am using.

    IPHostEntry localMachineInfo = Dns.GetHostEntry(Dns.GetHostName());

    IPEndPoint localEP = new IPEndPoint(localMachineInfo.AddressList[0], System.Convert.ToInt32(tbPort.Text));

    TcpListener listener = new TcpListener(localEP);

    listener.Start();

    When debugging this code I notice that right after listener object is created I see error code 10042 (An unknown, invalid, or unsupported option or level was specified in a getsockopt or setsockopt call).

    am I missing a configuration setup ?

    Thursday, February 8, 2007 5:52 PM

Answers

  • You are welcome ,

    If you feel my replies helped you in your problem. Please mark as Answered.

    Best Regards,

    Rizwan aka RizwanSharp

    Friday, February 9, 2007 4:16 PM

All replies

  • The Socket Error Code 10042 says : Bad Protocol Option.
    In my point of view using TcpListener should not raise such type of exception because unlike the direct use of Socket you cannot specify wrong arguments in Constructor but TcpListener is well formed socket with right protocol selection.
     
    Your code also seem totally fine.
    I would only suggest you to see what IP is in AddressList[0], Is it a valid IP. Besides this you can also try:

    TcpListener listener = new Listener(IPAddress.Any, port);

    listner.Start();

    Does this make a difference?

    Best Regards,

    Rizwan aka RizwanSharp

    Thursday, February 8, 2007 6:27 PM
  • It did not make a difference.

    Actually I've just noticed that before creating the TcpListener object, I am getting error code 10045 (operation not supported) when executing the following line of code.

    IPHostEntry localMachineInfo = Dns.GetHostEntry(Dns.GetHostName()); <==== Error Code = 10045

    IPEndPoint localEP = new IPEndPoint(localMachineInfo.AddressList[0], System.Convert.ToInt32(tbPort.Text));

    I can see that "localMachineInfo.AddressList[0]" has a valid Ip address, but also see the exception.

    Now by using the following line of code without the IPHostEntry and IPEndPoint, I still got the 10042.

    TcpListener listener = new TcpListener(IPAddress.Parse("10.227.30.152"), 5000);

    One additional question: After the listener.Start() when I issue the NETSTAT command in DOS, I should see a entry showing the listening port, right ?

     

    Thanks a lot for your comments !!!

     

     

     

     

    Thursday, February 8, 2007 7:08 PM
  • I added the following lines of code:

    while (true)

    {

    Socket s = listener.AcceptSocket();

    s.Listen(1);

    DisplayMessage("\r\nConnection received .... " + DateTime.Now.ToLongTimeString());

    }

    And I see the client connected and showing it as active connection in the NETSTAT screen.

    I guess the listener.start() does not show an active connection until a client connection request comes in and it is accepted.

    Also I should not be worried about those error codes until I do not received a client connection request.

    Thursday, February 8, 2007 7:23 PM
  • See the Web what 10045 means, ye after Listen method you should see Listeinging socket using netstat -a.

    What Windows u are using?

    Best Regards,

    Rizwan aka RizwanSharp

    Thursday, February 8, 2007 7:25 PM
  • You are right the TcpListener.Start() shows an active listening entry when doing NETSTAT -A

    I am using Windows XP SP2 and Visual Studio 2005 Team Suite.

    It is working now with the code below.

    try

    {

    IPHostEntry localMachineInfo = Dns.GetHostEntry(Dns.GetHostName());

    IPEndPoint localEP = new IPEndPoint(localMachineInfo.AddressList[0], System.Convert.ToInt32(tbPort.Text));

    listener = new TcpListener(localEP);

    listener.Start();

    while (true)

    {

    socket = listener.AcceptSocket();

    DisplayMessage("\r\nConnection received .... " + DateTime.Now.ToLongTimeString());

    ProcessLogon();

    }

    }

    catch (Exception e)

    {

    DisplayMessage("\r\nException raised, message: " + e.Message);

    }

    Thanks a lot for your input !!! 

     

     

     

    Thursday, February 8, 2007 8:05 PM
  • You are welcome ,

    If you feel my replies helped you in your problem. Please mark as Answered.

    Best Regards,

    Rizwan aka RizwanSharp

    Friday, February 9, 2007 4:16 PM