Question: PLEASE HELP ME IN UNDERSTANDING THE CODE BASICALLY WHAT IS HAPPENING IN THE CODE ABOUT VARIABLES LIBRARIES DEFINE FUNCTIONS AND EVERYTHING IN THE CODE STEP
PLEASE HELP ME IN UNDERSTANDING THE CODE BASICALLY WHAT IS HAPPENING IN THE CODE ABOUT VARIABLES LIBRARIES DEFINE FUNCTIONS AND EVERYTHING IN THE CODE STEP BY STEP
#include
void func(int connfd) { char buff[MAX]; int n; for (;;) { bzero(buff, MAX); read(connfd, buff, sizeof(buff)); printf("From client: %s\t To client : ", buff); bzero(buff, MAX); n = 0; while ((buff[n++] = getchar()) != ' ') ; write(connfd, buff, sizeof(buff)); if (strncmp("exit", buff, 4) == 0) { printf("Server Exit... "); break; } } } int main() { int sockfd, connfd, len; struct sockaddr_in servaddr, cli; // socket create and verification sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { printf("socket creation failed... "); exit(0); } else printf("Socket successfully created.. "); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(PORT); if ((bind(sockfd, (SA*)&servaddr, sizeof(servaddr))) != 0) { printf("socket bind failed... "); exit(0); } else printf("Socket successfully binded.. "); if ((listen(sockfd, 5)) != 0) { printf("Listen failed... "); exit(0); } else printf("Server listening.. "); len = sizeof(cli); connfd = accept(sockfd, (SA*)&cli, &len); if (connfd < 0) { printf("server accept failed... "); exit(0); } else printf("server accept the client... "); func(connfd); close(sockfd); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
