Modify the client.c and server.c in this way: After the client receives “This se
ID: 3888528 • Letter: M
Question
Modify the client.c and server.c in this way:
After the client receives “This server has been contacted 1 times”, instead of exit, the client sends back “This client has been contacted 1 times” to the server. The server then sends back “This server has been contacted 2 times”, the client sends back “This client has been contacted 2 times” to the server, and so on. That is, both the client and the server do not exit. They alternatively send messages to each other.
/////////////////////////////////////////////////////////////////////////////
client:
/* Create a socket. */
sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "socket creation failed ");
exit(1);
}
/* Connect the socket to the specified server. */
if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr,"connect failed ");
exit(1);
}
/* Repeatedly read data from socket and write to user.s screen. */
n = recv(sd, buf, sizeof(buf), 0);
while (n > 0) {
write(1,buf,n);
printf("n: %d ", n);
n = recv(sd, buf, sizeof(buf), 0);
}
printf("n: %d ", n);
/* Close the socket. */
closesocket(sd);
/* Terminate the client program gracefully. */
exit(0);
}
////////////////////////////////////////////////////////////////////////////////////////
server:
/* Create a socket */
sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "socket creation failed ");
exit(1);
}
/* Bind a local address to the socket */
if (bind(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
fprintf(stderr,"bind failed ");
exit(1);
}
/* Specify size of request queue */
if (listen(sd, QLEN) < 0) {
fprintf(stderr,"listen failed ");
exit(1);
}
/* Main server loop - accept and handle requests */
while (1) {
alen = sizeof(cad);
if ( (sd2=accept(sd, (struct sockaddr *)&cad, &alen)) < 0) {
fprintf(stderr, "accept failed ");
exit(1);
}
visits++;
sprintf(buf,"This server has been contacted %d time%s ",
visits,visits==1?".":"s.");
send(sd2,buf,strlen(buf),0);
closesocket(sd2);
}
Explanation / Answer
I have written below given code:
Client.c:
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/wait.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
struct sockaddr_in address;
int sock = 0, valread;
struct sockaddr_in serv_addr;
char hello[1024];
char buffer[1024] = {0};
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf(" Socket creation error ");
return -1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
// Convert IPv4 and IPv6 addresses from text to binary form
if(inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr)<=0)
{
printf(" Invalid address/ Address not supported ");
return -1;
}
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf(" Connection Failed ");
return -1;
}
int counter = 1;
while(1){
sprintf(hello, "This client has been contacted %d times", counter);
sleep(1);
send(sock , hello , strlen(hello) , 0 );
valread = read( sock , buffer, 1024);
printf("%s ",buffer );
counter++;
}
return 0;
}
Server.c:
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/wait.h>
#define PORT 8080
int main(int argc, char const *argv[])
{
int server_fd, new_socket, valread;
struct sockaddr_in address;
int opt = 1;
int addrlen = sizeof(address);
char buffer[1024] = {0};
char hello[1024];
// Creating socket file descriptor
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0)
{
perror("socket failed");
exit(EXIT_FAILURE);
}
// Forcefully attaching socket to the port 8080
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT,
&opt, sizeof(opt)))
{
perror("setsockopt");
exit(EXIT_FAILURE);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( PORT );
// Forcefully attaching socket to the port 8080
if (bind(server_fd, (struct sockaddr *)&address,
sizeof(address))<0)
{
perror("bind failed");
exit(EXIT_FAILURE);
}
if (listen(server_fd, 3) < 0)
{
perror("listen");
exit(EXIT_FAILURE);
}
if ((new_socket = accept(server_fd, (struct sockaddr *)&address,
(socklen_t*)&addrlen))<0)
{
perror("accept");
exit(EXIT_FAILURE);
}
int counter = 1;
while(1){
sprintf(hello, "This server has been contacted %d times", counter);
sleep(1);
valread = read( new_socket , buffer, 1024);
printf("%s ",buffer );
send(new_socket , hello , strlen(hello) , 0 );
counter++;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.