Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

create and combine the UDP client and UDP server programs in C language into one

ID: 3731862 • Letter: C

Question

create and combine the UDP client and UDP server programs in C language into one multithreaded P2P (peer-to-peer) application in C language which is a client and a server at the same time. The program shall start two threads. One thread shall execute the send loop: display the prompt; read user input; send it to the other chat application; repeat. The other thread shall execute the receive loop: receive a message from the other char application; display the received message; repeat. The send loop shall terminate when the user types %stop' (less the quotes). The main thread shall wait for the other two threads to terminate.The application shall maintain the counter of the sent and received messages. It shall increment the counter for each received message and decrement it for each sent message. Since the counter is a shared resource, it shall be locked by a mutex before the modifications. The initial value of the counter shall be 0. 10

Explanation / Answer

C code:

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

//Counter for send and receive messages.
int g_send;
int g_recv;
pthread_mutex_t lock;

//UDP client thread.
void *udp_client(void *vargp)
{
int sock;
struct sockaddr_in server_addr;
struct hostent *host;
char send_data[1024];

host= (struct hostent *) gethostbyname((char *)"127.0.0.1");


if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
{
perror("socket");
exit(1);
}

server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(5000);
server_addr.sin_addr = *((struct in_addr *)host->h_addr);
bzero(&(server_addr.sin_zero),8);

while (1)
{

printf("UDP client: Type Something (%%stop to quit):");
gets(send_data);

sendto(sock, send_data, strlen(send_data), 0,
(struct sockaddr *)&server_addr, sizeof(struct sockaddr));

printf("Taking Mutex lock ");
pthread_mutex_lock(&lock);
printf("UDP client : updating the send counter ");
g_send++;
pthread_mutex_unlock(&lock);
printf("Releasing Mutex Lock ");

usleep(5);
//Stoping the client.
if ((strcmp(send_data , "%stop") == 0))
break;

}
close(sock);
return NULL;

}//udp client ends

//udp server starts
void * udp_server(void *vargp)
{
int sock;
struct sockaddr_in addr;
char buffer[1024];

//creating the socket
sock = socket(AF_INET, SOCK_DGRAM, 0);

// Filing the server address
bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(5000);
addr.sin_addr.s_addr = INADDR_ANY;

//binding the addresss
if ( bind(sock, (struct sockaddr*)&addr, sizeof(addr)) != 0 )
perror("bind");

//Starting the listening loop
while (1)
{
int bytes, addr_len=sizeof(addr);
memset(buffer,0, 1024);
bytes = recvfrom(sock, buffer, sizeof(buffer), 0, (struct sockaddr*)&addr, &addr_len);
printf("UDP server: msg recieved %s ", buffer);

printf("Taking Mutex lock ");
pthread_mutex_lock(&lock);
printf("UDP client : updating the recv counter ");
g_recv++;
pthread_mutex_unlock(&lock);
printf("Releasing Mutex Lock ");

//stoping the server
if (strncmp(buffer, "%stop", 5) == 0)
break;
}
close(sock);
return NULL;

}//udp server ends

int main()
{
pthread_t send, recv;

//initalizing the mutex.
if (pthread_mutex_init(&lock, NULL) != 0)
{
printf(" mutex init has failed ");
return 1;
}
printf("Starting Thread ");
pthread_create(&send, NULL, udp_client, NULL);
pthread_create(&recv, NULL, udp_server, NULL);

// Waiting for the thread to exit.
pthread_join(send, NULL);
pthread_join(recv, NULL);
printf("Main : Msg sent : %d Msg recv : %d ", g_send, g_recv);
exit(0);
}

Output shown after running this program:

./a.out

Starting Thread
UDP client: Type Something (%stop to quit):rohit
Taking Mutex lock
UDP client : updating the send counter
Releasing Mutex Lock

UDP server: msg recieved rohit
Taking Mutex lock
UDP client : updating the recv counter
Releasing Mutex Lock

UDP client: Type Something (%stop to quit):%stop
Taking Mutex lock
UDP client : updating the send counter
Releasing Mutex Lock

UDP server: msg recieved %stop
Taking Mutex lock
UDP client : updating the recv counter
Releasing Mutex Lock

Main : Msg sent : 2 Msg recv : 2

Steps to compile:

gcc -g main.c -lpthread

Note: pthread library is used to implement thread and mutex . please share your feedback