Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Sockets recv() syscall returning -1
    primarykey
    data
    text
    <p>I'm currently having a problem passing messages between a server and client. As far as I know, I am properly following best practices for socket programming outlined by <a href="http://beej.us/guide/bgnet/" rel="nofollow">Beej's Socket Programming Tutorial</a>.</p> <p>When I run the two processes, the recv() syscall returns -1 (an error), rather than the number of bytes received. Also when trying to output the buf, there are a bunch of gobbledygook characters. Which makes sense, because of the error.</p> <p>I'm wondering if someone could steer me in the right direction as to why I am having issues with recv()? The following are relevant code snippets.</p> <p>Server:</p> <pre><code>struct sockaddr_storage their_addr; socklen_t addr_size; int sockfd, newfd, byte_count, status; char buf[512]; struct addrinfo hints, *res; // first, load up address structs with getaddrinfo(): memset(&amp;hints, 0, sizeof hints); hints.ai_family = PF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; // get address info, print stuff if error if((status = getaddrinfo("nunki.usc.edu", "21957", &amp;hints, &amp;res)) !=0){ fprintf(stderr, "getaddrinfo error: %s\n", gai_strerror(status)); exit(1); } // make a socket: if((sockfd = socket(res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol)) == -1){ cout &lt;&lt; "socket fail" &lt;&lt; endl; } // bind the socket to the port bind(sockfd, res-&gt;ai_addr, res-&gt;ai_addrlen); // required output cout &lt;&lt; "Phase1: Login server has TCP port number " &lt;&lt; "21957 " &lt;&lt; "and IP address " &lt;&lt; getIPfromHost("nunki.usc.edu") &lt;&lt; endl; // listen for incoming connections listen(sockfd, 10); cout &lt;&lt; "after listen" &lt;&lt; endl; // halt until receipt addr_size = sizeof(their_addr); newfd = accept(sockfd, (struct sockaddr *)&amp;their_addr, &amp;addr_size); cout &lt;&lt; "after accept" &lt;&lt; endl; // Now that we're connected, we can receive some data byte_count = recv(sockfd, buf, sizeof buf, 0); printf("recv()'d %d bytes of data in buf\n", byte_count); printf("Msg is %s\n", buf); </code></pre> <p>Client:</p> <pre><code>struct addrinfo hints, *res; int sockfd; // first, load up address structs with getaddrinfo(): memset(&amp;hints, 0, sizeof hints); hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; getaddrinfo("nunki.usc.edu", "21957", &amp;hints, &amp;res); // make a socket: if((sockfd = socket(res-&gt;ai_family, res-&gt;ai_socktype, res-&gt;ai_protocol)) == -1){ cout &lt;&lt; "socket fail" &lt;&lt; endl; } // attempt connection to port if(connect(sockfd, res-&gt;ai_addr, res-&gt;ai_addrlen) == -1){ cout &lt;&lt; "connect fail" &lt;&lt; endl; } // send message to server cout &lt;&lt; "sockfd " &lt;&lt; sockfd &lt;&lt; endl; int byte_count = send(sockfd, "Hello", 5, 0); cout &lt;&lt; byte_count &lt;&lt; endl; </code></pre> <p>The following is the output for Server:</p> <pre><code>Phase1: Login server has TCP port number 21957 and IP address 68.181.201.3 after listen after accept recv()'d -1 bytes of data in buf Msg is ÿhÿ?sÈ Glæ </code></pre> <p>The following is the output for Client:</p> <pre><code>sockfd 4 5 </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.
 

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