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

Write two C programs using sockets on Linux that implement the following behavio

ID: 3828032 • Letter: W

Question

Write two C programs using sockets on Linux that implement the following behavior: One program is a server, and the other is a client The server: o Receives numbers that are sent from the client o Keeps a running sum of all numbers received so far o Sends a the sum of all numbers received back to the client every time a new number is received The client: o Establishes a connection to the server o Prompts the user to enter a number o Sends the number to the server o Waits for a response back that says what the sum so far is o Prints the response received from the server

Explanation / Answer

I have give the basic code for socket programming.The code i have given is for simple message transfer beetween client and sever . Hope the code will be useful for you.

Client code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h> /* netbd.h is needed for struct hostent =) */

#define PORT 3550   /* Open Port on Remote Host */
#define MAXDATASIZE 100 /* Max number of bytes of data */

int main(int argc, char *argv[])
{
int fd, numbytes;   /* files descriptors */
char buf[MAXDATASIZE]; /* buf will store received text */
  
struct sockaddr_in server; /* server's address information */

if (argc !=2) {   /* this is used because our program will need one argument (IP) */
printf("Usage: %s <IP Address> ",argv[0]);
exit(-1);
}


if ((fd=socket(AF_INET, SOCK_STREAM, 0))==-1){ /* calls socket() */
printf("socket() error ");
exit(-1);
}

server.sin_family = AF_INET;
server.sin_port = htons(PORT); /* htons() is needed again */
server.sin_addr.s_addr =inet_addr(argv[1]);
bzero(&(server.sin_zero),8);

if(connect(fd, (struct sockaddr *)&server,sizeof(struct sockaddr))==-1){ /* calls connect() */
printf("connect() error ");
exit(-1);
}

if ((numbytes=recv(fd,buf,MAXDATASIZE,0)) == -1){ /* calls recv() */
printf("recv() error ");
exit(-1);
}

buf[numbytes]='';

printf("Server Message: %s ",buf); /* it prints server's welcome message =) */

close(fd); /* close fd =) */
  
}

Server code:

#include <stdio.h> /* These are the usual header files */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <stdlib.h>


#define PORT 3550 /* Port that will be opened */
#define BACKLOG 2 /* Number of allowed connections */

main()
{

int fd, fd2; /* file descriptors that will refer to different sockets used in the program*/

struct sockaddr_in server; /* server's address information */
struct sockaddr_in client; /* client's address information */

int sin_size;


if ((fd=socket(AF_INET, SOCK_STREAM, 0)) == -1 ){ /* calls socket() */
printf("socket() error ");
exit(-1);
}

server.sin_family = AF_INET;   
server.sin_port = htons(PORT);
server.sin_addr.s_addr = INADDR_ANY;   
bzero(&(server.sin_zero),8);
  
if(bind(fd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1){ /* calls bind() */
printf("bind() error ");
exit(-1);
}   

if(listen(fd,BACKLOG) == -1){ /* calls listen() */
printf("listen() error ");
exit(-1);
}

while(1){
sin_size=sizeof(struct sockaddr_in);
if ((fd2 = accept(fd,(struct sockaddr *)&client,&sin_size))==-1){ /* calls accept() */
printf("accept() error ");
exit(-1);
}
  
printf("You got a connection from %s ",inet_ntoa(client.sin_addr) ); /* prints client's IP */
  
send(fd2,"Welcome to my server. ",22,0); /* send to the client welcome message */
  
close(fd2); /* close fd2 */
}


}