Note that there are some explanatory texts on larger screens.

plurals
  1. POlinux c sockets
    primarykey
    data
    text
    <p>good evening, I am a newbie in using linux sockets under the c programming language.</p> <p>for now I'm trying to make a client communicate with a server. basically the thing i'm stuck at is making the server accept a connection from the client and print the numbers after it receives them. here is the source code for the server so far:</p> <pre><code>#include &lt;sys/socket.h&gt; #include &lt;netinet/in.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;unistd.h&gt; #include &lt;errno.h&gt; #include &lt;string.h&gt; #include &lt;sys/types.h&gt; #include &lt;time.h&gt; int main(int argc, char *argv[]) { int listenfd = 0, connfd = 0, n = 0; struct sockaddr_in serv_addr; char sendBuff[1024], recvBuff[1024]; listenfd = socket(AF_INET, SOCK_STREAM, 0); memset(&amp;serv_addr, '0', sizeof(serv_addr)); memset(sendBuff, '0', sizeof(sendBuff)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(5000); bind(listenfd, (struct sockaddr*)&amp;serv_addr, sizeof(serv_addr)); listen(listenfd, 10); while(1) { printf("in first while loop\n"); connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); while( (n = read(listenfd, recvBuff, sizeof(recvBuff)-1)) &gt; 0) { printf("in second while loop\n"); recvBuff[n] = 0; fputs(recvBuff, stdout); } } } </code></pre> <p>I didn't post the client code since I don't think it's relevant. I have made a server which sends something to a client and it worked just fine. but I'm seriously confused by how I'm supposed to make the server accept input from the client. thank you in advance. </p>
    singulars
    1. This table or related slice is empty.
    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.
 

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