Question
Explain this code in C language: //CLIENT PROGRAM: int main(int argc, char *argv[]) { struct sockaddr_in echoserver; char buffer[255]; int sock; sock = socket(PF_INET, SOCK_STREAM,
Explain this code in C language:
//CLIENT PROGRAM:
int main(int argc, char *argv[]) {
struct sockaddr_in echoserver;
char buffer[255];
int sock;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = inet_addr("127.0.0.1");
echoserver.sin_port = htons(atoi("6000"));
connect(sock, (struct sockaddr *) &echoserver, sizeof(echoserver));
printf("PLEASE ENTER TEXT: ");
fgets(buffer, 255, stdin);
write(sock, buffer, strlen(buffer) + 1);
printf("PLEASE ENTER TEXT: ");
fgets(buffer, 255, stdin);
write(sock, buffer, strlen(buffer) + 1);
printf("PLEASE ENTER TEXT: ");
fgets(buffer, 255, stdin);
write(sock, buffer, strlen(buffer) + 1);
close(sock);
exit(0);
}
//SERVER PROGRAM:
int main(int argc, char *argv[]) {
int serversock;
struct sockaddr_in echoserver;
unsigned int clientlen;
int clientsock;
struct sockaddr_in echoclient;
char buffer[255];
int result;
serversock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
memset(&echoserver, 0, sizeof(echoserver));
echoserver.sin_family = AF_INET;
echoserver.sin_addr.s_addr = htonl(INADDR_ANY);
echoserver.sin_port = htons(atoi("6000"));
bind(serversock, (struct sockaddr *) &echoserver, sizeof(echoserver));
listen(serversock, 5);
clientlen = sizeof(echoclient);
clientsock = accept(serversock, (struct sockaddr *) &echoclient, &clientlen);
read(clientsock, &buffer[0], 255);
printf("Message from client: %s ", buffer);
read(clientsock, &buffer[0], 255);
printf("Message from client: %s ", buffer);
read(clientsock, &buffer[0], 255);
printf("Message from client: %s ", buffer);
close(clientsock);
close(serversock);
exit(0);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started