Click to See Complete Forum and Search --> : Using WinSock2


cfchen
October 2nd, 2002, 11:06 PM
Dear Friends:

I am trying to use WinSock2 API to create a simple server. However, I encountered a strange linking problem:

------ Build started: Project: Sock, Configuration: Debug Win32 ------

Compiling...
Sock.cpp
Linking...
Sock.obj : error LNK2001: unresolved external symbol "unsigned int __stdcall socket(int,int,int)" (?socket@@$$J212YGIHHH@Z)
Debug/Sock.exe : fatal error LNK1120: 1 unresolved externals

Build log was saved at "file://c:\Documents and Settings\Chi Chen\My Documents\Visual Studio Projects\Sock\Debug\BuildLog.htm"
Sock - 2 error(s), 0 warning(s)


---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped


The source is as the following:
<code>
// This is the main project file for VC++ application project
// generated using an Application Wizard.

#include "stdafx.h"

#include
#using
#include

using namespace System;

// This is the entry point for this application
int _tmain(void)
{
// create a TCP socket
//
// see WinSock2.h for related macros
SOCKET server = 0;
server = socket(AF_INET, SOCK_STREAM, 0/*IPPROTO_TCP*/);
if(server == INVALID_SOCKET)
{
Console::WriteLine(S"*** Oops... ***");
}
return 0;
}
</code>

The stdafx.h file is empty.

My include path:
C:\Program Files\Microsoft SDK\include;C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\include\;C:\Program Files\Microsoft Visual Studio\VC98\atl\include;C:\Program Files\Microsoft Visual Studio\VC98\mfc\include;C:\Program Files\Microsoft Visual Studio\VC98\include

My lib path:
C:\Program Files\Microsoft SDK\Lib;C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Lib\;C:\Program Files\Microsoft Visual Studio\VC98\mfc\lib;C:\Program Files\Microsoft Visual Studio\VC98\lib

Does anyone know what the problem is? Thank you very much in advance.

Thanks,
Chi

JamesSchumacher
October 3rd, 2002, 12:24 AM
By what you have given me, that the error is a linker error. It's looking for the symbol of the function socket(). But what's weird is I believe socket() call is 'C' named, so it's name shouldn't be decorated. (But it is in the linker error). First of all, the problem is that the WS2_32.LIB file is NOT a default library. You have to either #pragma it in, or add it to the list of LIB files included by your application.

As for the name decoration - maybe for some reason, it thinks that there is a socket() call somewhere in a namespace or something. (I see a using namespace directive)

cfchen
October 3rd, 2002, 01:06 PM
Thanks very much for your reply JamesSchumacher.

Could you please elaborate the following points? Thanks! :-)

1. WS2_32.LIB file is NOT a default library. You have to either #pragma it in, or add it to the list of LIB files included by your application.

2. As for the name decoration - maybe for some reason, it thinks that there is a socket() call somewhere in a namespace or something. (I see a using namespace directive)

Mick
October 3rd, 2002, 01:24 PM
Add Ws2_32.lib in the project settings->link General category, Object/Library text entry line.

cfchen
October 3rd, 2002, 02:34 PM
Thanks very much for your kind response Mick_2002.

I am using Visual Studio .NET, however, and I can' seem to find where I can add ws2_32.lib the the lib list. I know that that's how it's done in VC6.

ws2_32.lib is in Microsoft SDK/lib directory.

Thanks,
Chi

Mick
October 3rd, 2002, 02:40 PM
Yea I haven't messed with .NET. But it's got to have some setting where you can tell it what lib you want to link to, I'd think.

JamesSchumacher
October 3rd, 2002, 11:21 PM
should probably work in VC7.


#pragma comment(lib,"WS2_32.LIB")


Put it in StdAfx.h or any .cpp file of your project. This embeds into the object file that the object requires linking with WS2_32.LIB (which is the import library for Winsock 2 implementation).

cfchen
October 3rd, 2002, 11:56 PM
Thanks James.

It works!

I also found another solution to it:

1) Right click on the project root within "Solution Explorer" window.
2) Click on Properties
3) Under Linker/Input, add ws2_32.lib to "Additional Dependencies" entry
4) Click "Apply" and then "OK"
5) Build it

Thanks again for your help. It's highly appreciated. :-D

Chi