Find the solution using C language 1. MULTI-FUNCTION CLIENT AND SERVER: This pro
ID: 3748125 • Letter: F
Question
Find the solution using C language
1. MULTI-FUNCTION CLIENT AND SERVER: This problem involves the development of a simple client-server application. Upon execution, the client should print a menu of possible commands, and prompt the user to enter the number of a command which should be executed on the server. Then, the client should send the number to the server (which is waiting to read requests on a predefined UDP port). The server should read the number from the client and execute the appropriate command, returning the results (as a string) to the client. The client should print the reply to the console and then be ready to accept additional commands from the user The command that should be available are: 1. Current time 2. PID (process ID) of the server 3. Random number between 1 and 20, inclusive Note: The Application Layer Protocol should be clearly defined, and must exchange data in ASCII format only. The server should be connectionless and iterative, i.e.only process one request at a time (see net2/server.c). Don't worry about making the communication reliable, e.g. by adding timeouts, etcExplanation / Answer
SERVER
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
int main()
{
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9734);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address,server_len);
/* Create a connection queue, ignore child exit details and wait for
clients. */
listen(server_sockfd, 5);
signal(SIGCHLD, SIG_IGN);
while(1) {
char ch;
printf("server waiting ");
/* Accept connection. */
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd,(struct sockaddr *)&client_address, &client_len);
/* Fork to create a process for this client and perform a test to see
whether we're the parent or the child. */
if(fork() == 0) {
/* If we're the child, we can now read/write to the client on
client_sockfd.
The five second delay is just for this demonstration. */
read(client_sockfd, &ch, 1);
sleep(5);
ch++;
write(client_sockfd, &ch, 1);
close(client_sockfd);
exit(0);
}
/* Otherwise, we must be the parent and our work for this client is
finished. */
else {
close(client_sockfd);
}
}
}
CLIENT
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>
#include<netinet/in.h>
void main()
{
int sid;
char s[10]={},s1[10]={};
struct sockaddr_in ssock,csock;
sid=socket(AF_INET,SOCK_STREAM,0);
ssock.sin_family=AF_INET;
ssock.sin_addr.s_addr=inet_addr("127.0.0.1");
ssock.sin_port=htons(9734);
connect(sid,(struct sockaddr *)&ssock,sizeof(ssock));
while(1)
{
printf(" Enter the string:");
scanf("%s",s);
write(sid,(void*)s,strlen(s));
if(strlen(s)==0)
break;
sleep(1);
read(sid,(void*)s1,sizeof(s1));
printf(" The received string is:%s ",s1);
}
close(sid);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.