Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you are trying to have your client bind() to the same port as the server. That's not necessary. And worse, you are trying to bind to to the IP address of the server - which is also a bigger problem. In general, for client sockets that are to call the connect() function, you should just have your socket bind to port 0 and IP 0, thus letting the OS pick a randomly available port for you and enabling use the right local IP address and adapter for the connection. You can call getsockname() to discover what port the OS picked for you after you call connect.</p> <p>And if you let the OS pick the client port for you, you won't need that SO_REUSESADDR call. Although, your server code could call it for cases where it needs to restart after shutting down with connections still pending to close.</p> <p>Also. you aren't checking the return value of any of your socket calls. That's probably why you are getting some mysterious results. The call to bind() is more likely failing because you are specifying the server IP, but connect() is succeeding because it will auto-bind the socket if it hasn't already.</p> <p>Here's a cleaned up version of you input() function. Converting your output() function is an exercise left up to the reader. If you follow my example, you'll be in good shape.</p> <pre><code>void* input(void* ptr) { int on = 1; bool *input_done = ((struct thread_args*)ptr)-&gt;process_done; int ret; int success = true; struct sockaddr_in addrLocal = {}; struct addrinfo *res = ((struct thread_args*)ptr)-&gt;result; char msg[256]; int sock = socket(AF_INET, SOCK_STREAM, 0); success = (sock != -1); if (success) { addrLocal.sin_family = AF_INET; addrLocal.sin_port = INADDR_ANY; // INADDR_ANY == 0 --&gt; pick a random port for me addrLocal.sin_addr.s_addr = INADDR_ANY; // INADDR_ANY == 0 --&gt; use all appropriate network ret = bind(sock,(sockaddr*)&amp;addrLocal,sizeof(addrLocal)); if (ret == -1) perror("bind: "); success = (ret != -1); } if (success) { ret = connect(sock,res-&gt;ai_addr,res-&gt;ai_addrlen); if (ret == -1) perror("connect: "); success = (ret != -1); } if (success) { cin.getline(msg,256); if (msg[0] == '/') {exit(1);} ret = send(sock,msg,sizeof msg,0); if (ret == -1) perror("send: "); success = (ret != -1); } if (success) { cout &lt;&lt; "You:" &lt;&lt; msg &lt;&lt; endl; *input_done = 1; } if (sock != -1) { close(sock); sock = -1; } return NULL; } </code></pre>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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