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

Write a C server program that takes the port value to listen to as command line

ID: 3707858 • Letter: W

Question

Write a C server program that takes the port value to listen to as command line arguments. If no command line arguments were given, ask the user to enter the port value. Then, the program accepts a connection from the clients and writes the message: "Connected to client xx.xx.xx.xx port xxx at time hh:mm:ss" When a command is read from the client it should be displayed on the server as well. When the client exits display the message: “Client xx.xx.xx.xx port xxx disconnected at time hh:mm:ss". The program should handle all system errors or user mistakes with suitable and clear messages. You must use "getnameinfo" and "getaddrinfo" functions. b-

Explanation / Answer

SocketClient.c:-
---------------------

#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include<time.h>
int main(int argc,char *argv[])
{
int clientSocket;
char *IpAddress;
int Portno;
char response_buffer[100];
char request_message[]="Hi please Accept";
char buf[100];
time_t curtime;
struct tm *loc_time;
struct sockaddr_in serverAddr;
socklen_t addr_size;
if(argc<2)
{
      printf(" Please Pass IP address and Port Number as a Parameter");
      exit(0);
}
IpAddress=argv[1]; //Assigning first Arguement as a IPaddress.
printf(" The Ip address is %s ",IpAddress);   
Portno=atoi(argv[2]);    //Assigning Second Arguement as a Port Number.
printf(" The Port Number is %d ",Portno);
    //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("127.0.0.1");
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: ");
   exit(0);
}
curtime = time (NULL);
loc_time = localtime (&curtime);
strftime (buf, 140, "Time is %I:%M %p. ", loc_time);
fputs (buf, stdout);
   //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;
}


SocketServer.c:-
------------------------------

#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(Server_Socket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));
if(listen(Server_Socket,5)==0)
{
      printf("Listening ");
}
else
{
      printf("Error ");
}
   addr_size = sizeof serverStorage;
newSocket = accept(Server_Socket, (struct sockaddr *) &serverStorage, &addr_size);
strcpy(response_buffer," Hello Welcome Please wait!: ");
recv(newSocket, request_buffer, sizeof(request_buffer), 0);
printf(" Received Request from the Client is %s ",request_buffer);
send(newSocket,response_buffer,sizeof(response_buffer),0);
printf(" Response Sent to the client Successfully: ");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote