Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple network-programming program - not working
    primarykey
    data
    text
    <p>I just began learning network programming. My first program is very simple: once connected, the server program displays what the client program sends (characters). Everything works fine until they get connected. At first, I did the sending loop using putc:</p> <pre><code>while((c = getc(stdin)) != '\n') putc(c, (FILE *) connection); </code></pre> <p>and the receiving one with getc:</p> <pre><code>while((c = getc((FILE *) connection)) != '\n') putc(c, stdout); </code></pre> <p>Once connected, I get a segmentation fault. Then I tried using send:</p> <pre><code>while(1) { memset(str, null, strlen(memset)); while(((c = getc(stdin)) != '\n') &amp;&amp; sizeof(str) &lt;= 100) { strcat(str, (char *) c); // cast to char * is to make gcc happy } send(connection, (void *) str, sizeof(str), (int) NULL); } </code></pre> <p>and recv:</p> <pre><code>while((stat = recv(connection,str,100,0)) != 0) { printf("\nReceived %d bytes of data from %s: %s", stat, client, str); } </code></pre> <p>Now recv() keeps returning -1 and errno is set to 107. What does this mean? Where am I doing wrong? By the way I'm using Linux with gcc.</p> <p>Thanks a lot for your help! :)</p> <hr> <p><strong>EDIT:</strong> Even if I used getc() and putc(), I get connection with socket(), then I connect() (client). The server, after obtaining connection (with socket()) bind()s, then listen()s and accept()s. Sorry if I ometted.</p> <hr> <p>Here's the server code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;errno.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;netdb.h&gt; void main(int argc, char *argv[]) { struct addrinfo hint; struct addrinfo *servinfo, *count; struct sockaddr_storage conn_addr; struct sockaddr host; int connessione; int stat; int conn_sock; int conn_size; int success; int c; char t[INET_ADDRSTRLEN]; char str[100]; char *client; if(argc != 1 &amp;&amp; strcmp(argv[1], "aiuto") == 0) { printf("%s(porta)\n",argv[0]); printf("porta: specifica su quale porta installarsi\n"); exit(-1); } memset(&amp;hint,0,sizeof hint); hint.ai_family = AF_INET; hint.ai_socktype = SOCK_STREAM; hint.ai_flags = AI_PASSIVE; if((stat = getaddrinfo(NULL, argv[1], &amp;hint, &amp;servinfo)) != 0) { printf("\n[FATAL]: getaddrinfo: %s",gai_strerror(stat)); exit(-1); } success = 0; for(count = servinfo; count != NULL; count = count-&gt;ai_next) { if((connessione = socket(count-&gt;ai_family, count-&gt;ai_socktype, count-&gt;ai_protocol)) &lt;= 0) continue; if((stat = bind(connessione, count-&gt;ai_addr, count-&gt;ai_addrlen)) == 0) { success = 1; break; } else { continue; } } if(success == 0) { printf("\n[FATAL]: Unable to bind()!\n"); exit(-1); } else { printf("\n[DEBUG]: %s: listening...", argv[0]); } servinfo = count; freeaddrinfo(count); if(listen(connessione, 20) == -1) { printf("\n[FATAL]: listen: %s (%d)\n", gai_strerror(errno), errno); close(connessione); exit(-1); } conn_size = sizeof(conn_addr); if(accept(connessione, (struct sockaddr *) &amp;conn_addr, &amp;conn_size) == -1) { printf("\n[FATAL]: accept: %s", gai_strerror(errno)); close(connessione); exit(-1); } client = (char *) malloc(INET_ADDRSTRLEN * sizeof(char)); client = inet_ntop(servinfo-&gt;ai_family, &amp;conn_addr, t, INET_ADDRSTRLEN); printf("\n[DEBUG]: %s: connection accepted: %s", argv[0], client); while((stat = recv(connessione,str,100,0)) != 0) { printf("\nReceived %d bytes of data from %s: %s", stat,client, str); } printf("\n[DEBUG]: connection closed by %s\n",client); } </code></pre> <p>And the client's code:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; #include &lt;stdlib.h&gt; #include &lt;assert.h&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;errno.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;netdb.h&gt; void main(int argc, char *argv[]) { struct addrinfo hint; struct addrinfo *servinfo, *count; struct sockaddr_storage conn_addr; struct sockaddr host; int connessione; int stat; int conn_sock; int conn_size; int success; int c; char t[INET_ADDRSTRLEN]; char *indirizzo; char str[100]; memset(&amp;hint,0,sizeof hint); host.sa_family = AF_INET; if(inet_pton(host.sa_family, argv[1], (void *) host.sa_data) == -1) { printf("\n[FATAL]: pton: %s (%d)\n", gai_strerror(errno), errno); exit(-1); } hint.ai_family = AF_INET; hint.ai_socktype = SOCK_STREAM; hint.ai_flags = AI_PASSIVE; hint.ai_addr = &amp;host; if((stat = getaddrinfo(argv[1], argv[2], &amp;hint, &amp;servinfo)) != 0) { printf("[FATAL]: getaddrinfo: %s\n",gai_strerror(stat)); exit(-1); } success = 0; for(count = servinfo; count != NULL; count = count-&gt;ai_next) { if((connessione = socket(count-&gt;ai_family, count-&gt;ai_socktype, count-&gt;ai_protocol)) == -1) continue; if(connect(connessione, count-&gt;ai_addr, count-&gt;ai_addrlen) != -1) { success = 1; break; } } if(success == 0) { printf("\n[FATAL]: impossibile trovare l'host specificato\n"); exit(-1); } servinfo = count; freeaddrinfo(count); indirizzo = (char *) malloc(INET_ADDRSTRLEN * sizeof(char)); indirizzo = inet_ntop(servinfo-&gt;ai_family, &amp;conn_addr, t, INET_ADDRSTRLEN); printf("\n[DEBUG]: %s: connesso a: %s\n",argv[0], indirizzo); while(1) { strcpy(str,"buffer for data to send"); while(((c = getc(stdin)) != '\n') &amp;&amp; sizeof(str) &lt;= 100) { strcat(str, (char *) c); } send(connessione, (void *) str, sizeof(str), (int) NULL); } } </code></pre> <hr> <p><strong>At the end...</strong></p> <p>It works perfectly! Thanks everyone. :)</p>
    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.
 

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