Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Winsock recv help

Winsock recv help

Scheduled Pinned Locked Moved C / C++ / MFC
databasedebugginghelp
4 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KnaveR777
    wrote on last edited by
    #1

    Hello! Im fairly new to winsock programing, and am having trouble receiving a response that is spread accross several packets. I have been watching the communications through a packet sniffer, and know that 30 to 40 1500 byte sized packets are comming in with good data, but my program is only executing one recv and reporting a bytes Recv of 9. There must be some concept that im completely missing :( any help would be greatly appreciated. heres my code: bytesRecv = SOCKET_ERROR; char queryRecvBuff[1500] = ""; CString reply = ""; send(m_socket,serverQuery,30,0); TRACE("SENT\nRECEIVING:\n"); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { TRACE( "Connection Closed in Auth.\n"); break; } if (bytesRecv < 0){ TRACE("\tbytes Recv < 0!!!"); return; } reply.Append(queryRecvBuff,bytesRecv); TRACE( "\tBytes Recv: %ld\n", bytesRecv ); } TRACE("\nRECEIVED"); output: WAITING FOR Verification: VERIFIED Sending Query: SENT RECEIVING: Bytes Recv: 9 RECEIVED

    G 1 Reply Last reply
    0
    • K KnaveR777

      Hello! Im fairly new to winsock programing, and am having trouble receiving a response that is spread accross several packets. I have been watching the communications through a packet sniffer, and know that 30 to 40 1500 byte sized packets are comming in with good data, but my program is only executing one recv and reporting a bytes Recv of 9. There must be some concept that im completely missing :( any help would be greatly appreciated. heres my code: bytesRecv = SOCKET_ERROR; char queryRecvBuff[1500] = ""; CString reply = ""; send(m_socket,serverQuery,30,0); TRACE("SENT\nRECEIVING:\n"); while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) { TRACE( "Connection Closed in Auth.\n"); break; } if (bytesRecv < 0){ TRACE("\tbytes Recv < 0!!!"); return; } reply.Append(queryRecvBuff,bytesRecv); TRACE( "\tBytes Recv: %ld\n", bytesRecv ); } TRACE("\nRECEIVED"); output: WAITING FOR Verification: VERIFIED Sending Query: SENT RECEIVING: Bytes Recv: 9 RECEIVED

      G Offline
      G Offline
      greba
      wrote on last edited by
      #2

      Knave777Wave wrote: while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); I don't have all your code (ie. a server to send responses back) so I can't step through your code to really help you. But based on the code above: SOCKET_ERROR is defined as having value -1 recv(...) is returning 9 bytes to you This explains why the loop is only run once as bytesRecv will contain the value 9 which is not equal to -1 (SOCKET_ERROR). As to if this is the proper value that is being recieved I don't know. If the first packet being sent from the server is of size 9 then this makes sense. Otherwise, I don't really know the reason. Greba, My lack of content on my home page should be entertaining.

      K 1 Reply Last reply
      0
      • G greba

        Knave777Wave wrote: while( bytesRecv == SOCKET_ERROR ) { bytesRecv = recv( m_socket, queryRecvBuff, 1500, 0); I don't have all your code (ie. a server to send responses back) so I can't step through your code to really help you. But based on the code above: SOCKET_ERROR is defined as having value -1 recv(...) is returning 9 bytes to you This explains why the loop is only run once as bytesRecv will contain the value 9 which is not equal to -1 (SOCKET_ERROR). As to if this is the proper value that is being recieved I don't know. If the first packet being sent from the server is of size 9 then this makes sense. Otherwise, I don't really know the reason. Greba, My lack of content on my home page should be entertaining.

        K Offline
        K Offline
        KnaveR777
        wrote on last edited by
        #3

        thnx... i dont know what i was thinking I switched it to a do--while w/ (bytesRecv != SOCKET_ERROR) and now i receive tons of packets and jump out of the loop with bytesRecv == 0. Is this the normal behavior for such a transaction to end? Ive noticed several empty data packets being sent in the sniffer. Are these special packets that singal 'received' and so forth? If so, is my app receiving a special 'transfer done' packet that causes my last recv to unblock and return 0 bytes? I dont know what the server is doing since it is not my app, its a particular games master server with a list of public game servers. I know all of this sounds pretty dumb, but this is a learning experiment where im trying to dive in a learn what is going on at the packet and app level. thnx

        G 1 Reply Last reply
        0
        • K KnaveR777

          thnx... i dont know what i was thinking I switched it to a do--while w/ (bytesRecv != SOCKET_ERROR) and now i receive tons of packets and jump out of the loop with bytesRecv == 0. Is this the normal behavior for such a transaction to end? Ive noticed several empty data packets being sent in the sniffer. Are these special packets that singal 'received' and so forth? If so, is my app receiving a special 'transfer done' packet that causes my last recv to unblock and return 0 bytes? I dont know what the server is doing since it is not my app, its a particular games master server with a list of public game servers. I know all of this sounds pretty dumb, but this is a learning experiment where im trying to dive in a learn what is going on at the packet and app level. thnx

          G Offline
          G Offline
          greba
          wrote on last edited by
          #4

          Under the documentation of the recv function: "If the connection has been gracefully closed, the return value is zero". I'm assuming this would mean that the server has closed the connection. As to if this a correct or not assumption I don't know. It doesn't sound pretty dumb, that is definitely a challanging and rewarding way to learn. If you still have problems with your recv, then I would suggest creating a server which emulates the behavior of the real server, and determine if you can send empty packets, and what happens when you close down a connection. Greba, My lack of content on my home page should be entertaining.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups