sedj
Programmer
- Aug 6, 2002
- 5,610
Hi,
I asked this question ages ago, and got around the problem by looking for a special terminating charcter or set of characters, but I'd like to revisit it, as I can't quite believe that what I'm seeing really the case.
Posted below is a very simple programme that listens on a port, and prints out the request. There is purposely no error checking on lots of the function calls, in order to keep things as simple as possible for those reading this post.
My question is, how do you tell when a client has sent all the data it wants to send ? It seems to just hang on the blocking recv(). Using fcntl() to set non-blocking seems to just break things.
Can anyone tell me whats going on or where my mistake lies, or is this correct behaviour ?
[Coming from a Java background I find this a bit odd, as the java.net.Socket classes and the associated IO streams don't seem to block when doing a similar kind of thing].
To test it, just do a "wget and watch the blocking happen in recvFunc() !
Thanks for any insights.
--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
I asked this question ages ago, and got around the problem by looking for a special terminating charcter or set of characters, but I'd like to revisit it, as I can't quite believe that what I'm seeing really the case.
Posted below is a very simple programme that listens on a port, and prints out the request. There is purposely no error checking on lots of the function calls, in order to keep things as simple as possible for those reading this post.
My question is, how do you tell when a client has sent all the data it wants to send ? It seems to just hang on the blocking recv(). Using fcntl() to set non-blocking seems to just break things.
Can anyone tell me whats going on or where my mistake lies, or is this correct behaviour ?
[Coming from a Java background I find this a bit odd, as the java.net.Socket classes and the associated IO streams don't seem to block when doing a similar kind of thing].
To test it, just do a "wget and watch the blocking happen in recvFunc() !
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <error.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define PORT 8080
void recvFunc(int client_sd);
int main(void) {
// set up the socket stuff
struct sockaddr_in name;
int server_sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
int yes = 1;
setsockopt(server_sd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
memset(&name, 0, sizeof(name));
name.sin_family = AF_INET;
name.sin_port = htons(PORT);
name.sin_addr.s_addr = htonl(INADDR_ANY);
bind(server_sd, (struct sockaddr *)&name, sizeof(name));
listen(server_sd, 10);
// accept loop
while (1) {
int client_sd;
struct sockaddr_in client_addr;
socklen_t sin_size = sizeof(client_addr);
client_sd = accept(server_sd, (struct sockaddr *) &client_addr, &sin_size);
recvFunc(client_sd);
}
}
void recvFunc(int client_sd) {
//fcntl(client_sd, F_SETFL, O_NONBLOCK);
int read;
char data;
while (1) {
// just read a byte at a time to keep things simple
read = recv(client_sd, &data, 1, 0);
printf("%c", data);
if (read == 0) {
printf("\0 bytes read - breaking\n");
break;
}
if (read < 0) {
perror("recv broke :");
}
}
printf("\nrecvFunc() complete\n");
}
Thanks for any insights.
--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software