473,378 Members | 1,142 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,378 software developers and data experts.

Net.Sockets endless block

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/de...tclasstopic.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)

Nov 15 '05 #1
4 2393
Michael Evanchik wrote:

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/de...classtopic.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();
Nov 15 '05 #2
Michael Evanchik <an*******@discussions.microsoft.com> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3
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.Environme nt.NewLine
headerz += postdata+System.Environment.NewLine+System.Environ ment.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
Nov 15 '05 #4
Michael Evanchik <an*******@discussions.microsoft.com> wrote:

<snip>
headerz += "Connection:
Keep-Alive"+System.Environment.NewLine+System.Environme nt.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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Tero Saarni | last post by:
Hi, I have several threads communicating with each other using events stored in Queues. Threads block on Queue.get() until somebody publishes an event in thread's event queue. I need to add...
4
by: Julia Goolia | last post by:
hello, i read that it is bad to use threads with tkinter. so my question is how does one create a gui program with sockets? at one point you have to call mainloop() which does not return. ...
3
by: David | last post by:
Hi, Ive been trying to work this out for the past 2 days now and im not getting anywhere fast. The problem i have is that i am using Asynchronous sockets to create a Socket Client library....
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
10
by: Alejandro Castañaza | last post by:
Hi. I'm writing a program, and I need to send confidential data through the network, so I decided to use encryption, using the System.Security.Cryptography namespace. I'm using the sockets...
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
3
by: J C | last post by:
Hi, I'm using UDPClient to make a simple DNS server. I notice that intermittently and unpredictibly I get: Unhandled Exception: System.Net.Sockets.SocketException: An existing connection...
5
by: Dan Ritchie | last post by:
I've got a client/server app that I used to send large amounts of data via UDP to the client. We use it in various scenarios, one of which includes rendering a media file on the client as it is...
6
by: 7stud | last post by:
My question pertains to this example: #!/usr/bin/env python import socket, sys, time host = sys.argv textport = sys.argv s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.