Thread: Can someone verify this code?

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Can someone verify this code?

    Hey, I wrote a test program for my current project... all it's supposed to do is loop 2000 times: create 50 sockets->connect them->receive on them->close them

    I don't see anything wrong with the code, but since I'm getting runtime errors I just want to be sure that nothing's wrong with the testing code itself. Can someone skim through this and make sure I haven't done anything stupid? Thanks
    Code:
    std::vector<SOCKET> socks;
     //Stick 50 SOCKET's in a vector
     for(int i = 0; i < 50; ++i) //Connect with 50 sockets at a time
     {
      socks.push_back(SOCKET());
     }
     //Fill a sockaddr with the local machine's address
     SOCKADDR_IN saddr;
     HOSTENT* h = gethostbyname("localhost");
     saddr.sin_addr.s_addr = *(unsigned long*)h->h_addr_list[0];
     saddr.sin_family = AF_INET;
     saddr.sin_port = htons(22222);
     //Connect the 50 sockets 2000 times
     for(int j = 0; j < 2000; ++j)
     {
      //Create the 50 sockets
      for(i = 0; i < socks.size(); ++i)
      {
       socks[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
       if(socks[i] == INVALID_SOCKET)
    	std::cout << "Bad init\n";
      }
      //Connect and receive data on each of the sockets
      for(i = 0; i < socks.size(); ++i)
      {
       if(socks[i] == INVALID_SOCKET)
       {
    	std::cout << "Invalid socket\n";
    	continue;
       }
       if(::connect(socks[i], (SOCKADDR*)&saddr, sizeof(saddr)) == SOCKET_ERROR)
       {
    	std::cout << "Err connect\n";
    	continue;
       }
       
       //Receive some data (don't bother checking for complete packet)
       char buf[256];
       int bytes = ::recv(socks[i], buf, 256, 0);
       if(bytes == SOCKET_ERROR || bytes == 0)
       {
    	std::cout << "Err recv\n";
    	continue;
       }
       //Stick a null on the end and print the message
       buf[bytes] = '\0';
       std::cout << buf << std::endl;
      }
      
      //Close the 50 sockets
      for(i = 0; i < socks.size(); ++i)
      {
       int res = ::closesocket(socks[i]);
       if(res == SOCKET_ERROR)
    	std::cout << "Bad close";
      }
     }
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You have an off-by-one bug:
    Code:
       buf[bytes] = '\0';
    What if bytes equals 256? Tell recv() to receive only 255 bytes so you have space for the nul.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ah right, thanks. I don't believe that would be the cause of the errors (the received data is probably at most 4 or 5 characters long), but thanks for pointing it out anyways. Is there anything else fundamentally wrong, or does the rest of it seem to check out?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    It looks alright to me (assuming WSAStartup() has been called), but I'm sure you've done a lot more socket programming than I have. What errors are you getting? Can the server handle 50 concurrent connections? What does WSAGetLastError() return?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM