Note that there are some explanatory texts on larger screens.

plurals
  1. POSocket programming in C not working on two distinct PCs
    text
    copied!<p>I have written a <strong>simple socket programming application in C</strong>.This code is working on the loopback address. Also this program works well if the two computers(one server one client) are in same network(I tried it on a college LAN network) But when I tried to run the same code on two completely different computers(<strong>In two different networks</strong>)Then it displays error in "connect" method of client.Client is unable to connect to server.Please help me out guys.I tried almost everything but nothing helped so please help me.Please help me ASAP I am struck on this assignment for almost a month now. Thanks in advanced!</p> <p>here is the code: for Client.c</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;fcntl.h&gt; int chat(); int main(){ chat(); } int chat() { int clientid, n = 0; char msgsend[512], msgrecv[512]; struct sockaddr_in client; memset(msgrecv, '0', sizeof(msgrecv)); clientid = socket(AF_INET, SOCK_STREAM, 0); if(clientid == -1){ printf("Could not create a socket!\n"); return -1; } client.sin_family = AF_INET; client.sin_addr.s_addr = /*INADDR_ANY;*/inet_addr("10.200.56.187"); client.sin_port = htons(5001); printf(" Trying to connect..."); if(connect(clientid, (struct sockaddr*)&amp;client, sizeof(client)) &lt; 0){ printf("Error:Connection failed\n"); return -1; } printf("Connected"); while(1){ printf("please enter your reply\n"); gets(msgsend); send(clientid, msgsend, sizeof(msgsend) - 1, 0); n = recv(clientid, msgrecv, sizeof(msgrecv) - 1, 0); if(n &lt; 0){ printf("Read error\n"); } printf("\n"); printf("server says:"); msgrecv[n] = 0; fputs(msgrecv, stdout); printf("\n"); } return 0; } And here is the code: for server.c #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;fcntl.h&gt; int chat(); int main(){ chat(); } int chat(){ int serverid, clientid, n = 0, len; char msgsend[512], msgrecv[512]; struct sockaddr_in server,client; serverid = socket(AF_INET, SOCK_STREAM, 0); memset(&amp;server, '0', sizeof(server)); memset(msgrecv, '0', sizeof(msgrecv)); server.sin_family = AF_INET; server.sin_addr.s_addr = htonl(INADDR_ANY); server.sin_port = htons(5001); bind(serverid, (struct sockaddr*)&amp;server, sizeof(server)); if( listen(serverid, 10) == -1){ printf("ERROR"); return -1; } len = sizeof(client); clientid = accept(serverid, (struct sockaddr*)&amp;client, &amp;len); while(1){ n = recv(clientid, msgrecv, sizeof(msgrecv) - 1, 0); if(n &lt; 0){ printf("Error while reading...\n"); return -1; } printf("client says:"); msgrecv[n] = 0; fputs(msgrecv, stdout); printf("\n"); printf("enter your reply\n"); gets(msgsend); send(clientid, msgsend, sizeof(msgsend) - 1, 0); printf("\n"); } return 0; } </code></pre>
 

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