Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<windows.h> already included
WINVER = 0x0501 for Xp already defined in windows.h
Server program, using TCP

C++
#include <stdio.h>
#include <winsock2.h>
 
int main()
{
WORD wVersionRequested;
WSADATA wsaData;
int wsaerr;
 
// Using MAKEWORD macro, Winsock version request 2.2
wVersionRequested = MAKEWORD(2, 2);
 
wsaerr = WSAStartup(wVersionRequested, &wsaData);
if (wsaerr != 0)
{
    /* Tell the user that we could not find a usable WinSock DLL.*/
    printf("Server: The Winsock dll not found!\n");
    return 0;
}
else
{
       printf("Server: The Winsock dll found!\n");
       printf("Server: The status: %s.\n", wsaData.szSystemStatus);
}
 
/* 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.*/
    printf("Server: The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
    WSACleanup();
    return 0;
}
else
{
       printf("Server: The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion), HIBYTE(wsaData.wVersion));
       printf("Server: The highest version this dll can support: %u.%u\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
}
 
//////////Create a socket////////////////////////
//Create a SOCKET object called m_socket.
SOCKET m_socket;
 
// Call the socket function and return its value to the m_socket variable.
// For this application, use the Internet address family, streaming sockets, and
// the TCP/IP protocol.
// using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 
// Check for errors to ensure that the socket is a valid socket.
if (m_socket == INVALID_SOCKET)
{
    printf("Server: Error at socket(): %ld\n", WSAGetLastError());
    WSACleanup();
    return 0;
}
else
{ printf("Server: socket() is OK!\n"); }
 
////////////////bind//////////////////////////////
// Create a sockaddr_in object and set its values.
sockaddr_in service;
 
// AF_INET is the Internet address family.
service.sin_family = AF_INET;
// "127.0.0.1" is the local IP address to which the socket will be bound.
service.sin_addr.s_addr = inet_addr("127.0.0.1");
// 55555 is the port number to which the socket will be bound.
service.sin_port = htons(55555);
 
// Call the bind function, passing the created socket and the sockaddr_in structure as parameters.
// Check for general errors.
if (bind(m_socket, (SOCKADDR*)&service, sizeof(service)) == SOCKET_ERROR)
{
    printf("Server: bind() failed: %ld.\n", WSAGetLastError());
    closesocket(m_socket);
    return 0;
}
else
{
    printf("Server: bind() is OK!\n");
}
 
// Call the listen function, passing the created socket and the maximum number of allowed
// connections to accept as parameters. Check for general errors.
if (listen(m_socket, 10) == SOCKET_ERROR)
    printf("Server: listen(): Error listening on socket %ld.\n", WSAGetLastError());
else
{
   printf("Server: listen() is OK, I'm waiting for connections...\n");
}
 
// Create a temporary SOCKET object called AcceptSocket for accepting connections.
SOCKET AcceptSocket;
 
// Create a continuous loop that checks for connections requests. If a connection
// request occurs, call the accept function to handle the request.
printf("Server: Waiting for a client to connect...\n" );
printf("***Hint: Server is ready...run your client program...***\n");
// Do some verification...
while (1)
{
    AcceptSocket = SOCKET_ERROR;
    while (AcceptSocket == SOCKET_ERROR)
    {
        AcceptSocket = accept(m_socket, NULL, NULL);
    }
 
// else, accept the connection...
// When the client connection has been accepted, transfer control from the
// temporary socket to the original socket and stop checking for new connections.
printf("Server: Client Connected!\n");
m_socket = AcceptSocket; 
break;
}
 
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[200] = "This string is a test data from server";
// initialize to empty data...
char recvbuf[200] = "";
 
// Send some test string to client...
printf("Server: Sending some test data to client...\n");
bytesSent = send(m_socket, sendbuf, strlen(sendbuf), 0);
 
if (bytesSent == SOCKET_ERROR)
       printf("Server: send() error %ld.\n", WSAGetLastError());
else
{
       printf("Server: send() is OK.\n");
       printf("Server: Bytes Sent: %ld.\n", bytesSent);
}
 
// Receives some test string from client...and client
// must send something lol...
bytesRecv = recv(m_socket, recvbuf, 200, 0);
 
if (bytesRecv == SOCKET_ERROR)
       printf("Server: recv() error %ld.\n", WSAGetLastError());
else
{
       printf("Server: recv() is OK.\n");
       printf("Server: Received data is: \"%s\"\n", recvbuf);
       printf("Server: Bytes received: %ld.\n", bytesRecv);
}
 
WSACleanup();
return 0;
}


error log'

1>------ Build started: Project: server, Configuration: Debug Win32 ------
1>Compiling...
1>server.cpp
1>Linking...
1>server.obj : error LNK2028: unresolved token (0A000018) "extern "C" int __stdcall WSAStartup(unsigned short,struct WSAData *)" (?WSAStartup@@$$J18YGHGPAUWSAData@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A00001B) "extern "C" unsigned long __stdcall inet_addr(char const *)" (?inet_addr@@$$J14YGKPBD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A000020) "extern "C" int __stdcall WSAGetLastError(void)" (?WSAGetLastError@@$$J10YGHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A000022) "extern "C" unsigned short __stdcall htons(unsigned short)" (?htons@@$$J14YGGG@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A00002B) "extern "C" int __stdcall WSACleanup(void)" (?WSACleanup@@$$J10YGHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A00002C) "extern "C" int __stdcall recv(unsigned int,char *,int,int)" (?recv@@$$J216YGHIPADHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A00002D) "extern "C" int __stdcall bind(unsigned int,struct sockaddr const *,int)" (?bind@@$$J212YGHIPBUsockaddr@@H@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A000031) "extern "C" unsigned int __stdcall socket(int,int,int)" (?socket@@$$J212YGIHHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A000037) "extern "C" int __stdcall closesocket(unsigned int)" (?closesocket@@$$J14YGHI@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A000041) "extern "C" int __stdcall send(unsigned int,char const *,int,int)" (?send@@$$J216YGHIPBDHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A000047) "extern "C" int __stdcall listen(unsigned int,int)" (?listen@@$$J18YGHIH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2028: unresolved token (0A00004A) "extern "C" unsigned int __stdcall accept(unsigned int,struct sockaddr *,int *)" (?accept@@$$J212YGIIPAUsockaddr@@PAH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall recv(unsigned int,char *,int,int)" (?recv@@$$J216YGHIPADHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall send(unsigned int,char const *,int,int)" (?send@@$$J216YGHIPBDHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" unsigned int __stdcall accept(unsigned int,struct sockaddr *,int *)" (?accept@@$$J212YGIIPAUsockaddr@@PAH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall listen(unsigned int,int)" (?listen@@$$J18YGHIH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall closesocket(unsigned int)" (?closesocket@@$$J14YGHI@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall bind(unsigned int,struct sockaddr const *,int)" (?bind@@$$J212YGHIPBUsockaddr@@H@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" unsigned short __stdcall htons(unsigned short)" (?htons@@$$J14YGGG@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" unsigned long __stdcall inet_addr(char const *)" (?inet_addr@@$$J14YGKPBD@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall WSAGetLastError(void)" (?WSAGetLastError@@$$J10YGHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" unsigned int __stdcall socket(int,int,int)" (?socket@@$$J212YGIHHH@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall WSACleanup(void)" (?WSACleanup@@$$J10YGHXZ) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>server.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall WSAStartup(unsigned short,struct WSAData *)" (?WSAStartup@@$$J18YGHGPAUWSAData@@@Z) referenced in function "int __cdecl main(void)" (?main@@$$HYAHXZ)
1>C:\Documents and Settings\syam\My Documents\Visual Studio 2008\Projects\server\Debug\server.exe : fatal error LNK1120: 24 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\syam\My Documents\Visual Studio 2008\Projects\server\server\Debug\BuildLog.htm"
1>server - 25 error(s), 0 warning(s)<pre lang="text">
Posted
Updated 4-Jun-12 7:39am
v3
Comments
Albert Holguin 4-Jun-12 13:41pm    
How did you write all this code without getting to this problem before? Did you copy code from somewhere? It's not a bad idea to reuse code or use libraries, but you should definitely understand what it's doing.

In C/C++, only the first (few) errors are worth to look at, the rest is followup chaos caused by the first ones.

The first error says clearly that WSAStartup is not found. You need to add a reference to the respective library (dll) that contains that function.

Cheers
Andi
 
Share this answer
 
v3
Comments
Albert Holguin 4-Jun-12 13:38pm    
+5
Andreas Gieriet 4-Jun-12 13:41pm    
Thanks for your 5!
Andi
Albert Holguin 4-Jun-12 14:32pm    
Sure, thanks for providing solutions! :)
sree rama murthy nunna 6-Jun-12 4:02am    
Thank for your help
Andreas Gieriet 6-Jun-12 7:29am    
You are welcome. So, you might *accept* this and the other solution (see header of the solutions)?
Cheers
Andi
The Documentation[^] clearly states the needed DLLs / Librarys to link with. Did you do that?

Requirements
Minimum supported client
Windows 2000 Professional
Minimum supported server
Windows 2000 Server
Header
Winsock2.h
Library
Ws2_32.lib
DLL
Ws2_32.dll
 
Share this answer
 
Comments
Albert Holguin 4-Jun-12 13:38pm    
Need to tell linker about ws2_32.lib... +5
sree rama murthy nunna 6-Jun-12 4:02am    
Thank for your help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900