Comment the below code please. Server: int main(int argc, char *argv[]){ int soc
ID: 3587123 • Letter: C
Question
Comment the below code please.
Server:
int main(int argc, char *argv[]){
int sockfd, newsockfd, portnum, clilen;
char buffer[256], hostname[256];
pid_t p_id;
struct sockaddr_in serv_addr, cli_addr;
int n, pid;
if(argc < 2){
fprintf(stderr, "ERROR, NO PORT PROVIDED! ");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);//socket is made
if(sockfd < 0){
fprintf(stderr,"ERROR opening socket!!");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
portnum = atoi(argv[1]);//port num
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(portnum);
if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
fprintf(stderr,"ERROR on binding");
exit(1);
}
if( listen(sockfd, 5) < 0){
printf("ERROR ON LISTEN");
exit(1);
}
// accept
clilen = sizeof(cli_addr);
do{
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if(newsockfd<0){
fprintf(stderr,"ERROR on accept ");
exit(1);
}
pid=fork();
if(pid==0){
bzero(buffer, 256);
n= read(newsockfd, buffer, 255);
if(n<0){//message from client
fprintf(stderr,"ERROR Reading from socket ");
exit(1);
}
strcpy(hostname, buffer);
printf("Here is the hostname : %s ", hostname);
//variables used for acsessing webserver?
int sockwb, wbport, x;
struct sockaddr_in webser_addr;
struct hostent *wbhost;
char webbuf[510];//sending to webserver
wbport =80;//port used to access web server
sockwb = socket(AF_INET, SOCK_STREAM, 0);
if(sockwb <0){
printf("error opeing websocket ");
exit(1);
}
wbhost= gethostbyname(hostname);
printf("%s", wbhost->h_name);
if(sockwb == -1){
printf("NO SUCH web HOST ");
exit(1);
}
bzero((char*) &webser_addr, sizeof(webser_addr));
webser_addr.sin_family = AF_INET;
bcopy((char *)wbhost->h_addr,
(char *)&webser_addr.sin_addr.s_addr,
wbhost->h_length);
webser_addr.sin_port = htons(wbport);
if(connect(sockwb,(struct sockaddr *)&webser_addr,sizeof(webser_addr)) < 0){
printf("error on web connecting ");
exit(1);
}
bzero(webbuf,510);
strcpy(webbuf, "GET http://");
strcat(webbuf, hostname);
strcat(webbuf, " HTTP/1.1");
printf("%s ", webbuf);
x=write(sockwb,webbuf,strlen(webbuf));
if(x<0){
printf("error writing to web sock");
exit(1);
}
bzero(webbuf,510);
x=read(sockwb,webbuf,510);
if(n<0){
printf("error reading from web socket");
exit(1);
}
n = write(newsockfd, webbuf,255 );//write back to client
if (n<0){
fprintf(stderr,"ERROR WRITING to socket");
exit(1);
}
printf("%s ", webbuf);
}//end of if pid==0
printf("closing client");
close(newsockfd);//closing client socket
}while(1);
return 0;
}
Client:
int main(int argc, char *argv[]){
int sockfd, portnum, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if(argc < 3){
fprintf(stderr, "usage %s hostname port ", argv[0]);
exit(1);
}
portnum = atoi(argv[2]);
sockfd= socket(AF_INET, SOCK_STREAM, 0);
if(sockfd <0){
perror("ERROR opening Socket");
exit(1);
}
server= gethostbyname(argv[1]);
if(sockfd == NULL){
fprintf(stderr,"ERROR, no such host ");
exit(1);
}
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(portnum);
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) <0){
fprintf(stderr,"ERROR, on connecting");
exit(1);
}
printf("Please enter the Host name: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n=write(sockfd,buffer,strlen(buffer));
if(n<0){
printf("Error writing to socket");
exit(1);
}
bzero(buffer,256);
n=read(sockfd,buffer,255);
if(n<0){
printf("ERROR reading from socket");
exit(1);
}
printf("%s ", buffer);
return 0;
}
Explanation / Answer
Server:
int main(int argc, char *argv[])
{
int sockfd, newsockfd, portnum, clilen;
char buffer[256], hostname[256];
pid_t p_id;
struct sockaddr_in serv_addr, cli_addr; //"sockaddr_in" is name of inbuilt structure used for binding ip and port
int n, pid;
if(argc < 2){
fprintf(stderr, "ERROR, NO PORT PROVIDED! ");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);//socket is made, "socket" is system call use to create socket
//sockfd: socket descriptor, an integer (like a file-handle)
//domain: integer, communication domain e.g., AF_INET (IPv4 protocol)
//SOCK_STREAM for TCP & SOCK_DGRAM: UDP(unreliable, connectionless)
//protocol: Protocol value for Internet Protocol(IP), which is 0.
if(sockfd < 0){
fprintf(stderr,"ERROR opening socket!!");
exit(1);
}
bzero((char *) &serv_addr, sizeof(serv_addr)); //structure name serv_addr equal to 0;
portnum = atoi(argv[1]);//port num
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); // The htonl() function converts the unsigned integer hostlong from host byte order to network byte order.
serv_addr.sin_port = htons(portnum); //The htons() function converts the unsigned short integer hostshort from host byte order to network byte order.
//Bind: After creation of the socket, bind function binds the socket to the address and port number specified in addr(custom data structure). In the example code,
//we bind the server to the localhost, hence we use INADDR_ANY to specify the IP address.
if(bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0){
fprintf(stderr,"ERROR on binding");
exit(1);
}
//Listen: It puts the server socket in a passive mode, where it waits for the client to approach the server to make a connection.
//The backlog, defines the maximum length to which the queue of pending connections for sockfd may grow.
if( listen(sockfd, 5) < 0){
printf("ERROR ON LISTEN");
exit(1);
}
// accept : It extracts the first connection request on the queue of pending connections for the listening socket, sockfd, creates a new connected socket,
//and returns a new file descriptor referring to that socket.It has client addrs as second argument
clilen = sizeof(cli_addr);
do{
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); //
if(newsockfd<0){
fprintf(stderr,"ERROR on accept ");
exit(1);
}
pid=fork(); // fork is a system call used to create child process of the parent from where it is called
if(pid==0){ //if pid==0 ,it means it is child process
bzero(buffer, 256);
n= read(newsockfd, buffer, 255); //read:used to read data in buffer from newsoclfd created in accept system call
if(n<0){//message from client
fprintf(stderr,"ERROR Reading from socket ");
exit(1);
}
strcpy(hostname, buffer);
printf("Here is the hostname : %s ", hostname);
//variables used for acsessing webserver?
int sockwb, wbport, x;
struct sockaddr_in webser_addr;
struct hostent *wbhost;
char webbuf[510];//sending to webserver
wbport =80;//port used to access web server
sockwb = socket(AF_INET, SOCK_STREAM, 0);
if(sockwb <0){
printf("error opeing websocket ");
exit(1);
}
wbhost= gethostbyname(hostname); //Given the name of a host, gethostbyname returns a pointer to the hostent structure containing the host's IP address and other information.
//Refer to <netdb.h> for details on the hostent structure.
printf("%s", wbhost->h_name);
if(sockwb == -1){
printf("NO SUCH web HOST ");
exit(1);
}
bzero((char*) &webser_addr, sizeof(webser_addr));
webser_addr.sin_family = AF_INET;
bcopy((char *)wbhost->h_addr,
(char *)&webser_addr.sin_addr.s_addr,
wbhost->h_length);
webser_addr.sin_port = htons(wbport);
if(connect(sockwb,(struct sockaddr *)&webser_addr,sizeof(webser_addr)) < 0){
printf("error on web connecting ");
exit(1);
}
bzero(webbuf,510);
strcpy(webbuf, "GET http://");
strcat(webbuf, hostname);
strcat(webbuf, " HTTP/1.1");
printf("%s ", webbuf);
x=write(sockwb,webbuf,strlen(webbuf));
if(x<0){
printf("error writing to web sock");
exit(1);
}
bzero(webbuf,510);
x=read(sockwb,webbuf,510);
if(n<0){
printf("error reading from web socket");
exit(1);
}
n = write(newsockfd, webbuf,255 );//write back to client
if (n<0){
fprintf(stderr,"ERROR WRITING to socket");
exit(1);
}
printf("%s ", webbuf);
}//end of if pid==0
printf("closing client");
close(newsockfd);//closing client socket
}while(1);
return 0;
}
Client:
int main(int argc, char *argv[]){
int sockfd, portnum, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
if(argc < 3){
fprintf(stderr, "usage %s hostname port ", argv[0]);
exit(1);
}
portnum = atoi(argv[2]);
sockfd= socket(AF_INET, SOCK_STREAM, 0);
if(sockfd <0){
perror("ERROR opening Socket");
exit(1);
}
server= gethostbyname(argv[1]);
if(sockfd == NULL){
fprintf(stderr,"ERROR, no such host ");
exit(1);
}
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(portnum);
// connect ::The connect() system call connects the socket referred to by the file descriptor sockfd to the address specified by addr.
//Server’s address and port is specified in addr.
if(connect(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr)) <0){
fprintf(stderr,"ERROR, on connecting");
exit(1);
}
printf("Please enter the Host name: ");
bzero(buffer,256);
fgets(buffer,255,stdin);
n=write(sockfd,buffer,strlen(buffer)); //use to write message to server side from client
if(n<0){
printf("Error writing to socket");
exit(1);
}
bzero(buffer,256);
n=read(sockfd,buffer,255);
if(n<0){
printf("ERROR reading from socket");
exit(1);
}
printf("%s ", buffer);
return 0;
}