I've seen plenty of other questions like this but I just couldn't figure this problem out with the help of them. I've understood that it's a linking problem but from what I can see, I've got the linking straightened out.
I'm writing a chat server/client (with the help of this article).
I've defined a class to hold the server functions and have a header-file that handles all the includes.
This is the header file:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <tchar.h>
#include <strsafe.h>
#include "resource1.h"
class ChatServer
{
public: int InitServer(HINSTANCE hInst);
public: void ReportError(int errorCode, const char *whichFunc);
};
This is the actual server "class":
#include "server.h"
#define NETWORK_ERROR -1
#define NETWORK_OK 0
//Keeps stuff for the server
int ChatServer::InitServer(HINSTANCE hInst)
{
WORD sockVersion;
WSADATA wsaData;
int nret;
sockVersion = MAKEWORD(1,1); //Version 1.1
//Init winsock
WSAStartup(sockVersion, &wsaData);
//Create listening socket
SOCKET listeningSocket;
//AFINET - Go over TCP
//SOCK_STREAM - Stream oriented socket
//IPPROTO_TCP - Use tcp rather than udp
listeningSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP);
if(listeningSocket == INVALID_SOCKET)
{
nret = WSAGetLastError(); //Get error detail
ReportError(nret, "socket()");
WSACleanup();
return NETWORK_ERROR;
}
SOCKADDR_IN serverInfo;
serverInfo.sin_family = AF_INET;
serverInfo.sin_addr.s_addr = INADDR_ANY;
serverInfo.sin_port = htons(1337);
//Bind the socket to local server address.
nret = bind(listeningSocket, (LPSOCKADDR)&serverInfo, sizeof(struct sockaddr));
if(nret == SOCKET_ERROR)
{
nret = WSAGetLastError();
ReportError(nret, "bind()");
WSACleanup();
return NETWORK_ERROR;
}
//Make socket listen
nret = listen(listeningSocket, 10); //Up to 10 connections at the same time.
if(nret = SOCKET_ERROR)
{
nret = WSAGetLastError();
ReportError(nret, "listen()");
WSACleanup();
return NETWORK_ERROR;
}
//Wait for client
SOCKET theClient;
theClient = accept(listeningSocket, NULL, NULL);
if(theClient == INVALID_SOCKET)
{
nret = WSAGetLastError();
ReportError(nret, "accept()");
WSACleanup();
return NETWORK_ERROR;
}
//Send and receive from the client, and finally,
closesocket(theClient);
closesocket(listeningSocket);
//shutdown
WSACleanup();
return NETWORK_OK;
}
void ChatServer::ReportError(int errorCode, const char *whichFunc)
{
char errorMsg[92]; // Declare a buffer to hold
// the generated error message
ZeroMemory(errorMsg, 92); // Automatically NULL-terminate the string
// The following line copies the phrase, whichFunc string, and integer errorCode into the buffer
sprintf(errorMsg, "Call to %s returned error %d!", (char *)whichFunc, errorCode);
MessageBox(NULL, errorMsg, "socketIndication", MB_OK);
}
And lastly, the main.cpp file with the entry-method for the program calls "ChatServer::InitServer(g_hInst)". It's pretty big so I omitted it but if it's needed I'll post it aswell.
The error messages I'm getting are like the one below but they all state problems with the api-functions related to winsockets API:
Error 3 error LNK2019: unresolved external symbol _closesocket@4 referenced in function "public: int __thiscall ChatServer::InitServer(struct HINSTANCE__ *)" (?InitServer@ChatServer@@QAEHPAUHINSTANCE__@@@Z)
As I stated before, I believe this problem has something to do with the compiler misunderstanding what to do with functions like "closesocket" that should be linked to winsock.h.
Thanks for any advice at all and thanks for reading all of this gibberish :)
These linker errors are always the same: you're using a symbol that the linker cannot find. You need to tell the linker to link with the libraries that contain those symbols.
As I stated before, I believe this problem has something to do with the compiler misunderstanding what to do with functions like "closesocket" that should be linked to winsock.h.
No, closesocket
is not linked to winsock.h. winsock.h is a header file, not a library. It may contain a declaration of closesocket
and that's ok for the compiler, but the linker really needs to know where the code of that function is so it can link your program to it.
You should probably be using winsock2.h instead of winsock.h, though. The Windows Sockets API 2.0 dates from 1994, and version 1 is long obsolete.
You can see at the bottom of this function's documentation that the library where it resides is ws2_32.lib.
Your class can be defined like so:
class ChatServer
{
public:
int InitServer(HINSTANCE hInst);
void ReportError(int errorCode, const char *whichFunc);
};
However you're error is caused by not including the library input. On your project input line add the dependency on winsock library also consider using winsock2.h.
For reference: http://msdn.microsoft.com/en-us/library/ms737629(v=vs.85).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With