none
WSANOTINITIALISED (10093) when calling BeginAcceptTcpClient() RRS feed

  • Question

  • Hi,

    I have an .NET 3.5 application, which have a server socket. The connecting clients are handled using functions BeginAcceptTcpClient and EndAcceptTcpClient. Old version of the application have worked fine and served TCP clients without problems. But, now when the protocol an other socket connection that the application uses have changed, the server socket serves a few clients without error and then BeginAcceptTcpClient rises exception WSANOTINITIALISED (10093). The code of the server socket have not changed, and the changes in the the other socket connection is that it started using OpenSSL (new DLLs ssleay32.dll and libeay32.dll). The implementation of the protocol and the changes was made by an other company, and I don't have the source codes.

    Detailed description of the exception is:
    Successful WSAStartup not yet performed.
    Either the application has not called WSAStartup or WSAStartup failed. The application may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), or WSACleanup has been called too many times.

    But, our application code doesn't have any WSAStartup or WSACleanup calls.

    Stack Trace when the exception occurs:
    System.Net.Sockets.SocketException: Either the application has not called WSAStartup, or WSAStartup failed
       at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
       at System.Net.Sockets.Socket.DoBeginAccept(Socket acceptSocket, Int32 receiveSize, AcceptOverlappedAsyncResult asyncResult)
       at System.Net.Sockets.Socket.BeginAccept(Socket acceptSocket, Int32 receiveSize, AsyncCallback callback, Object state)
       at System.Net.Sockets.Socket.BeginAccept(AsyncCallback callback, Object state)
       at System.Net.Sockets.TcpListener.BeginAcceptTcpClient(AsyncCallback callback, Object state)

    So, what could corrupt the sever socket that way? Has anybody seen this kind server socket malfunction before? Is there some good way to trace WSACleanup calls, if those cause the problem?

    Monday, May 16, 2016 5:12 PM

Answers

  • So the additional "TCP Listener testing" does not reveal anything more useful than "the winsock is in hopelessly broken state that even new-ing a socket won't fix".

    I guess you could only seek help from the author of those DLLs.

    Monday, May 23, 2016 2:07 AM
    Answerer

All replies

  • My guess is their library didn't call WSAStartup() or somehow it fails. It should be the first function to be called by any application/DLL that needs to use socket.

    Is their library new? I think if that's the case, such error is pretty verbose and the people who creates the library is unlikely to miss it.

    If you use System.Net.Sockets classes, the WSAStart() and WSACleanup are called by the .NET runtime on behalf of your application so you shouldn't need to worry. The fact that this function needs to be called on each module that calls WSA*() directly means that they don't interfere with each others, so the error is very unlikely to be caused by your code. You will want to ask them to fix it.

    Tuesday, May 17, 2016 2:06 AM
    Answerer
  • Thanks for your comments. The library changes on the other (socket) interface are about half year old. The interface where libraries changed works fine, but this server/listener, which was not changed, can now serve only 1 to 5 clients before the exception. So, it's strange, what can cause that System.Net.Sockets works well only short time in our module. Could .NET has some bug handilng win socket DLLs?
    Tuesday, May 17, 2016 8:04 AM
  • I suggest that you may temporary try to new a TcpListener to listen a port on loopback interface (127.0.0.1) on the exception handler of that section of code to see if a second exception is thrown (i.e.: you'll want to wrap that temporary code in it's own try...catch... block with exception logging)

    If it can successfully listen to the port, it's not a problem on .NET framework. If not, post the error you see here and see if we can further deduce the reason why it fails.

    Wednesday, May 18, 2016 1:15 AM
    Answerer
  • Below, you can see that part of original code and the added "Temp test code"

      try
      {
          listener.BeginAcceptTcpClient(OnAcceptConnection, listener);
          //complete the last operation...
          client = listener.EndAcceptTcpClient(result);
          MessageSocketServer server = new MessageSocketServer(client.GetStream(), myEncoding, client.Client.RemoteEndPoint.ToString());
          myNotifier.ConnectionEstablished(server);
      }
      catch (Exception ex)
      {
          NSLogger.Logger.NoteLogEntry(myProgram, myModule, myLoggingGuid, "A handled exception occurred in OnAcceptConnection", ex);
          // Temp test code begin
          NSLogger.Logger.NoteLogEntry("SocketTest", "Test listener start.", NSLogger.EventSeverity.DEBUG);
          try
          {
              var testListener = new TcpListener(IPAddress.Parse("127.0.0.1"), 17000);
          }
          catch (Exception testEx)
          {
              NSLogger.Logger.NoteLogEntry(myProgram, myModule, "SocketTest", "Test listener exception.", testEx);
          }
          // Temp test code end
      }

               

    The log lines containing stack traces:

    749;2016.05.18 15.32.31;SocketReceiver;EXCEPTION;A handled exception occurred in OnAcceptConnection; OnAcceptConnection:line 80
    System.Net.LazyAsyncResult.Complete:line 0
    System.Net.ContextAwareResult.CompleteCallback:line 0
    System.Threading.ExecutionContext.Run:line 0
    System.Net.ContextAwareResult.Complete:line 0
    System.Net.LazyAsyncResult.ProtectedInvokeCallback:line 0
    System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback:line 0
    System.Threading._IOCompletionCallback.PerformIOCompletionCallback:line 0
    ;System.Net.Sockets.SocketException: Either the application has not called WSAStartup, or WSAStartup failed
       at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
       at System.Net.Sockets.Socket.DoBeginAccept(Socket acceptSocket, Int32 receiveSize, AcceptOverlappedAsyncResult asyncResult)
       at System.Net.Sockets.Socket.BeginAccept(Socket acceptSocket, Int32 receiveSize, AsyncCallback callback, Object state)
       at System.Net.Sockets.Socket.BeginAccept(AsyncCallback callback, Object state)
       at System.Net.Sockets.TcpListener.BeginAcceptTcpClient(AsyncCallback callback, Object state)
       at OnAcceptConnection(IAsyncResult result) in MainSocketServer.cs:line 67;
    750;2016.05.18 15.32.31;SocketTest;DEBUG;Test listener start.;OnAcceptConnection:line 82;;
    751;2016.05.18 15.32.31;SocketTest;EXCEPTION;Test listener exception.; OnAcceptConnection:line 91
    System.Net.LazyAsyncResult.Complete:line 0
    System.Net.ContextAwareResult.CompleteCallback:line 0
    System.Threading.ExecutionContext.Run:line 0
    System.Net.ContextAwareResult.Complete:line 0
    System.Net.LazyAsyncResult.ProtectedInvokeCallback:line 0
    System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback:line 0
    System.Threading._IOCompletionCallback.PerformIOCompletionCallback:line 0
    ;System.Net.Sockets.SocketException: Either the application has not called WSAStartup, or WSAStartup failed
       at System.Net.Sockets.Socket..ctor(AddressFamily addressFamily, SocketType socketType, ProtocolType protocolType)
       at System.Net.Sockets.TcpListener..ctor(IPAddress localaddr, Int32 port)
       at OnAcceptConnection(IAsyncResult result) in MainSocketServer.cs:line 85;

    So, the added "new TcpListener" cause the same socket exception.

    P.S. Sorry I did not find way to make the code sample more readable in this forum.


    • Edited by Mikapamika Wednesday, May 18, 2016 1:44 PM
    Wednesday, May 18, 2016 1:21 PM
  • So the additional "TCP Listener testing" does not reveal anything more useful than "the winsock is in hopelessly broken state that even new-ing a socket won't fix".

    I guess you could only seek help from the author of those DLLs.

    Monday, May 23, 2016 2:07 AM
    Answerer