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


cfchen
October 2nd, 2002, 11:35 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 <WinSock2.h>
#using <mscorlib.dll>
#include <tchar.h>

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

FirePhoenix
September 4th, 2004, 01:54 PM
I have been having the exact same problem, and i can't figure out what the heck is wrong with the code. I don't know if you'll see this post, but If you do, have you figured out what the problem was? If so, please let me know. Thanks in advance.
~Phoenix

cfchen
September 4th, 2004, 02:22 PM
it has been a while since I worked on it.

I think that was what I had to do (hope this helps):


// For using Microsoft SDK headers
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500
#endif

#ifndef WINVER
#define WINVER 0x0500
#endif

// WinSock2.h has to come before to prevent some socket data
// structures from being redefined
#include <winsock2.h>
#include <windows.h>
#pragma comment(lib, "WS2_32.LIB")

// Triton includes
#include <stdlib.h>
#include <stdio.h>

jaljal
October 18th, 2004, 03:51 PM
Thanks; this was very helpful and solved my problem.