Pthread socket connection

Hi all,

I have a problem. The code is from an example that I've followed to the letter and it compiles fine, however what stumps me is that the build is unsuccessful as a result of "undefined reference to connectToHost()". Any ideas for why this is happening and what I should to fix this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef GAMEPROJECTCLIENT_H
#define	GAMEPROJECTCLIENT_H

#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <cstdlib>
#include <pthread.h>
using namespace std;

class ChatPair{
private:
    class Attribs {
    public:
        int * socket;
        pthread_t sendThread, receiveThread;
    } attribs;

public:
    ChatPair(int);
    static void * sendMessage(void *);
    static void * receiveMessage(void *);
    void start();
    void join();
    virtual ~ChatPair();
};

#endif	/* GAMEPROJECTCLIENT_H */ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "GameProjectClient.h"

ChatPair::ChatPair(int socket){
    attribs.socket = new int(socket);
    start();
}

void ChatPair::start() {
    pthread_create(&attribs.sendThread, NULL, (void * (*) (void *))sendMessage, &attribs);
    pthread_create(&attribs.receiveThread, NULL, (void * (*) (void *))receiveMessage, &attribs);
}

void ChatPair::join() {
    pthread_join(attribs.receiveThread, NULL);
    pthread_join(attribs.sendThread, NULL);
}

void * ChatPair::receiveMessage(void* params) {
    char buf[100];
    Attribs *attribs = (Attribs *)params;
    int *listenSocket = attribs->socket;

    while(true){
        if(recv(*listenSocket, buf, 100, 0) <= 0){
            cerr << "Server not responding" << endl;
            break;
        }
        if(strlen(buf) > 0) cout << "\t Server: " << buf << endl << flush;
    }
    close(*listenSocket);
    pthread_cancel(attribs->sendThread);
}

void * ChatPair::sendMessage(void* params) {
    char line[100];
    string input;
    Attribs *attribs = (Attribs *)params;
    int *talkSocket = attribs->socket;

    while(true) {
        getline(cin, input);
        strcpy(line, input.c_str());
        if(strlen(line) == 1 && line[0] == 'q') {
            cout << "Quit" << endl << flush;
            break;
        }
        else if (send(*talkSocket, line, strlen(line) + 1, 0) < 0) {
            cerr << "Error: Cannot send message" << endl;
            break;
        }
    }

    close(*talkSocket);
    pthread_cancel(attribs->receiveThread);
}

ChatPair::~ChatPair() {
    delete attribs.socket;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <cstdlib>

#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>

#include "GameProjectClient.h"

using namespace std;

int connectToHost(string hostURL, int port);
string getDestination();

int main() {
    pthread_t receiveThread, sendThread;
    string destination = getDestination();
    int socket = connectToHost(destination, 2255); //problem relates to this line of code here
    cout << "Connected to server. Start Conversation\n" << flush;

    ChatPair *chatpair = new ChatPair(socket);
    chatpair->join();

    close(socket);
    return EXIT_SUCCESS;
}

int ConnectToHost(string hostURL, int serverPort) {
    int *socketDescriptor;
    struct sockaddr_in serverAddress;
    struct hostent *hostInfo;
    char buf[100];

    hostInfo = gethostbyname(hostURL.c_str());
    if(hostInfo == NULL){
        cout << "Problem interpreting host: " << buf << endl;
        exit(1);
    }

    int conn = socket(AF_INET, SOCK_STREAM, 0);
    socketDescriptor = &conn;
    if (*socketDescriptor < 0) {
        cerr << "Cannot create socket \n";
        exit(1);
    }

    serverAddress.sin_family = hostInfo->h_addrtype;
    memcpy((char *) &serverAddress.sin_addr.s_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
    serverAddress.sin_port = htons(serverPort);

    if (connect(*socketDescriptor, (struct sockaddr *) &serverAddress, sizeof(serverAddress)) < 0) {
        cerr << "Cannot connect \n";
        exit(1);
    }

    return *socketDescriptor;
}

string getDestination() {
    string destination;
    cout << "Address of server: ";
    getline(cin, destination);
    if (destination.compare("") == 0) destination = "localhost";

    return destination;
}
closed account (DSLq5Di1)

int connectToHost(string hostURL, int port);

int ConnectToHost(string hostURL, int serverPort) { ... }
Thank you for pointing that out. I'm still getting the hang of C++!
Topic archived. No new replies allowed.