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

Write two C programs one as a client and the other is a server: Do not use the \

ID: 3707732 • Letter: W

Question

Write two C programs one as a client and the other is a server: Do not use the "csapp" or's files. You must use "read" and "write" functions for sockets communication. Write a C client program that takes the IP address of a server and it port value as command ine arguments. If no command line arguments were given, ask the user to enter those two alues. Then, the program connects to the server and display a message: Connected to server xx.xx.xx.xx port xxx at time hh:mm:ss", where hh, mm, ss represent he current time in hours, minutes and seconds. The program asks the user to enter a ommand "Enter a command:" he user may enter any of the three following commands: 11 P a g

Explanation / Answer

Source Code:-
-----------------------
Client Process(Socket Client):-
--------------------------
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char *argv)
{
int clientSocket;
char IpAddress[100],Portno[100];
char response_buffer[100];
char request_message[]="Hi please Accept";
char buffer[11], date[11],time[9], *token;
struct sockaddr_in serverAddr;
socklen_t addr_size;
time_t timer;
struct tm* tm_info;
time(&timer);
if(argc<2)
{
    printf(" Please Pass IP address and Port Number as a Parameter");
    exit(0);
}
IpAddress=argv[0];    //Assigning first Arguement as a IPaddress.
Portno=atoi(argv[1]);        //Assigning Second Arguement as a Port Number.
//create The Socket Connection
if(!(clientSocket = socket(PF_INET, SOCK_STREAM, 1)))
{
     printf("Failed to create a Scoket: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(Portno);
serverAddr.sin_addr.s_addr = inet_addr(IpAddress);
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
addr_size = sizeof serverAddr;
//Connecting with IPaddress
if(connect(clientSocket, (struct sockaddr *) &serverAddr, addr_size)<0)
{
   printf("Connect Error: ");
}
tm_info = localtime(&timer);
strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
strncpy(date,buffer,10);
token=strtok(buffer," ");
while (token != NULL)
{
        strcpy(time,token);
        token = strtok(NULL, " ");
}
printf(" Its Connected at %s Time",token);
//Send Request to The Server
send(clientSocket,request_message,sizeof(request_message),0);
printf("Request Successfully sent to the server: ");
   //Receive Response from the Server
recv(clientSocket, response_buffer, sizeof(response_buffer), 0);
printf("Received Response from the Server is: %s ",response_buffer);

return 0;
}


Server Process:-
------------------------------
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
int main(int argc,char *argv)
{
   
int Server_Socket, newSocket;
char response_buffer[500];
char request_buffer[1000];
struct sockaddr_in serverAddr;
struct sockaddr_storage serverStorage;
socklen_t addr_size;
if(!(Server_Socket = socket(PF_INET, SOCK_STREAM, 0))<0)
{
      printf("Socket Error: ");
}
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(8888);
serverAddr.sin_addr.s_addr = inet_addr("127.0.0.1");
memset(serverAddr.sin_zero, '', sizeof serverAddr.sin_zero);
//Bind The Ipaddress
bind(Server_Socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
//Listening at port
if(listen(Server_Socket,5)==0)
{
      printf("Listening ");
}
else
{
      printf("Error ");
}
   addr_size = sizeof serverStorage;
//Accept Client Resquest
newSocket = accept(Server_Socket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(response_buffer," Hello Welcome Please wait!: ");
//Receiving Resquest_message from the Client
recv(newSocket, request_buffer, sizeof(request_buffer), 0);
printf(" Received Request from the Client is %s ",request_buffer);
//Sending Response Back to the Client
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
return 0;
}