Winsock Programming

I have had nothing but trouble with Winsock since I started using it. I cannot seem to initialize Winsock to save my life. I'm not asking for anyone to write the whole program ( As I know how annoying that is ) I just need help with Winsock. I have tried several compilers and always get weird errors.

*****
Here is the output from VC++ 2008 Express Edition:
*****
1
2
3
4
5
6
7
8
9
1
1>Compiling...
1>main.cpp
1>Linking...
1>main.obj : error LNK2019: unresolved external symbol __imp__WSACleanup@0 referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol __imp__WSAStartup@8 referenced in function _main
1>C:\Users\Rory\ProjectX\ProjectX\Debug\ProjectX.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\Users\Rory\ProjectX\ProjectX\ProjectX\Debug\BuildLog.htm"
1>ProjectX - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


*****
Here is my code:
*****
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

#include <cstdlib>
#include <iostream>
#include <string.h>
#include <winsock2.h>

int iReqWinsockVer = 2;

using namespace std;

int main()
{
    cout<<"Initializing Winsock 2...\n";
	
	// WINSOCK INITIALIZATION

WSADATA wsaData;

if (WSAStartup(MAKEWORD(iReqWinsockVer,0), &wsaData)==0) {
    // Check if major version is at least iReqWinsockVer
    if (LOBYTE(wsaData.wVersion) >= iReqWinsockVer) {
        // Network stuff here
    }
    else {
        // Required version not available
    }

    // Cleanup winsock
    if (WSACleanup()!=0) {
        // cleanup failed
		}
	}
else {
    //  startup failed
}
	// END WINSOCK INITIALIZATION

		system("PAUSE");
	}


Thanks for any help in advance!
Last edited on
Add ws2_32.lib as linker input.
I have looked in project configuration, but I cannot seem to find the proper place. There are a lot of options that reference input. How should I do this?
Project Properties->linker->input page

On that page you will see Additional Dependencies.

Put it in there - note that library names should be seperated with spaces
Or you could add this line directly to your source file:
#pragma comment(lib, "ws2_32.lib")
Thanks guys!
Last edited on
It's doing the same thing D: What should I try next?
Well the simple question is - Are you sure you did it correctly?
write the following line just after the include statements . . .


#pragma comment(lib, "ws2_32.lib")


as stated earlier this will definitely work fine. ..
I created a new project, and added the previous source file, then put

#pragma comment(lib, "ws2_32.lib")

And It worked fine. Thank you! Not quite sure what went wrong the first time I tried it.
Topic archived. No new replies allowed.