Error when create socket

 I'm using MSVC6.0 . I'm tring do make a client app (I use sample Chatter as model) but when I can't create the socket:
      m_pSocket = new CChatSocket(this);
      if (!m_pSocket->Create())
            delete m_pSocket;
            m_pSocket = NULL;
            AfxMessageBox("Socket can't be create!");
            return FALSE;
because it's an error at runtime(I got i=10093) and in help i've found 10093 = WSANOTINITIALISED .
  Can anyone help?
Who is Participating?
jkrConnect With a Mentor Commented:
nietod is basically right, in a 'normal' Win32 app you'd use

    WORD            wVersionRequested;
    WSADATA         wsaData;

    wVersionRequested   =   MAKEWORD(   1,  1);

    if  (   nErr    =   WSAStartup  (   wVersionRequested,  &wsaData))
            //  error

When using MFC, make sure to call 'AfxSocketInit()' (which will also maintain the MFC state):

(From the docs)

BOOL AfxSocketInit( WSADATA* lpwsaData = NULL );

Return Value

Nonzero if the function is successful; otherwise 0.


lpwsaData   A pointer to a WSADATA structure. If lpwsaData is not equal to NULL, then the address of the WSADATA structure is filled by the call to ::WSAStartup. This function also ensures that ::WSACleanup is called for you before the application terminates.


Call this function in your CWinApp::InitInstance override to initialize Windows Sockets.

See Also   CWinApp::InitInstance

NOTE that you'll have to place this call explicitly in your 'CWinApp::InitInstance()' - MFC doesn't do that automatically!
This error is returned when you use a socket function before WSAStartup() has been called.  Do you call WSAStartup()?   (I'm not sure if you have to however, as MFC may do this for you.)
goyamAuthor Commented:
No, I don't call WSAStartup(), because I've understood that MFC will (just as I said before I use sample chatter as model and I copyed the socket class, and on OnNewDocument I call a function to create the socket, function that calls CSocket::Create.)
goyamAuthor Commented:
Thanks a lot,
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.