Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &client_len); // Step 5: Read data from the connection memset(buffer, 0, sizeof(buffer)); int len = read(newsockfd, buffer, 100); printf(Received

image text in transcribed newsockfd = accept(sockfd, (struct sockaddr *)&client_addr, &client_len); // Step 5: Read data from the connection memset(buffer, 0, sizeof(buffer)); int len = read(newsockfd, buffer, 100); printf("Received %d bytes: %s", len, buffer); // Step 6: Close the connection close(newsockfd); close(sockfd); return 0; } Does the program get blocked when invoking listen() until a connection comes? (2) What is the purpose of the accept()? (3) Whydoes the accept() call create a new socket? Why cannot we use the same one that is used in the listen() call? 
#include #include #include #include int main () int sockfd, newsockfd; struct sockaddr_in my_addr, client_addr; char buffer [100]; // Step 1: Create a socket sockfd= socket (AF_INET, SOCK_STREAM, 0); // Step 2: Bind to a port number memset (&my_addr, 0, sizeof (struct sockaddr_in)) my-addr. sin-family = AF_INET; my_addr.sin_port-htons (9090); bind (sockfd, (struct sockaddr ) &my_addr, sizeof (struct sockaddr_in)); // Step 3: Listen for connections listen (sockfd, 5); // Step 4: Accept a connection request int client_lensizeof (client_addr); #include #include #include #include int main () int sockfd, newsockfd; struct sockaddr_in my_addr, client_addr; char buffer [100]; // Step 1: Create a socket sockfd= socket (AF_INET, SOCK_STREAM, 0); // Step 2: Bind to a port number memset (&my_addr, 0, sizeof (struct sockaddr_in)) my-addr. sin-family = AF_INET; my_addr.sin_port-htons (9090); bind (sockfd, (struct sockaddr ) &my_addr, sizeof (struct sockaddr_in)); // Step 3: Listen for connections listen (sockfd, 5); // Step 4: Accept a connection request int client_lensizeof (client_addr)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions