title: Winsock send fails with error 10093
body: <p>I am writing a bittorrent client and need to contact multiple trackers over a tcp connection. In order to do this I wrote a winsock wrapper class shown below:</p>
  
  <pre><code>class trackerSocket{
  public:
      ~trackerSocket();
  
      int trackerInitialize(string address);
      int trackerSend(string getParams);
      int trackerRecv();
  
      be_node *responseDict;
      bool working;
  
  private:
      string address;
      string port;
      string protocol;
      string page;
      SOCKET ConnectSocket;
  
      int parseAnnounce(string announce);
      int parseTrackerResponse(string response);
  };
  </code></pre>
  
  <p>The program starts by assigning a new trackerSocket class to a variable. The trackerInitialize function is called on this function and if successful, the class is pushed onto a vector to store all the working trackers. Here is the trackerInitialize function:</p>
  
  <pre><code>int trackerSocket::trackerInitialize(string announce){
      WSADATA wsaData;
      int iResult;
  
      working = true;
  
      iResult = parseAnnounce(announce);
      if(iResult != 0){
          working = false;
          return iResult;
      }
  
      //Initialize Winsock
      iResult = WSAStartup(MAKEWORD(2,2), &amp;wsaData);
      if(iResult != 0){
          return 1;
      }
  
      struct addrinfo *result = NULL,
                      *ptr  = NULL,
                      hints;
  
      ZeroMemory(&amp;hints, sizeof(hints));
      hints.ai_family = AF_INET;
      hints.ai_socktype = SOCK_STREAM;
      hints.ai_protocol = IPPROTO_TCP;
  
      //Resolve the server address and port
      iResult = getaddrinfo(address.c_str(), port.c_str(), &amp;hints, &amp;result);
      if(iResult != 0){
          WSACleanup();
          return 1;
      }
  
          ConnectSocket = INVALID_SOCKET;
  
      //Attempt to connect to the first address returned by
      //the call to getaddrinfo
      ptr = result;
  
      do{
          //Create a socket for connecting to the server
          ConnectSocket = socket(ptr-&gt;ai_family, ptr-&gt;ai_socktype, ptr-&gt;ai_protocol);
          if(ConnectSocket == INVALID_SOCKET){
              ptr = ptr-&gt;ai_next;
              continue;
          }
  
          //Connect to server
          iResult = connect(ConnectSocket, ptr-&gt;ai_addr, (int)ptr-&gt;ai_addrlen);
          if(iResult != SOCKET_ERROR){
              break;
          } else {
              closesocket(ConnectSocket);
              ConnectSocket = INVALID_SOCKET;
              ptr = ptr-&gt;ai_next;
          }
      } while(ptr != NULL);
  
      freeaddrinfo(result);
      if(ConnectSocket == INVALID_SOCKET){
          working = false;
          WSACleanup();
          return 1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Then the program performs code to generate the message to be sent to the trackers. For each tracker class in the vector it's trackerSend function is called with the message, here is the trackerSend function:</p>
  
  <pre><code>int trackerSocket::trackerSend(string getParams){
      int iResult;
  
      ostringstream os;
      os &lt;&lt; "GET " &lt;&lt; page &lt;&lt; getParams &lt;&lt; " HTTP/1.1\r\n"
          &lt;&lt; "Host: " &lt;&lt; address &lt;&lt; "\r\n"
          &lt;&lt; "Accept: text/html\r\n"
          &lt;&lt; "\r\n";
  
      string sendBuf = os.str();
  
      //Send tracker request
      iResult = send(ConnectSocket, sendBuf.c_str(), strlen(sendBuf.c_str()), 0);
      if(iResult == SOCKET_ERROR){
          working = false;
          closesocket(ConnectSocket);
          WSACleanup();
          return 1;
      }
  
      return 0;
  }
  </code></pre>
  
  <p>Every time the program runs the send function returns -1 for every tracker. If I call the WSAGetLastError() function it returns 10093. The msdn definition for this error is:</p>
  
  <p>Successful WSAStartup not yet performed.
  Either the application has not called WSAStartup or WSAStartup failed. The application may be accessing a socket that the current active task does not own (that is, trying to share a socket between tasks), or WSACleanup has been called too many times.</p>
  
  <p>I can't see that WSACleanup has been called too many times so I can only assume that the socket is not owned by the current active task (no idea what that means), can anyone see the problem?</p>
  
  <p>Heres some of the code for the main program (which I have described above):</p>
  
  <pre><code>//Store tracker URL's in vector
  vector&lt;trackerSocket&gt; trackers;
  trackerSocket *temptracker = new trackerSocket();
  iResult = temptracker-&gt;trackerInitialize(announce);
  if(iResult == 0){
      trackers.push_back(*temptracker);
  }
  if(announcelist != NULL){
      i = 0;
      while(announcelist[i]){
          if(strcmp(announcelist[i]-&gt;val.l[0]-&gt;val.s, announce.c_str()) != 0){
              temptracker = new trackerSocket();
              iResult = temptracker-&gt;trackerInitialize(announcelist[i]-&gt;val.l[0]-&gt;val.s);
              if(iResult == 0){
                  trackers.push_back(*temptracker);
              }
          }
          i++;
      }
  }
  
  //Check that at least one of the tracker URL's was valid
  if(trackers.size() == 0){
      printf("None of the tracker URL's provided were valid.\n");
      return 1;
  }
  
  //Generate some required values
  string peerid = genPeerID();
  string peerport = "12345";
  int uploaded = 0;
  int downloaded = 0;
  
  //Work out how many bytes are left to download
  int left = 0;
  if(singlefile){
      left = length;
  } else {
      for(i = 0; i &lt; filesinfo.size(); i++){
          left += filesinfo[i].length;
      }
  }
  
  //Send GET Request to tracker
  i = 0;
  ostringstream os;
  string getParams;
  string response;
  os &lt;&lt; "info_hash=" &lt;&lt; infohash &lt;&lt; "&amp;peer_id=" &lt;&lt; peerid &lt;&lt; "&amp;port=" &lt;&lt; peerport &lt;&lt;
      "&amp;uploaded=" &lt;&lt; uploaded &lt;&lt; "&amp;downloaded=" &lt;&lt; downloaded &lt;&lt; "&amp;event=started";
  
  getParams = os.str();
  do{     
      iResult = trackers[i].trackerSend(getParams);
      if(iResult != 0){
          printf("trackerSend %d failed: %d\n", i, iResult);
          i++;
          continue;
      }
  } while(i &lt; trackers.size());
  </code></pre>