Note that there are some explanatory texts on larger screens.

plurals
  1. POBittorrent Client in C++, Connecting to Peer on Non-Blocking socket always times out
    primarykey
    data
    text
    <p>EDIT::</p> <p>So the answer I accepted below was not actually the issue. I verified through wireshark that the peers are indeed transmitting over TCP for torrent downloads. So I should be able to connect, but all attempts timeout...</p> <hr> <p>So I'm making a bittorrent client in C++ and I'm using the BSD sockets library for all network communications. I have some code to connect to peers over TCP but every attempt times out. I am 100% certain the peers are valid for the file I'm seeking to download, I started downloading the file in Transmission and the same peers were being connected to.</p> <p>Here is my connect code, the first part is simply adding a bunch of peers to a vector so I can iterate over it and try each peer: </p> <p>(NOTE" all of the upper-case system calls are just wrapper functions for error handling purposes. There isn't any funny business happening there.)</p> <pre><code> char * HOST; uint16_t PORT; std::vector&lt;char *&gt; all_ips; std::vector&lt;uint16_t&gt; all_ports; all_ips.push_back("213.112.225.102"); all_ports.push_back(18715); uint32_t i = 0; for (; i &lt; all_ips.size(); i++) { HOST = all_ips[i]; PORT = all_ports[i]; struct sockaddr * saddr; struct sockaddr_in addr; struct addrinfo hints, * ai, * it; char strportnum[25]; memset(&amp;hints, '\0', sizeof(hints)); hints.ai_flags = AI_ADDRCONFIG; hints.ai_socktype = SOCK_STREAM; snprintf(strportnum, 10, "%d", PORT); GetAddrInfo(HOST, strportnum, &amp;hints, &amp;ai); for (it = ai; it != NULL; it = it-&gt;ai_next) { if ((sockFd = Socket(AF_INET, SOCK_STREAM, 0)) != -1) { saddr = ai-&gt;ai_addr; saddr-&gt;sa_family = AF_INET; int res; long arg; fd_set myset; struct timeval tv; int valopt; socklen_t lon; // Set non-blocking if( (arg = fcntl(sockFd, F_GETFL, NULL)) &lt; 0) { fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); exit(0); } arg |= O_NONBLOCK; if( fcntl(sockFd, F_SETFL, arg) &lt; 0) { fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); exit(0); } // Trying to connect with timeout res = Connect(sockFd, saddr, sizeof(*saddr)); if (res &lt; 0) { if (errno == EINPROGRESS) { fprintf(stderr, "EINPROGRESS in connect() - selecting\n"); do { //Set timeouts tv.tv_sec = 20; tv.tv_usec = 0; FD_ZERO(&amp;myset); FD_SET(sockFd, &amp;myset); res = Select(sockFd + 1, NULL, &amp;myset, NULL, &amp;tv); if (res &lt; 0 &amp;&amp; errno != EINTR) { fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); break; } else if (res &gt; 0) { // Socket selected for write lon = sizeof(int); if (getsockopt(sockFd, SOL_SOCKET, SO_ERROR, (void*)(&amp;valopt), &amp;lon) &lt; 0) { fprintf(stderr, "Error in getsockopt() %d - %s\n", errno, strerror(errno)); break; } // Check the value returned... if (valopt) { fprintf(stderr, "Error in delayed connection() %d - %s\n", valopt, strerror(valopt)); break; } break; } else { fprintf(stderr, "Timeout in select() - Cancelling!\n"); break; } } while (1); } else { fprintf(stderr, "Error connecting %d - %s\n", errno, strerror(errno)); break; } } // Set to blocking mode again... if( (arg = fcntl(sockFd, F_GETFL, NULL)) &lt; 0) { fprintf(stderr, "Error fcntl(..., F_GETFL) (%s)\n", strerror(errno)); break; } arg &amp;= (~O_NONBLOCK); if(fcntl(sockFd, F_SETFL, arg) &lt; 0) { fprintf(stderr, "Error fcntl(..., F_SETFL) (%s)\n", strerror(errno)); break; } } } freeaddrinfo(ai); } </code></pre> <p>I've been using this site as a guide to non-blocking sockets: <a href="http://developerweb.net/viewtopic.php?id=3196" rel="nofollow">http://developerweb.net/viewtopic.php?id=3196</a></p>
    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