Note that there are some explanatory texts on larger screens.

plurals
  1. POsocket programming in client
    text
    copied!<p>I wrote this program in C++ and on Linux platform.</p> <p>I wrote a client and server socket program.</p> <p>In that client program, I wrote socket function and immediately after that I am doing some other actions not at all depending on socket (I wrote 2 for loops for some other logic).</p> <p>After that I prepared the structures required for the socket and I wrote the connect function...in that I am getting error as unable to connect because connect is returning -1..</p> <p>But for the same program, if I write that for loop's logic above the socket function and immediately after that structures, and connect function, then it is working fine..</p> <p>What might be the reason I am not able to get? Please help me in this aspect. Here is my code</p> <hr> <p>here index1 and index 2 are simple integer variables..The configstring is a char array contains 127.0.0.1:7005(address and port number)...address and port are char array variables to store address and port number..</p> <pre><code>struct sockaddr_in s1; for(index1=0;configstring[index1]!=':';index1++) { address[index1] = configstring[index1]; } address[index1++]='\0'; for(index2=0;configstring[index1]!='\0';index1++,index2++) { port[index2] = configstring[index1]; } port[index2++]='\0'; int port_num = changeto_int(port); if((sock_fd = socket(AF_INET,SOCK_STREAM,0)) == -1) { printf("unable to create a socket\n"); return 0; } s1.sin_family=AF_INET; s1.sin_port=htons(port_num); s1.sin_addr.s_addr=inet_addr(address); memset(s1.sin_zero, '\0', sizeof s1.sin_zero); int errno; if(connect(sock_fd,(struct sockaddr *)&amp;s1,sizeof(s1)) == -1) { printf("error:unable to connect\n"); printf("Error in connect(): %s\n", strerror( errno)); return -1; } </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