The following code behaves differently from what I would expect:

socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
System.Net.Sockets.ProtocolType.Tcp);
socket.Blocking = false;
socket.Connect(ipe);
isConnected = socket.Poll(30*1000000, SelectMode.SelectWrite);

I would have expected that once I set the socket to non-blocking, the
connect would return immediately and the poll would wait up to 30 seconds
for the connect to succeed.

Instead, what I see is that after 10 or so seconds, the Connect() throws a
SocketException ("A non-blocking socket operation could not be completed
immediately").

Am I misunderstanding the documentation? Help would be appreciated.

Joe

RE: Non-blocking socket question by timhuang

timhuang
Wed Jul 09 02:26:43 CDT 2003

Hi,

1. "A non-blocking socket operation could not be completed immediately" is
the WSAEWOULDBLOCK error. It is normal for WSAEWOULDBLOCK to be reported as
the result from calling connect on a non-blocking socket, since some time
must elapse for the connection to be established. For detailed information,
please refer to MSDN on "connect" at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/win
sock/connect_2.asp

2. In .NET, you can use asynchronous socket calls alternatively. Please
refer to MSDN documentation on Socket.BeginConnect and Socket.EndConnect
methods. I believe the following articles and samples are very helpful:

Using an Asynchronous Client Socket
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconusingnon-blockingclientsocket.asp?frame=true

Using an Asynchronous Server Socket
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconusingnon-blockingserversocket.asp?frame=true

Please feel free to let me know if you have any problems or concens.

Have a nice day!

Regards,
HuangTM
This posting is provided "AS IS" with no warranties, and confers no rights.


Re: Non-blocking socket question by Joe

Joe
Wed Jul 09 07:26:58 CDT 2003

This is very helpful. If you don't mind, I do however, have a couple more
questions. I chose not the use the asynchronous calls since it requires a
callback, which adds unneeded complexity to what is otherwise a very simple
problem. If I was writing directly to winsock, I would implement it roughly
as I outlined in my post. I have two concerns with doing this in .Net: 1)
the Connect() call does not return immediately but inexplicably seems to
take several seconds, and 2) I'm concerned about the overhead of relying on
a SocketException being thrown then caught for each socket connection I
make.

If you have advice, it would be appreciated.

Joe



Re: Non-blocking socket question by zane

zane
Wed Jul 09 10:55:36 CDT 2003

On Wed, 9 Jul 2003 08:26:58 -0400, "Joe Kinsella" <jkinsella@silverbacktech.com>
wrote:

>2) I'm concerned about the overhead of relying on
>a SocketException being thrown then caught for each socket connection I
>make.

How many connections do you intend to establish each second?


--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com