socket error 10093 when trying to reconnect client

Posted on 2005-04-22
Last Modified: 2013-11-20
I have a socket connection going when my application starts up. Then the user can click on a button to change the port number that the application is connected to. When the button is clicked, I close the socket (CAsynSocket btw), and tried the following in trying to reconnect it, but keep getting error 10093 each time:

Ex: I have a socket:

CAsyncSocket *mySocket;

1) call connect() again  // got 10093 error

2) call create()  
   then call connect()  // same 10093 error

3) mySocket = new CAsyncSocket();
   mySocket->connect()  // same 10093 error

My question: what happens after I close a socket? Do I need to do a complete clean up with WSACleanup() then call WSAStartup again? Does error 10093 mean WSAStartup is not being called?
Question by:jade03
    LVL 86

    Expert Comment

    >>Does error 10093 mean WSAStartup is not being called?

    The error is


    which indeed means that you forgot to

    AfxSocketInit ();


    WORD wVersionRequested;
    WSADATA wsaData;
    int err;
    wVersionRequested = MAKEWORD( 2, 2 );
    err = WSAStartup( wVersionRequested, &wsaData );
    if ( err != 0 ) {
        /* Tell the user that we could not find a usable */
        /* WinSock DLL.                                  */

    Author Comment

    yes, I realized that was probably the error...but my question is, I had called that once already on the first connection, should I call it AGAIN after I close the socket in order to reconnect?
    LVL 86

    Expert Comment

    >>should I call it AGAIN after I close the socket in order to reconnect?

    You don't need to call 'AfxSocketInit ()'/'WSAStartup()' unless you called 'WSACleanup()'

    Author Comment

    right...that's the thing...I didn't call WSACleanup() after I call close(), but still it won't let me do another connect() I'm trying to figure out why...
    LVL 6

    Accepted Solution

    I am not sure, but it seems that when you close the socket the socket state data is freed and on the destructor of the state data it calls AfxSocketTerm which class WSACleanup

          if (m_pfnSockTerm != NULL)
     where m_pfnSockTerm points to AfxSocketTerm. This is my guess. so you need to re-call WSAStartup or AfxSocketInit again


    Author Comment

    yes, after fidgiting around with it for a while, it seems to work after I recall WSAStartup and then create() then connect(). Thank you!

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli‚Ķ
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
    This video discusses moving either the default database or any database to a new volume.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now