title: c++ sockets multiple requests
body: <p>I have created a short class for socket communication and want so send and receive multiple times to the same device (here as an example www.kernel.org in my case its a sensor connected over ethernet).
  The communication is working with HTTP and I tested the device with "PacketSender" and it works fine.</p>
  
  <p>For the first try I send the request and get the expected response. Then I want to loop it, but then I can send as much and as diffrent requests as I want and I get nothing.</p>
  
  <p>Here is my class (I left it only as a draft in the H-File and will make it more decent when it works)</p>
  
  <pre><code>#ifndef SOCKETCLASS_H_INCLUDED
  #define SOCKETCLASS_H_INCLUDED
  
  #include &lt;windows.h&gt;
  #include &lt;winsock2.h&gt;
  
  #include &lt;stdio.h&gt;
  
  #pragma comment(lib, "Ws2_32.lib")
  
  class WinSocket{
  private:
      SOCKET s;
      long rc;
      int Status = 0;
      int PackageSize = 10;
  
  public:
  
      WinSocket(){
         rc=startWinsock();
          if(rc!=0){
              printf("Error: startWinsock failed, ErCode %d \n",int(rc));
              Status= -1;
          }else{
              printf("Winsock ready!\n");
          }
  
      }
  
      int openSocket(char *IPv4, int Port){
          SOCKADDR_IN addr;
  
          int rc = 0;
  
          s = socket(AF_INET,SOCK_STREAM,0);
          if(s==INVALID_SOCKET){
              printf("Error: Socket could not be created! ErCode: %d\n",WSAGetLastError());
              return 1;
          }else{
              printf("Socket created\n");
          }
  
          memset(&amp;addr,0,sizeof(SOCKADDR_IN));
          addr.sin_family=AF_INET;
          addr.sin_port=htons(80);    
          addr.sin_addr.s_addr=inet_addr(IPv4);    
  
          rc=connect(s,(SOCKADDR*)&amp;addr,sizeof(SOCKADDR));
          if(rc==SOCKET_ERROR){
              printf("Error: Failed to connect, ErCode: %d\n",WSAGetLastError());
              return 1;
          }else{
              printf("Connected to %s...\n",IPv4);
  
          }
  
          return 0;
      }
  
      int sendPackage(char *Buffer, int BufferSize){
          int i,j;
          int result;
          int Pointer = 0;
  
  
          char *Package;
          Package = new char[PackageSize];
  
          for(i = 0; i &lt; BufferSize; i+=PackageSize){
              for(j = 0; j &lt; PackageSize; j++){
                  Package[j] = Buffer[Pointer];
                  Pointer++;
                  if(BufferSize &lt;= Pointer){
                      break;
                  }
              }
  
              result = send(s,Package,PackageSize,0);
              if(result&lt;0){
                  printf("Error Occured!\n");
                  return -1;
              }
  
          }
          delete[]Package;
  
          return 0;
  
      }
  
  
  
  int readLine(char *Buffer, int *BufferSize){
      int MAX_BUFFERSIZE = 1024;
      int res = 0;
      char B[1];
      *BufferSize = 0;
      int Pointer = 0;
  
      for(int i=0;i&lt;MAX_BUFFERSIZE;i++){
          res = recv(s,B,1,0);
          if(res &gt; 0){
              if(B[0]!='\n'){
                  //printf("%d;%s\n",res,B);
                  Buffer[Pointer] = B[0];
                  Pointer += 1;
              }else{
                  Buffer[Pointer]='\n';
                  *BufferSize = Pointer;
                  return 0;
              }
          }else if(res == 0){
              return 1;
          }else{
              return -1;
          }
      }
  
      return 0;
  }
  
  int startWinsock(void){
      WSADATA wsa;
      return WSAStartup(MAKEWORD(2,0),&amp;wsa);
  }
  
  
  
        ~WinSocket(){
            closesocket(s);
        }
    };
  
    #endif // SOCKETCLASS_H_INCLUDED
  </code></pre>
  
  <p>As a main function i use the following code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "SocketClass.h"
  
  int main(){
      char buf[256];
  
      int i,j;
  
      int requestSize = 100;
      char request[100];
  
      char Buf[1024];
      int BufferSize = 1;
  
      WinSocket S;
  
      S.openSocket("147.75.44.153",80);
      for(int l = 0; l&lt;10; l++){
          printf("Loop %d\n",l);
          printf("Sending: ");
          requestSize =  sprintf(request,"GET /faq.html HTTP/1.1\r\nHost: www.kernel.org\r\nConnection: close\r\n\r\n");
          printf("%d Bytes: \n",requestSize);
          S.sendPackage(request,100);
          for(j= 0; j &lt; requestSize; j ++){
              printf("%c",request[j]);
          }
          printf("\n");
  
          Sleep(500);
  
  
          printf("Waiting for responset...\n");
  
          while(BufferSize!=0){
              printf("Received: ");
              S.readLine(Buf, &amp;BufferSize);
              printf("%d Bytes: \n",BufferSize);
  
              for(int i=0;i&lt;BufferSize;i++){
                      printf("%c",Buf[i]);
              }
              printf("\n");
          }
          if(BufferSize==0){
              printf("\nNothing more received...\n");
          }
          printf("Repeat!\n");
          Sleep(5000);
      }
  
      printf("Finished...\n");
      printf("Press ENTER to continue...\n");
      getchar();
      return 0;
  }
  </code></pre>
  
  <p>I tested to open and close the socket every loop. This is sometimes working (so is there a non defined pointer I don't mentioned?), but I want a long duration of the connection.</p>
  
  <p>What I do not understand is, why I can send multiple times but then receive only at the first sending any data. </p>
  
  <p>I am using windows 7 and mingw (GNU GCC Compiler).</p>
  
  <p>Thank you for your help.</p>