473,409 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,409 software developers and data experts.

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 1882
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jean-Philippe Guyon | last post by:
Hello, I am trying to compile a class that uses socket using the Visual C++ ..NET compiler. I get the following error: ------ Build started: Project: infCommon, Configuration: Release Win32...
2
by: dream machine | last post by:
Hi all , with BegeinReceive I can build async method of Socket Class that Receive the data from the Socket Client . My question is , if I have this code that create 3 Receive Async Call : ...
1
by: John Sheppard | last post by:
Thanks to everyone that responded to my previous Socket Programming question. Now I have run into some behavior that I don't quite understand. Programming environment. VS.NET 2003, C#, Windows...
5
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...
5
by: Justin Creasy | last post by:
If this is the wrong group for this posting please let me know and I'll move it. I have an application that has an ArrayList of sockets to clients. For standard one-to-one messages my...
2
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...
14
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I have a sync socket application. The client is blocked with Socket.Receive(...) in a thread, another thread calls Socket.Close(). This unblock the blocked thread. But the socket server is still...
3
by: Stuart | last post by:
I am in the process of teaching myself socket programming. I am "playing around" with some simple echo server-client programs for m the book TCP/IP Sockets in C. The Server program is: ...
1
by: =?Utf-8?B?UmFqbmk=?= | last post by:
Dear Sir/Mam, I have written a server code using the Windows Socket API's. Wherein I have created the socket and bound it to a particular IP address and port number. Later I have made the socket...
4
by: danomano | last post by:
The code below works fine. As of now I just set the buffer length real big and all is good. However, I hate the impreciseness of that. So my question in general is how do I know what length to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.