Socket Programming in C language Write two socket programs and name one sender a
ID: 3909782 • Letter: S
Question
Socket Programming in C language
Write two socket programs and name one sender and other one receiver
receiver: will open a socket (TCP) on port specified on command line ( typical use a port number greater than 10000 for testing). Establish any connection request and echo all information that it reads from the socket to standard output (terminal) until sender informs that it is end of transmission.
Sender: Will have two command line options, first an IP address and second a port number. Sender will open socket connection on port specified on command line to send information to receiver (Receiver should listen on the same port number). Sender will request user input from standard input (keyboard) and send out the information as soon as user press enter key to the recceiver, program will continue to read user input until it reads a blank line, at this point, it can send all unsent data if any, inform the receiver it has ended it transmission and close the connection with the receiver program
Ex: run
Computer 1: ./receiver 15000
Computer 2: ./sender 192.168.1.1 15000
Explanation / Answer
Sender.c File:-
==========
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
using namespace std;
int main(int argc,char *argv[])
{
int clientSocket;
char *IpAddress;
int Portno;
char response_buffer[100];
char request_buffer[100];
char buf[100];
time_t curtime;
struct tm *loc_time;
struct sockaddr_in serverAddr;
socklen_t addr_size;
if(argc<3)
{
printf(" Please Pass IP address and Port Number and Message 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.
cout<<" The Port Number is: "<<Portno<<endl;
request_buffer=argv[3];
//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;
}
Receiver.c file:-
============
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
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: ");
exit(0);
}
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 ");
exit(0);
}
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;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.