Click to See Complete Forum and Search --> : undefined reference to


Tivoilos
June 9th, 2009, 02:22 PM
I am writing a Multithreaded Server I am not having problems with my NetWorking code just with my threading code. I am using _beginthread which is found in process.h

#include <stdio.h>
#include <winsock2.h>
#include <stdlib.h>
#include <process.h>
#include <windows.h>
#include <conio.h>
#define maxconnections 5

void ClientThread(void *client);
void MTServerThread(void *dummy);
int thread()
{

_beginthread(MTServerThread, 0, NULL);

}

void MTServerThread(void *dummy)
{
WSAData wsaData;

SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
int eServ = WSAStartup(MAKEWORD(2,2), &wsaData);

if(eServ != 0)
{
printf("WSAStartup failed: %d\n", eServ);
exit (1);
}

sockaddr_in sin;
sin.sin_port = htons(6000);
sin.sin_addr.s_addr = inet_addr("127.0.0.1");
sin.sin_family = AF_INET;

if(bind(server,(sockaddr*)&sin, sizeof(sin) == INVALID_SOCKET))
{
printf("bind failed: %d\n", WSAGetLastError());
WSACleanup();
exit (1);
}

if(listen(server,10)!=0)
{
printf("listen failed: %d\n", WSAGetLastError());
exit (1);
}

SOCKET client;
sockaddr_in from;
int fromlen=sizeof(from);

while(true)
{
client=accept(server,(sockaddr*)&from, &fromlen);
_beginthread(ClientThread, 0, (void*)(client));
}
_endthread();
_endthread();
exit(0);
}


The problem I am having problems with is

_beginthread(ClientThread, 0,(void*)(client));

It says undefined reference to 'ClientThread' I have referenced it though =[

any help?

MrViggy
June 9th, 2009, 05:50 PM
That's a linker error. You have declared it, but didn't define. You need to fill out the body of the "ClientThread" function.

Viggy

Tivoilos
June 10th, 2009, 11:31 PM
Care to explain a little better?

Arjay
June 11th, 2009, 01:41 AM
Care to explain a little better?In your code see MTServerThread. There is a declaration of MTServerThread:
void MTServerThread(void *dummy);
and a definition:
void MTServerThread(void *dummy)
{
WSAData wsaData;

SOCKET server = socket(AF_INET, SOCK_STREAM, 0);
int eServ = WSAStartup(MAKEWORD(2,2), &wsaData);
...
}

For the ClientThread, there is only a declaration. As MrViggy stated, you need to provide its definition (or in other words, you need to implement it).