read socket problem (proxy server related)
I've moved the 'cheap http server problems...' into a new thread, because it wasn't really my thread :)
To sum up: I'm coding a proxy server, and I'm having great difficulty reading the body, og a http request. I read once, and then read returns -1 (with errno: "Connection reset by peer). It only reads a fraktion of what it is supposed to do. I memset the buffer, it reads into, and I'm pretty sure, it's ok. Here's the code: Code:
int read_body(int fd, char *ptr, int length) Output in terminal: Code:
Status-code: 200 If I read relative small sites (e.g. www.hootmail.dk, 2999b), there's no problem. If I read larger sites (e.g. www.leog.net), it only reads a fraktion (in one read cycle) and returns -1 (in the second read cycle). Does anyone have any experience or have encountered the same problem? Or am I doing somethink wrong? Any info is greatly appriciated. /Jnusa |
The -1 does not mean that no data was read. From the manpage:
Quote:
|
How do you open your socket ? Could you post the code that initialize fd ?
|
No, i do not set errno = 0. Should I do this every time, I call a function that use errno?
Yes you're right about -1, not meaning that there's no more data. But I guess it means that there has been som kind of error (specified with errno). Keep in mind, that I use the current filedescriptor to read the header og the server respons, and I'm having no problem there. I pretty much followed the tutorial in "Internetworking with TCP/IP", by Comer: Here's my code for opening my socket. function call in main: Code:
#define QLEN 32 //max connection queue length Code:
int passiveTCP(const char *service, int qlen){ Code:
int passivesock(const char *service, const char *transport, int qlen) |
I've tried setting errno to zero
Code:
errno = 0; |
It looks like you're doing a read() to get the header, then you're calling read_body(), but there's a good chance the body was already read in with the header and you've just somehow discarded that buffer. Once the HTTP server is done sending you the stuff it closes the connection (Connection reset by peer).
My guess is that you're going to find a big chunk of the body in the buffer with the header. |
Yeah, Im reading the header first. Since I only read 1 char at a time (until I reach body - is determined by CRLFCRLC), I'm certain that I'm not reading anymore data, than the header info. Also I can see in the 1k-2k bytes of body that I do read, that it's the beginning of the body (all the meta tags html start tag and so on). I'm trying to debug, by module testing my functions, and so far they work fine (read_body anyway, and som of the socket functions). I'm very puzzled about this. I've tried rearranging the read_body function, so it's reads the body faster, to avoid a timeout in the socket buffer. That wasn't the either. I've also tried declaring the buffer variable inside read_body, to see if the buffer pointer was messed up... that wasn't the case. Is there any socket options, that only will allow on read, pr connection or something like that?
|
Check out man select. You can add the socket to the read set.
|
hmm... well I'm doing that before I read the reply, to be sure that data is being transmitted. After I'm sure that data is being transmitted, I read the header, look at content (body length, status code, host name ect), and read the body content.
My select code: Code:
int maxfd = fd_request + 1; |
Problem solved
The reson for the problem was, that I was sending a request from the client, to the server. When I calculated the length of the request, it was 100-300kb indstead of 200-300b! This length would be used when sending the data to the server (via : int write(int fd, char *data, int size). This somehow corrupted the memory or something. When I fixed the length, I could read the file descriptor more than once. I have no rational explaination for this.
Scenario: Code:
-> Make mastersocket, and wait for connection |
All times are GMT -5. The time now is 08:37 AM. |