Help me with threaded client..

Hi! I am beginner in network programming... and i tried to create UDP server/ client. I have to do it by threads. Every client is a new thread. Can someone help me find the error in my code? It's in pthread_create function but i cannot find the problem....

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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
class udp_client {
public:
    //functions...
private:
    struct sockaddr_in * server;
    struct hostent     * host;
    string     		     f_addr;
    int                	 f_socket;
    int                  f_port;
}

udp_client::udp_client(const string& addr, int port)
: f_port(port)
, f_addr(addr)
{
	if ((host = gethostbyname(f_addr.c_str()))== NULL) {
		perror("host");
		exit(1);
	}

    if ((f_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
		perror("socket");
		exit(1);
    }
    memset((char *) &server, 0, sizeof(server));
	server->sin_family = AF_INET;
	server->sin_port = htons(f_port);
	server->sin_addr = *((struct in_addr*) host->h_addr);
}

udp_client::~udp_client()
{
	close(f_socket);
}

int  udp_client::get_socket()
{
    return f_socket;
}

int udp_client::get_port() const
{
    return f_port;
}

string udp_client::get_addr() const
{
    return f_addr;
}

int udp_client::send(const char *msg, size_t size)
{
	 return sendto(f_socket, msg, size, 0 , (struct sockaddr *) &server, sizeof(server));
	 printf("Send:     %s", msg);
}

int udp_client::recv(const char *msg, size_t size)
{
	socklen_t Addrlen = sizeof(&server);
	 return recvfrom(f_socket, &msg, size, 0 , (struct sockaddr *) &server, &Addrlen);
	 printf("Received: %s", msg);
}

//UDP_CODE
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <pthread.h>
#include "udp_client.h"

using namespace std;

#define NUM_CLIENTS 3
#define NUM_THREADS 3
int 	port;
char *  addr;
char *  msg;

void * request(void * arg)
{
	 long thread_id;
	   thread_id = (long)arg;
	  printf("Thread ID:  %d\n", thread_id );
	udp_client client(addr, port);
	if(client.send(msg, sizeof(&msg)) == -1) {
		perror("sendto()");
		exit(1);
	}
	client.recv(msg, sizeof(&msg));

	pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
	pthread_t threads[NUM_THREADS];

    int  thread_no = 0;
    addr = argv[1];
    port = atoi(argv[2]);
    msg  = argv[3];

    for (int i = 0; i  < NUM_CLIENTS; ++i ) {
    	if(thread_no < NUM_THREADS) {
    		int j = 0;
    		j = pthread_create(&threads[thread_no], NULL, &request, (void *)thread_no);
    		if (j) {
    			printf(" A request cann't be procceses.\n");
         	} else {
    		    ++thread_no;
    		}
    		pthread_join(threads[thread_no], NULL);
    	 }
    }
    return 0;
}
It would help if you described the error, instead of expecting us to just examine your code, pointing out various problems.
It prints out Segmentation fault(core) when try to examine this
server->sin_addr = *((struct in_addr*) host->h_addr);
I create a socket and want to give a server host address.
i tried to create UDP server/ client. I have to do it by threads. Every client is a new thread.

The start of your problem is UDP is a datagram protocol, there is no notion of session.

A UDP packet is received by the server using recvfrom(), and recvfrom gets the address of client that sent the message. So your server needs to manage your session and dispatch the message to the right handler.

So you need to decide what a session is, and create a handler function to deal with it. You then run the handler function in a seperate thread, and maintain a map of client/hander for the dispatcher to use.

You don't have these problems with TCP, as TCP handles streams.

Your code won't work.
Last edited on
closed account (N36fSL3A)
If you're doing UDP multiple threads are not required... is this a homework assignment where it is?
Topic archived. No new replies allowed.