Net.Sockets endless block

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

According to code i got right of MSDN here is a snip where its shown how to recieve data. The problem is this hangs FOREVER for me in debug mode. After the last packet comes in s.Recieve line never returns. I have subsequently had to add if (bytes < 256) break; But this isnt right. The last packet could be 256 exactly and freeze again

http://msdn.microsoft.com/library/d...html/frlrfsystemnetsocketssocketclasstopic.as

do

bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); //line freezes for me on the last run if there is no more dat
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes)

while (bytes > 0)
 
Michael said:
According to code i got right of MSDN here is a snip where its shown how to recieve data. The problem is this hangs FOREVER for me in debug mode. After the last packet comes in s.Recieve line never returns. I have subsequently had to add if (bytes < 256) break; But this isnt right. The last packet could be 256 exactly and freeze again.

http://msdn.microsoft.com/library/d...tml/frlrfsystemnetsocketssocketclasstopic.asp

do
{
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0); //line freezes for me on the last run if there is no more data
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);

Sounds like you are using a blocking socket. Either change to non-blocking
socket (s.Blocking = false) or use s.Available() to determine if more data
exists before calling s.Receive();
 
Michael Evanchik said:
Thanks Julie,

Be nice if microsofts examples worked!

They do - but only if the server closes the connection at the end. What
are you actually connecting to in this case?
 
I was connecting to IIS

string headerz=""
headerz = "POST /"+urlz+" HTTP/1.1"+System.Environment.NewLine
headerz += "Accept: */*"+System.Environment.NewLine
headerz += "Content-Type: application/x-www-form-urlencoded"+System.Environment.NewLine
headerz += "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; YComp 5.0.2.6; .NET CLR 1.0.3705; .NET CLR 1.1.4322)"+System.Environment.NewLine
headerz += "Host: "+serverz+System.Environment.NewLine
headerz += "Content-Length: "+postdata.Length+System.Environment.NewLine
headerz += "Connection: Keep-Alive"+System.Environment.NewLine+System.Environment.NewLine
headerz += postdata+System.Environment.NewLine+System.Environment.NewLine

Byte[] bytesSent = Encoding.ASCII.GetBytes(headerz)
Byte[] bytesReceived = new Byte[256]

s.Send(bytesSent, bytesSent.Length, 0);

int bytes = 0
string page = ""

while (s.Available==0

bytes = s.Receive(bytesReceived, bytesReceived.Length, 0)
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes)


seems be work fin

Mike
 
headerz += "Connection:
Keep-Alive"+System.Environment.NewLine+System.Environment.NewLine;

This line is the problem: you've told IIS to keep the connection open,
which means when it's finished sending its response, it's waiting for
you to send the next request... so yes, it will block, as it's meant
to.

Either don't use keepalive, or take note of the ContentLength returned
to you and stop trying to read after you've read that many bytes.

However, if you're really just wanting to make web requests, I suggest
you use the framework's built-in libraries for that purpose.

Note also that your way of building up the returned page is very
inefficient - you should use a StringBuilder instead of string
concatenation, as otherwise you'll be creating a new string for every
iteration of the loop, and copying all the previous data each time.
 
Back
Top