Berkeley sockets
-
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."
-
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."
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."
-
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."
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. -
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.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."
-
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."
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."
-
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."
-
ULONG mode = 0; // 0 for blocking mode, nonzero for nonblocking
WSAIoctl(m_socket, FIONBIO, (LPVOID) &mode, sizeof(ULONG), NULL, 0, NULL, NULL, NULL);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."