Task #1. First (1), create the Server (name it myServer1.c) program to accept on
ID: 3817786 • Letter: T
Question
Task #1.
First (1), create the Server (name it myServer1.c) program to accept one argument which is the port number of the server to bind and listen to. Second (2), create the client program (name it myClient1.c) to accept two arguments (IP address and port number of the Server).
Task #2.
Copy the Task#1 programs and name the server "myServe2.c" and the client "myClient2.c" for Task #2.
When a client connects to the server, the server will create a thread to serve this client (for each command sent from the client to be processed in the server, and its result sent back to the client).
Notes:
A client will send a simple command via socket program to a server listening. The server will receive a command from the client, use a thread to handle this command to be processed, and then echo back its result of the command to the client. You need to set up a pipe for each thread to process the command to output its result through pipe, then to socket (back to the client).
For example, try first with ls command. That is, ls command is sent from the client, the server-thread receives it and process the ls command in the server, and then pipe its result to the socket so that the result is then sent back to the server.
Task #3.
Copy the Task#2 programs and name the server "myServe3.c" and the client "myClient3.c" for Task #3.
Modify the server so that the result of the command (e.g., ls or ls | sort) processed by each thread will be kept (logged) into a log file (file name: myLog.txt). That is, the result of each command is echoed back to the client as well as to be logged in the server's log file (to append the log to the log file).
Notes:
You may need to a mutex to lock the log file if you have more than one thread trying to access the log file exclusively.
You should name the source programs as follows: the name of the server program is: myServer2.c, and the client program is: myClient2.c.
Explanation / Answer
//Here i have given the client and server code in c.
//client .c
//client side program
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
void error(char *msg)
{
perror(msg);
exit(0);
}
int main(int argc, char *argv[])
{
int sockcon, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port ", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockcon = socket(AF_INET, SOCK_STREAM, 0);
printf("connecting to server..... ");
if (sockcon < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host ");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockcon,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
printf("Please enter the message: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockcon,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockcon,buffer,255);
if (n < 0)
error("ERROR reading from socket");
printf("%s ",buffer);
return 0;
}
//server.c
include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
void error(char *msg)
{
perror(msg);
exit(1);
}
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;
FILE *fptr;
fptr = fopen("mylog.txt", "w");
if (argc < 2) {
fprintf(stderr,"ERROR, no port provided ");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
printf("finding connection....");
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
bzero(buffer,256);
n = read(newsockfd,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the message: %s ",buffer);
fprintf(fptr,"client:%s", buffer);
n = write(newsockfd,"I got your message",18);
fprintf(fptr,"server:%s", buffer);
if (n < 0) error("ERROR writing to socket");
fclose(fptr);
return 0;
}
output:
client.c
>>client Client_side 1010
connecting to server....
please enter your message
hi hello
server.c
>>server 1010
finding connection....
Here is the message : hi hello
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.