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 :)
SpringMVCAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SpringMVCAuthor Commented:
Yep, already added.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.