Note that there are some explanatory texts on larger screens.

plurals
  1. POAddress Already in Use.
    primarykey
    data
    text
    <p>Recently I have been working on some client-side code for sending and receiving messages from a server using threading. The below code behaves strangely when run. Upon inputting a message to send to the server, the code completes the task, albeit with a "socket already in use" error, the server gets it. But every subsequent message I attempt to send to the server is not received immediately, yet it is seemingly all received at once when the client program terminates. </p> <p>(Additionally, I am certain the error is client-side, the strange behavior isn't exhibited if one comments the output function.)</p> <p>How can I fix this error? </p> <p>Client</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;cstdlib&gt; #include &lt;sys/types.h&gt; #include &lt;sys/socket.h&gt; #include &lt;sys/time.h&gt; #include &lt;unistd.h&gt; #include &lt;netdb.h&gt; #include &lt;arpa/inet.h&gt; #include &lt;string&gt; #include &lt;iostream&gt; #include &lt;errno.h&gt; #include &lt;pthread.h&gt; void* input(void* ptr) { int on = 1; bool *input_done = ((struct thread_args*)ptr)-&gt;process_done; struct addrinfo *res = ((struct thread_args*)ptr)-&gt;result; char msg[256]; int sock = socket(res-&gt;ai_family,res-&gt;ai_socktype,res-&gt;ai_protocol); setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&amp;on,sizeof(on)); bind(sock,res-&gt;ai_addr,res-&gt;ai_addrlen); connect(sock,res-&gt;ai_addr,res-&gt;ai_addrlen); cin.getline(msg,256); if (msg[0] == '/') {exit(1);} send(sock,msg,sizeof msg,0); cout &lt;&lt; "You:" &lt;&lt; msg &lt;&lt; endl; *input_done = 1; close(sock); pthread_exit(NULL); } void* output(void* ptr) { int on = 1; bool *output_done = ((struct thread_args*)ptr)-&gt;process_done; struct addrinfo *res = ((struct thread_args*)ptr)-&gt;result; char msg[256]; int sock = socket(res-&gt;ai_family,res-&gt;ai_socktype,res-&gt;ai_protocol); bind(sock,res-&gt;ai_addr,res-&gt;ai_addrlen); connect(sock,res-&gt;ai_addr,res-&gt;ai_addrlen); recv(sock,msg,sizeof msg,0); cout &lt;&lt; "Recieved:" &lt;&lt; msg; *output_done = 1; close(sock); pthread_exit(NULL); } void io_client() { //thread function variables pthread_t t1,t2; bool input_done = 1, output_done = 1; //socket setup variables struct addrinfo hints, *res; memset(&amp;hints,0,sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; getaddrinfo("localhost","8080",&amp;hints,&amp;res); //setting up structures to pass data to threaded functions struct thread_args i_args, o_args; i_args.result = res; i_args.process_done = &amp;input_done; o_args.result = res; o_args.process_done = &amp;output_done; while(1) { if (output_done) { pthread_create(&amp;t2,NULL,output,&amp;o_args); output_done = 0; } if (input_done) { pthread_create(&amp;t1,NULL,input,&amp;i_args); input_done = 0; } } } int main() { io_client(); } </code></pre> <p>Server</p> <pre><code>void server() { struct addrinfo hints, *res; int sock=-1, newsock=-1; int length, on=1; char **address_list; int entries = 0; //fd_set read_fd; //struct timeval timeout; char buffer[100]; memset(&amp;hints,0,sizeof hints); res = NULL; memset(&amp;res,0,sizeof res); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; getaddrinfo("localhost","8080",&amp;hints,&amp;res); sock = socket(res-&gt;ai_family,res-&gt;ai_socktype,res-&gt;ai_protocol); setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,(char *)&amp;on,sizeof(on)); bind(sock,res-&gt;ai_addr,res-&gt;ai_addrlen); listen(sock,10); while(1) { struct sockaddr_storage addr; char ipstr[INET6_ADDRSTRLEN]; socklen_t len; len = sizeof addr; newsock = accept(sock,NULL,NULL); getpeername(newsock,(struct sockaddr*)&amp;addr,&amp;len); struct sockaddr_in *s = (struct sockaddr_in*)&amp;addr; inet_ntop(AF_INET,&amp;s-&gt;sin_addr,ipstr,sizeof ipstr); length = 100; setsockopt(newsock,SOL_SOCKET,SO_RCVLOWAT, (char*)&amp;length,sizeof length); recv(newsock,buffer,sizeof buffer,0); cout &lt;&lt; buffer &lt;&lt; endl; } if (newsock != -1) { close(newsock); } if (sock != -1) { close(sock); } } int main() { server(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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