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

Need help with C programming with servers and clients in linux: Consruct C progr

ID: 3828407 • Letter: N

Question

Need help with C programming with servers and clients in linux:

Consruct C programming code for an echo server file and a log server file (the echo and log servers have a client-server relationship that communicate via UDP (User Datagram Protocol)) so that the echo server will send "echo server is stopping" message to the log server when the echo server is stopped with "ctrl+c". Usually the log server logs the messages the echo server sends to it in an output log file called "myLog.txt", but the log server should not log this message and instead terminate.

The echo server source file name is echoServer.c while the client server source file name is logServer.c

echo server is started with: echoServer 4000 -logip 10.24.36.33 -logport 8888

the above input means that the log server is running at 10.26.36.33 machine, port 8888

In the log server file, an argument passed to the log server should indicate what port addresss it should listen on.

Explanation / Answer

UDP server to a client, a server program:-


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* Server's port number, listen at 3333 */
#define SERVPORT 3333

int main(int argc, char *argv[])
{

int sd, rc;
struct sockaddr_in serveraddr, clientaddr;
int clientaddrlen = sizeof(clientaddr);
int serveraddrlen = sizeof(serveraddr);
char buffer[100];
char *bufptr = buffer;
int buflen = sizeof(buffer);

if((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
perror("UDP server - socket() error");
exit(-1);
}
else
printf("UDP server - socket() is OK ");

printf("UDP server - try to bind... ");

memset(&serveraddr, 0x00, serveraddrlen);
serveraddr.sin_family = AF_INET;
serveraddr.sin_port = htons(SERVPORT);
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if((rc = bind(sd, (struct sockaddr *)&serveraddr, serveraddrlen)) < 0)
{
perror("UDP server - bind() error");
close(sd);
/* If something wrong with socket(), just exit lol */
exit(-1);
}
else
printf("UDP server - bind() is OK ");

printf("Using IP %s and port %d ", inet_ntoa(serveraddr.sin_addr), SERVPORT);
printf("UDP server - Listening... ");

/************************************************/
/* Wait on client requests. */
rc = recvfrom(sd, bufptr, buflen, 0, (struct sockaddr *)&clientaddr, &clientaddrlen);
if(rc < 0)
{
perror("UDP Server - recvfrom() error");
close(sd);
exit(-1);
}
else
printf("UDP Server - recvfrom() is OK for... ");

printf("UDP Server received for the following: "%s" message ", bufptr);
printf("from port %d and address %s. ", ntohs(clientaddr.sin_port),
inet_ntoa(clientaddr.sin_addr));

/* Send a reply, just echo the request */
printf("UDP Server replying to the stupid UDP client in network... ");
rc = sendto(sd, bufptr, buflen, 0, (struct sockaddr *)&clientaddr, clientaddrlen);
if(rc < 0)
{
perror("UDP server - sendto() error");
close(sd);
exit(-1);
}
else
printf("UDP Server - sendto() is OK sarathi ... ");

/* close() the socket descriptor. */
close(sd);
exit(0);
}

Compile and link for above program:
1. [bodo@bakawali testsocket]$ gcc -g udpclient.c -o udpclient

Run the program. Before that make sure the previous program example is running.:
1.[bodo@bakawali testsocket]$ ./udpclient
2.UDP Client - socket() is OK!
3.UDP Client - Usage ./udpclient <Server hostname or IP>
4. UDP Client - Using default hostname/IP!
5.UDP Client - gethostname() of the server is OK...
6.Connected to UDP server bakawali on port 3333.
7.UDP Client - sendto() is OK!
8. Waiting a reply from UDP server...
9.UDP client received the following: "Hello! A client request message lol!" message
10. from port 3333, address 203.106.93.94
11.[bodo@bakawali testsocket]$