• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1084
  • Last Modified:

WinSock, is WSAStartup required?

Hello!
I am a newbie in C++ and in winsock programming.
I've taken one FTP library, which uses CBlockingSocket implementation.
The problem is, that there is method CBlockingSocket::Create, which
calls for socket(AF_INET, SOCK_STREAM, 0). It works with a different visual studios( 2008, 2005 ) under different OS ( Vista, Win7, XP).
But there is one machine with VS2005 WinXP, there this method returns INVALID_SOCKET.
I have tried different things and found out, that it will start to work if i will call WSAStartup() before socket(AF_INET, SOCK_STREAM, 0).
In MSDN description of WSAStartup is written, that this function must be called, before any other functions in WinSock.
But why this works on most machines, and on only one computer - it doesn't work?
Any comments? Suggestions? Ideas?

P.S Sorry for my english :)
0
SpringMVC
Asked:
SpringMVC
  • 6
  • 5
  • 2
1 Solution
 
Infinity08Commented:
>> But why this works on most machines, and on only one computer - it doesn't work?

Undefined behavior is precisely that : undefined. Anything could happen - including the code working as you expected.

That doesn't make it correct though ;) When doing socket programming on Windows platforms, you need to initialize using WSAStartup, before any winsock functions are called.
0
 
SpringMVCAuthor Commented:
I am using winsock in one thread, if I initialize it with WSAStartup and then clean it with WSACleanup,
I hope it won't affect on different threads or different processes?
In msdn is written, that WSACleanup just decrements internal reference count, so actual clean up makes the last call.
0
 
jkrCommented:
If 'WSAStartup()' hasn't been called before using any Winsock functions, you get 'WSANOTINITIALISED' from 'WSAGetLastError()'. 'INVALID_SOCKET' - especially if that occurs only on one machine - would rather make me check the firewall settings.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
Infinity08Commented:
My suspicion was that the WSAStartup was called from some dependent library, which is why it worked on some platforms (not by design, but by accident).
0
 
jkrCommented:
Well, most Winsock libs that reside in DLLs call 'WSAStartup()' in their 'DllMain()' - if that call is omitted (or the wron Winsock version was initialized) none of the Winsock functions will yield anything else but WSANOTINITIALISED, so that condition should be really easy to detect.
0
 
SpringMVCAuthor Commented:
I couldn't detect a condition, why these function doesn't work on one particular computer, but I included WSAStartup and WSACleanup to my code, after that it seems, that everything works, so I stick with that.
Anyway, thanks for comments.
0
 
SpringMVCAuthor Commented:
I think, that Infinity08 is right, and by accident WSAStartup was called by some other library, process etc.
0
 
jkrCommented:
The call definitely is required. If other work, it has definitely been makde by some other module. But again: If you get 'INVALID_SOCKET' only on one machine, check the firewall settings.
0
 
SpringMVCAuthor Commented:
There are no firewall :)
Even Windows firewall is turned off
0
 
jkrCommented:
>>Even Windows firewall is turned off

That's th eone I was thinking about.

Can you check the return value of 'WSAGetLastError()' (http://msdn.microsoft.com/en-us/library/ms741580(VS.85).aspx) after getting 'INVALID_SOCKET'?
0
 
SpringMVCAuthor Commented:
Already checked :)
If WSAStartup is not called - socket method will fail(and return INVALID_SOCKET) and WSAGetLastError will return WSANOTINITIALISED.
If WSAStartup is called - everything works fine.

Of course, that is only for one computer. On other computers everything works without WSAStartup, but I think that's just accidentally... :)
0
 
jkrCommented:
Well, in that case, just add the following snippet to your code - calling that more than once does no harm:
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.                                  */
    return;
}
 
/* Confirm that the WinSock DLL supports 2.2.*/
/* Note that if the DLL supports versions greater    */
/* than 2.2 in addition to 2.2, it will still return */
/* 2.2 in wVersion since that is the version we      */
/* requested.                                        */
 
if ( LOBYTE( wsaData.wVersion ) != 2 ||
        HIBYTE( wsaData.wVersion ) != 2 ) {
    /* Tell the user that we could not find a usable */
    /* WinSock DLL.                                  */
    WSACleanup( );
    return; 
}
 
/* The WinSock DLL is acceptable. Proceed. */

Open in new window

0
 
SpringMVCAuthor Commented:
Yep, already added.
0
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.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now