Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does the program wait for incoming connections once it goes past listen()
    primarykey
    data
    text
    <p>I am learning socket programming in c and trying to understand what exactly is <code>listen()</code> doing in the code. I understand it defines the maximum number of connection that can be queued for <code>accept()</code> but other than that I can't seem to understand what else is it doing.</p> <p>According to the text I'm learning from, </p> <blockquote> <p>listen() waits for incoming connections. Accept() gets the pending connection on the port you are listen()<em>ing</em>.</p> </blockquote> <p>Looking at the flow of execution, I see that <code>listen()</code> is, in fact, called only once and after that only accept() is handling whatever new connection that comes in. Then, how does the server listen the future requests if listen() is never called once we get inside the accept loop.?</p> <pre><code> +----------+ |socket() | +----------+ +----------+ |bind() | +----------+ +----------+ |listen() | +----------+ +----------+ |while(1) | |----------| |accept() | | | | | +----------+ </code></pre> <p>Here's the code I am working on:</p> <pre><code>/*Server*/ #include &lt;stdio.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;stdlib.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;string.h&gt; #include &lt;unistd.h&gt; int main() { int sockfd, newsock; char buffer[200]; memset(buffer, 0, sizeof(buffer)); /*Creating a new socket*/ if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) &lt; 0) { perror("Socket error"); exit(1); } /*Creating socket address structure*/ struct sockaddr_in server_addr, client_address; server_addr.sin_family = AF_INET; server_addr.sin_port = htons(7200); server_addr.sin_addr.s_addr = inet_addr("192.168.1.2"); bzero(&amp;(server_addr.sin_zero),8); /*Binding*/ if(bind(sockfd, (struct sockaddr *)&amp;server_addr, sizeof(server_addr)) &lt; 0) { perror("Bind error"); exit(1); } /*Listening for new connection; maximum 5*/ if(listen(sockfd, 5) &lt; 0) { perror("Listen error"); } while(1) { socklen_t len = sizeof(client_address); if((newsock = accept(sockfd, (struct sockaddr *)&amp;client_address, &amp;len)) &lt; 0) { perror("Accept error"); exit(1); } int i; if((i = read(newsock, buffer, sizeof(buffer))) &lt; 0) { perror("Receive error"); exit(1); } if(i == 0) { printf("Socket closed remotely\n"); } if(i &gt; 0) { printf("received %d bytes\n",i); printf("data: %s",buffer); } close(newsock); } close(sockfd); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload