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. Database & SysAdmin
  3. System Admin
  4. Berkeley sockets

Berkeley sockets

Scheduled Pinned Locked Moved System Admin
sysadminquestion
7 Posts 2 Posters 4 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.
  • J Offline
    J Offline
    Jon Sagara
    wrote on last edited by
    #1

    Ok, I'm a bit perplexed with simple concept. I have a small client/server app. I'm sending <1024 bytes of data from the client to the server with the send() function call. On the server, I read this data with the recv() function call. I assumed that this would "consume" the data, and the next call to recv() from either side would block because there is no more data to get. Well, for some reason, the data remains, and I am confused why. Here are the steps of the program: 1. Client sends file name to Server with send() 2. Server reads file name with recv() 3. Server tries to open file; if successful, send back "yes" else send back "no" using the send() function. 4. Client calls recv() to find out whether open() was successful. At this point, if I didn't send the "yes" or "no" back to the Client, I thought the call to recv() on the Client would block because there would be no data to read. However, it reads in the data that it originally sent to the Server (i.e., the filename). Where am I going wrong here? I realize this isn't the best solution. I have a better one that works, but I tried this first and the mystery has me perplexed. I didn't post the code because it is lengthy. Thanks, Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

    J 1 Reply Last reply
    0
    • J Jon Sagara

      Ok, I'm a bit perplexed with simple concept. I have a small client/server app. I'm sending <1024 bytes of data from the client to the server with the send() function call. On the server, I read this data with the recv() function call. I assumed that this would "consume" the data, and the next call to recv() from either side would block because there is no more data to get. Well, for some reason, the data remains, and I am confused why. Here are the steps of the program: 1. Client sends file name to Server with send() 2. Server reads file name with recv() 3. Server tries to open file; if successful, send back "yes" else send back "no" using the send() function. 4. Client calls recv() to find out whether open() was successful. At this point, if I didn't send the "yes" or "no" back to the Client, I thought the call to recv() on the Client would block because there would be no data to read. However, it reads in the data that it originally sent to the Server (i.e., the filename). Where am I going wrong here? I realize this isn't the best solution. I have a better one that works, but I tried this first and the mystery has me perplexed. I didn't post the code because it is lengthy. Thanks, Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

      J Offline
      J Offline
      Jon Sagara
      wrote on last edited by
      #2

      So now I'm thinking the read() returns 0 when there is no data to read. I think in my case, I was actually reusing a dirty buffer, giving me the bad results. So read()/recv() will only block if I partially fill the buffer but it is expecting a full buffer. Is this correct? Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

      M J 2 Replies Last reply
      0
      • J Jon Sagara

        So now I'm thinking the read() returns 0 when there is no data to read. I think in my case, I was actually reusing a dirty buffer, giving me the bad results. So read()/recv() will only block if I partially fill the buffer but it is expecting a full buffer. Is this correct? Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

        M Offline
        M Offline
        markkuk
        wrote on last edited by
        #3

        You can set the socket to be either blocking or nonblocking. recv() should return 0 only when the connection has been closed at the other end. If the socket is set to blocking behaviour, recv() blocks only when there aren't any bytes in the receive buffer.

        J 1 Reply Last reply
        0
        • M markkuk

          You can set the socket to be either blocking or nonblocking. recv() should return 0 only when the connection has been closed at the other end. If the socket is set to blocking behaviour, recv() blocks only when there aren't any bytes in the receive buffer.

          J Offline
          J Offline
          Jon Sagara
          wrote on last edited by
          #4

          How do you specify that? Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

          M 1 Reply Last reply
          0
          • J Jon Sagara

            So now I'm thinking the read() returns 0 when there is no data to read. I think in my case, I was actually reusing a dirty buffer, giving me the bad results. So read()/recv() will only block if I partially fill the buffer but it is expecting a full buffer. Is this correct? Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

            J Offline
            J Offline
            Jon Sagara
            wrote on last edited by
            #5

            Got it. It was a small misunderstanding on my part. Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

            1 Reply Last reply
            0
            • J Jon Sagara

              How do you specify that? Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

              M Offline
              M Offline
              markkuk
              wrote on last edited by
              #6

              ULONG mode = 0; // 0 for blocking mode, nonzero for nonblocking
              WSAIoctl(m_socket, FIONBIO, (LPVOID) &mode, sizeof(ULONG), NULL, 0, NULL, NULL, NULL);

              J 1 Reply Last reply
              0
              • M markkuk

                ULONG mode = 0; // 0 for blocking mode, nonzero for nonblocking
                WSAIoctl(m_socket, FIONBIO, (LPVOID) &mode, sizeof(ULONG), NULL, 0, NULL, NULL, NULL);

                J Offline
                J Offline
                Jon Sagara
                wrote on last edited by
                #7

                I was on Unix, so that wouldn't work, but I was able to make it work another way. Thanks for the answer, though. Jon Sagara "Oh Lisa, you and your lies. Bart's a vampire, beer kills brain cells. Let's go back to that building... thingy... where our beds... is."

                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