I would never claim to know what I am doing wrt to sockets (I barely
understand them) but I got the c# loop from:
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx
Where they write:
...
// Send request to the server.
s.Send(bytesSent, bytesSent.Length, 0);
// Receive the server home page content.
int bytes = 0;
string page = "Default HTML page on " + server + ":\r\n";
// The following will block until te page is transmitted.
do {
bytes = s.Receive(bytesReceived, bytesReceived.Length, 0);
page = page + Encoding.ASCII.GetString(bytesReceived, 0, bytes);
}
while (bytes > 0);
return page;
...
But it only works for me with the loop commented out. It works okay
actually, but I will follow Bill's advice and use the
~system/examples/socket which looks slightly ( :-) ) better than mine.
2010/1/3 Raul Miller <[email protected]>:
> On Sat, Jan 2, 2010 at 9:31 AM, Matthew Brand <[email protected]>
> wrote:
>> int bytes = 0;
>> // do {
>> try {
>> bytes = JSocket.Receive(buffer,
>> buffer.Length, 0);
>> reply = reply +
>> System.Text.Encoding.ASCII.GetString(buffer, 0, bytes);
>> // Print("reply was:" + reply);
>> }
>> catch (System.Net.Sockets.SocketException ex)
>> {
>> Print("Socket exception on read:" +
>> ex.SocketErrorCode);
>> }
>>
>> // } while (bytes > 0); Loop causes a hang. Do not know why!
>
> I did not look to see if you had this socket configured for blocking
> or non-blocking reads, but either way this loop should never
> exit. Here's a quote from the relevant documentation:
>
> If no data is available for reading, the Receive method will block until
> data is available, unless a time-out value was set by using
> Socket.ReceiveTimeout. If the time-out value was exceeded, the
> Receive call throws a SocketException. If you are in non-blocking
> mode, and there is no data available in the in the protocol stack
> buffer, the Receive method will complete immediately and throw a
> SocketException. You can use the Available property to determine if
> data is available for reading. When Available is non-zero, retry the
> receive operation.
>
> Normally, the .Receive() method would never exit. But if you are
> non-blocking, your "bytes" variable will not be updated.
>
> --
> Raul
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
--
"There is a 75 per cent chance that Al Gore, during the summer months,
could be completely fact-free within five to seven years.”
http://www.youtube.com/watch?v=X-5XwlcBqF0&feature=player_embedded
http://www.youtube.com/watch?v=t8O-E_GN0Kg&feature=player_embedded
Climate Scientists predicted UK snowfall thing of the past 10 years ago:
http://www.independent.co.uk/environment/snowfalls-are-now-just-a-thing-of-the-past-724017.html
Yet here we are, covered in snow!
Say no to fascism! Say no to the Carbon tax!
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm