I am trying to write a program that will send an HTTP request and get an
HTTP response. However, I am having trouble getting the full payload
(i.e. the web page) returned from the host. I am using a blocking
socket, so I need to test the end of the data somehow, or I get a "hung"
effect while the Socket.Receive() method waits.
I constructed a loop like the following to test for end-of-data, but I
only get part of the data returned for large pages and the loop exits.
Socket IPsocket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
// Socket connection code was here...
// The first read takes place...
// ...then the following is executed:
while ( rBytes > 0)
{
if (!IPsocket.Poll(3000,SelectMode.SelectRead))
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
mStrm.Write(RecvBytes,0,rBytes);
break;
}
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
// Write to buffer
mStrm.Write(RecvBytes,0,rBytes);
}
Is there different way I should be testing for the end of the data? I've
tried using Socket.Available(), but found that it returns 0 when there
is in fact more data.
Thanks, JA 9 4804
"gilad" <gi***@arbingerBADSPAMBOTsys.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... I am trying to write a program that will send an HTTP request and get an HTTP response. However, I am having trouble getting the full payload (i.e. the web page) returned from the host. I am using a blocking socket, so I need to test the end of the data somehow, or I get a "hung" effect while the Socket.Receive() method waits.
I constructed a loop like the following to test for end-of-data, but I only get part of the data returned for large pages and the loop exits.
Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Is there a particular reason you're not using the .NET HTTP classes?
If you want to implement the HTTP protocol using TCP/IP yourself, you'd
better read the spec: http://www.w3.org/Protocols/rfc2616/rfc2616.html
Pay special attention to the section on the content-length header, the
Connection header, chunked transfer encoding and the 100 continue response
code.
Consider using HTTP 1.0 http://www.w3.org/Protocols/rfc1945/rfc1945
as it's considerably simpler to implement in raw sockets. For instance,
Socket.Recive() in a loop will work in HTTP 1.0 since the server will close
the connection when the response is done.
David
"gilad" <gi***@arbingerBADSPAMBOTsys.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl... I am trying to write a program that will send an HTTP request and get an HTTP response. However, I am having trouble getting the full payload (i.e. the web page) returned from the host. I am using a blocking socket, so I need to test the end of the data somehow, or I get a "hung" effect while the Socket.Receive() method waits.
I constructed a loop like the following to test for end-of-data, but I only get part of the data returned for large pages and the loop exits.
Socket IPsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Is there a particular reason you're not using the .NET HTTP classes?
If you want to implement the HTTP protocol using TCP/IP yourself, you'd
better read the spec: http://www.w3.org/Protocols/rfc2616/rfc2616.html
Pay special attention to the section on the content-length header, the
Connection header, chunked transfer encoding and the 100 continue response
code.
Consider using HTTP 1.0 http://www.w3.org/Protocols/rfc1945/rfc1945
as it's considerably simpler to implement in raw sockets. For instance,
Socket.Recive() in a loop will work in HTTP 1.0 since the server will close
the connection when the response is done.
David
David Browne wrote: Is there a particular reason you're not using the .NET HTTP classes?
I am building a simple proxy server.
If you want to implement the HTTP protocol using TCP/IP yourself, you'd better read the spec:
http://www.w3.org/Protocols/rfc2616/rfc2616.html
Pay special attention to the section on the content-length header, the Connection header, chunked transfer encoding and the 100 continue response code.
I've done this, but the problem that I see is that the Content-Length
and other fields are not always present. For instance, here is what is
returned from "www.wired.com" as the HTTP header.
HTTP/1.1 200 OK
Date: Wed, 31 Aug 2005 18:48:35 GMT
Server: Apache/1.3.31 (Unix) PHP/4.3.9
Cache-Control: max-age=300
Expires: Wed, 31 Aug 2005 18:53:35 GMT
Connection: close
Content-Type: text/html
(From here on there is just HTML content)
I have found that I either loop with Receive until it hangs (no more
data available, and it blocks), or I've got to check the end of the
transmission. As you can see from the above, there is really nothing to
indicate packet length in the HTTP header.
I am reading the packets in buffers with 4096 bytes length.
David Browne wrote: Is there a particular reason you're not using the .NET HTTP classes?
I am building a simple proxy server.
If you want to implement the HTTP protocol using TCP/IP yourself, you'd better read the spec:
http://www.w3.org/Protocols/rfc2616/rfc2616.html
Pay special attention to the section on the content-length header, the Connection header, chunked transfer encoding and the 100 continue response code.
I've done this, but the problem that I see is that the Content-Length
and other fields are not always present. For instance, here is what is
returned from "www.wired.com" as the HTTP header.
HTTP/1.1 200 OK
Date: Wed, 31 Aug 2005 18:48:35 GMT
Server: Apache/1.3.31 (Unix) PHP/4.3.9
Cache-Control: max-age=300
Expires: Wed, 31 Aug 2005 18:53:35 GMT
Connection: close
Content-Type: text/html
(From here on there is just HTML content)
I have found that I either loop with Receive until it hangs (no more
data available, and it blocks), or I've got to check the end of the
transmission. As you can see from the above, there is really nothing to
indicate packet length in the HTTP header.
I am reading the packets in buffers with 4096 bytes length.
Actually, I may have figured this out, at least partly. If the Poll()
returns "false" (no more data), then I go ahead and try another read. If
the read returns any bytes (rBytes), then I do not break out of the
loop. If it does, then I break out. So far this seems to work. My loop
code now looks like
while ( rBytes > 0)
{
if (!IPsocket.Poll(3000,SelectMode.SelectRead))
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
mStrm.Write(RecvBytes,0,rBytes);
///////// Added the following, and it seems to have helped:
if (rBytes <= 0) break;
}
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
// Write to buffer
mStrm.Write(RecvBytes,0,rBytes);
}
Actually, I may have figured this out, at least partly. If the Poll()
returns "false" (no more data), then I go ahead and try another read. If
the read returns any bytes (rBytes), then I do not break out of the
loop. If it does, then I break out. So far this seems to work. My loop
code now looks like
while ( rBytes > 0)
{
if (!IPsocket.Poll(3000,SelectMode.SelectRead))
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
mStrm.Write(RecvBytes,0,rBytes);
///////// Added the following, and it seems to have helped:
if (rBytes <= 0) break;
}
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
// Write to buffer
mStrm.Write(RecvBytes,0,rBytes);
}
"gilad" <gi***@arbingerBADSPAMBOTsys.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Actually, I may have figured this out, at least partly. If the Poll() returns "false" (no more data), then I go ahead and try another read. If the read returns any bytes (rBytes), then I do not break out of the loop. If it does, then I break out. So far this seems to work. My loop code now looks like
while ( rBytes > 0) {
if (!IPsocket.Poll(3000,SelectMode.SelectRead)) { rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0); mStrm.Write(RecvBytes,0,rBytes);
///////// Added the following, and it seems to have helped: if (rBytes <= 0) break;
Yes. A zero byte Recieve always means that the server has closed the
connection. http://msdn.microsoft.com/library/de...eivetopic1.asp
If the remote host shuts down the Socket connection with the Shutdown
method, and all available data has been received, the Receive method will
complete immediately and return zero bytes.
David
"gilad" <gi***@arbingerBADSPAMBOTsys.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl... Actually, I may have figured this out, at least partly. If the Poll() returns "false" (no more data), then I go ahead and try another read. If the read returns any bytes (rBytes), then I do not break out of the loop. If it does, then I break out. So far this seems to work. My loop code now looks like
while ( rBytes > 0) {
if (!IPsocket.Poll(3000,SelectMode.SelectRead)) { rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0); mStrm.Write(RecvBytes,0,rBytes);
///////// Added the following, and it seems to have helped: if (rBytes <= 0) break;
Yes. A zero byte Recieve always means that the server has closed the
connection. http://msdn.microsoft.com/library/de...eivetopic1.asp
If the remote host shuts down the Socket connection with the Shutdown
method, and all available data has been received, the Receive method will
complete immediately and return zero bytes.
David
David Browne wrote: Yes. A zero byte Recieve always means that the server has closed the connection.
I guess what I didn't understand is why, if that's true, the
while ( rBytes > 0 )
test was not sufficient to break out of the loop. Before, when I
observed the loop to hang, it was constructed as follows (without the
Poll() test):
while ( rBytes > 0 )
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
// Write to buffer
mStrm.Write(RecvBytes,0,rBytes);
}
The Poll() test worked, but showed a inconsistencies, so I did something
else and it seems to be working as expected:
while ( rBytes > 0 )
{
rBytes = IPsocket.Receive(RecvBytes, RecvBytes.Length, 0);
// Write to buffer
if (rBytes > 0) mStrm.Write(RecvBytes,0,rBytes);
}
Before, I assumed that even if rBytes was 0, the Write() method would
simply write 0 bytes to the MemoryStream "mStrm". Could this have been
the cause of the hang? At any rate, it seems to have fixed the problem,
and I do not need to use the Poll() code.
Any thoughts?
Thanks, JA This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mscirri |
last post by:
The code below is what I am using to asynchronously get data from a
PocketPC device. The data comes in fine in blocks of 1024 bytes but
even when I send no data from the PocketPC constant blocks of...
|
by: Colin |
last post by:
I'm writing a little console socket server but I'm having some
difficulty. Can I ask your advice - where is the best place to get
some help on that topic? It would be nice if some people who knew...
|
by: Macca |
last post by:
Hi,
I have a synchronous socket server which my app uses to read data from
clients.
To test this I have a simulated client that sends 100 byte packets.
I have set up the socket server so...
|
by: Macca |
last post by:
My app has an asynchronous socket server. It will have 20 clients connected
to the server. Each client sends data every 500 millisecondsThe Connections
once established will not be closed unless...
|
by: djc |
last post by:
I read a network programming book (based on framework 1.1) which indicated
that you should 'never' use the RecieveTimeout or the SendTimeout 'socket
options' on TCP sockets or you may loose data. I...
| |
by: Macca |
last post by:
Hi,
I am writing an asychronous socket server to handle 20+ simulataneous
connections.
I have used the example in MSDN as a base. The code is shown at end of
question.
Each connection has a...
|
by: zxo102 |
last post by:
Hi everyone,
I am using a python socket server to collect data from a socket
client and then control a image location ( wxpython) with the data,
i.e. moving the image around in the wxpython frame....
|
by: ahlongxp |
last post by:
socket.makefile() may lose data when "connection reset by peer".
and socket.recv() will never lose the data.
change the "1" to "0" in the client code to see the difference.
confirmed on both...
|
by: manasap |
last post by:
Hi all!
I've written a server and a client application using asynchronous sockets.The client sends data packets for every 7 seconds.The server receives the packets.
This process proceeds...
|
by: keksy |
last post by:
Hi every1,
I am writing a small client/server application and in it I want to
send an image asynchronous from the client to the server through a TCP
socket. I found an example code on the MSDN...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |