Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Error when create socket

Posted on 1999-07-19
4
Medium Priority
?
1,146 Views
Last Modified: 2006-11-17
 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())
      {
            i=m_pSocket->GetLastError();
            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?
Regards
0
Comment
Question by:goyam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1200481
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.)
0
 
LVL 2

Author Comment

by:goyam
ID: 1200482
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.)
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 1200483
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)

AfxSocketInit
BOOL AfxSocketInit( WSADATA* lpwsaData = NULL );

Return Value

Nonzero if the function is successful; otherwise 0.

Parameters

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.

Remarks

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!
0
 
LVL 2

Author Comment

by:goyam
ID: 1200484
Thanks a lot,
Regards
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

670 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