470,833 Members | 1,967 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,833 developers. It's quick & easy.

Socket Programming Question

Hello-

I'm trying to create a console app that will accept an incoming HTTP request
and output the information of the request to the console. The code is pretty
simple - here's what it looks like:

TcpListener listenter = new TcpListener(IPAddress.Any, 8000);

listenter.Start();

Socket socket = listenter.AcceptSocket();

byte[] buffer = new byte[32];

int bytesReceived = 0;

while( (bytesReceived = socket.Receive(buffer, 0, buffer.Length,
SocketFlags.None)) > 0 )
{
Console.Write(Encoding.ASCII.GetString(buffer));
}

socket.Close();

The response I get looks like this:

GET / HTTP/1.1
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
2.0
..50727; .NET CLR 1.1.4322)
Host: localhost:8000
Connection: Keep-Alive

8000
Connectio

The problem is that the application is hanging after it finished outputting
"Connectio". I had expected the output to end earlier, after the Keep-Alive
header.

Does anyone know why the app might be hanging here?

Regards-
Eric
Mar 2 '06 #1
3 1769
One development on this is that the streange "8000 Connectio" string is
caused by some left-over data in my receive buffer. I changed my while loop
to be this:

while( (bytesReceived = socket.Receive(buffer, 0, buffer.Length,
SocketFlags.None)) > 0 )
{
Console.Write(Encoding.ASCII.GetString(buffer, 0, bytesReceived));
}

and I don't get the errant data.

However, my application is still having trouble figuring out that the client
is done sending data. Any ideas?

Regards-
Eric

"Eric Marthinsen" wrote:
Hello-

I'm trying to create a console app that will accept an incoming HTTP request
and output the information of the request to the console. The code is pretty
simple - here's what it looks like:

TcpListener listenter = new TcpListener(IPAddress.Any, 8000);

listenter.Start();

Socket socket = listenter.AcceptSocket();

byte[] buffer = new byte[32];

int bytesReceived = 0;

while( (bytesReceived = socket.Receive(buffer, 0, buffer.Length,
SocketFlags.None)) > 0 )
{
Console.Write(Encoding.ASCII.GetString(buffer));
}

socket.Close();

The response I get looks like this:

GET / HTTP/1.1
Accept: */*
Accept-Language: en-us
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR
2.0
.50727; .NET CLR 1.1.4322)
Host: localhost:8000
Connection: Keep-Alive

8000
Connectio

The problem is that the application is hanging after it finished outputting
"Connectio". I had expected the output to end earlier, after the Keep-Alive
header.

Does anyone know why the app might be hanging here?

Regards-
Eric

Mar 2 '06 #2
This code
while( (bytesReceived = socket.Receive(buffer, 0, buffer.Length,
SocketFlags.None)) > 0 )
{
Console.Write(Encoding.ASCII.GetString(buffer));
} assumes that client which sends request will break connection, however you
have HTTP/1.1.
HTTP 1.1 assumes that connection is persistant. That is you cannot rely on
the fact that socket.Receive will return 0...
What you can do is to follow HTTP 1.1 protocol, where is stated that header
is ended with double CRLF characters. When you obtained header, you can
determine if there will be any further data.
GET assumes that there will be no data from the requester, POST can have
data.
The problem is that the application is hanging after it finished
outputting
"Connectio". I had expected the output to end earlier, after the
Keep-Alive
header.


Is it blocked on socket.Receive?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Mar 2 '06 #3
Yes, it was blocking on the socket.Receive call.

I was able to achieve partial success by using this code:

byte[] buffer = new byte[socket.Available];
socket.Receive(buffer, 0, buffer.Length, SocketFlags.None);
Console.WriteLine(Encoding.ASCII.GetString(buffer) );

I say partial success because my concern was what would happen if the header
was large (e.g. with post data) so the value of socket.Available was less
than the total amount being sent.

I should check the spec. as I would like to be able to handle posts.

What I'm trying to do is write a simple web server, then ASP.NET clone
(with GREATLY reduced functionality) as a learning exercise.

Thanks for your help.
"Vadym Stetsyak" wrote:
This code
while( (bytesReceived = socket.Receive(buffer, 0, buffer.Length,
SocketFlags.None)) > 0 )
{
Console.Write(Encoding.ASCII.GetString(buffer));
} assumes that client which sends request will break connection, however you
have HTTP/1.1.
HTTP 1.1 assumes that connection is persistant. That is you cannot rely on
the fact that socket.Receive will return 0...
What you can do is to follow HTTP 1.1 protocol, where is stated that header
is ended with double CRLF characters. When you obtained header, you can
determine if there will be any further data.
GET assumes that there will be no data from the requester, POST can have
data.
The problem is that the application is hanging after it finished
outputting
"Connectio". I had expected the output to end earlier, after the
Keep-Alive
header.


Is it blocked on socket.Receive?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com

Mar 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Jean-Philippe Guyon | last post: by
2 posts views Thread by dream machine | last post: by
1 post views Thread by John Sheppard | last post: by
5 posts views Thread by Justin Creasy | last post: by
2 posts views Thread by djc | last post: by
14 posts views Thread by =?Utf-8?B?TWlrZVo=?= | last post: by
3 posts views Thread by Stuart | last post: by
1 post views Thread by =?Utf-8?B?UmFqbmk=?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.