Socket Programming Question

G

Guest

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
 
G

Guest

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
 
V

Vadym Stetsyak

This codeassumes 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.

Is it blocked on socket.Receive?
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top