C++ programming Write a pair of client and server programs in that allow the cli
ID: 3580314 • Letter: C
Question
C++ programming
Write a pair of client and server programs in that allow the client to download a file from the server.
First, the user on the server's side starts the server as follows.
./fileserver 6018
, where 6018 is the port number where the server will be waiting for connection.
(Use your own unique port number (1024-49151) to avoid conflict with another user on the same computer.)
Then, the user on the client side starts the client as follows.
./fileclient remoteservername remotefilename 6018
, where remoteservername is the domain name of the remote server's computer, remotefilename is the name of the file on the server's side, and 6018 is the port number where the server is waiting.
The client sends the remotefilename to the server, which, then, opens the file, and sends its contents back to the client. The client should create a file with the same name based on the contents received from the server. In order to avoid confusion, execute the client and the server in separate directories.
You may assume that no error occurs in the client server interaction.
Submit fileclient.c and fileserver.c.
Explanation / Answer
Socket Server Example
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
int main(int argc, char *argv[])
{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr;
char sendBuffer[1025];
time_t ticks;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
memset(&serv_addr, '0', sizeof(serv_addr));
memset(sendBuffer, '0', sizeof(sendBuffer));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuffer, sizeof(sendBuffer), "%.24s ", ctime(&ticks));
write(connfd, sendBuffer, strlen(sendBuffer));
close(connfd);
sleep(1);
}
}
Socket Client Example
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char *argv[])
{
int sockfd = 0, n = 0;
char recieveBuffer[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf(" Usage: %s <ip of server> ",argv[0]);
return 1;
}
memset(recieveBuffer, '0',sizeof(recieveBuffer));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf(" Error : Could not create socket ");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf(" inet_pton error occured ");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf(" Error : Connect Failed ");
return 1;
}
while ( (n = read(sockfd, recieveBuffer, sizeof(recieveBuffer)-1)) > 0)
{
recieveBuffer[n] = 0;
if(fputs(recieveBuffer, stdout) == EOF)
{
printf(" Error : Fputs error ");
}
}
if(n < 0)
{
printf(" Read error ");
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.