Non-blocking socket question

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





Ads by Google
Source Code Review Tool
Source Code Reviews Without the Hassles. Download Free Trial Now!
www.SmartBear.com/30_Day_Trial
Block SMS Messages
On Your Mobile Devices - Win and BB Free Download
textgard.com
Tian Min Huang's Avatar
Tian Min Huang
Guest
n/a Posts
November 13th, 2005
08:06 PM
# 2

Re: Non-blocking socket question
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/d...-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/d...-us/cpguide/htm
l/cpconusingnon-blockingclientsocket.asp?frame=true

Using an Asynchronous Server Socket
http://msdn.microsoft.com/library/d...-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.


Joe Kinsella's Avatar
Joe Kinsella
Guest
n/a Posts
November 13th, 2005
08:07 PM
# 3

Re: Non-blocking socket question
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



Zane Thomas [.NET/C# MVP]'s Avatar
Zane Thomas [.NET/C# MVP]
Guest
n/a Posts
November 13th, 2005
08:08 PM
# 4

Re: Non-blocking socket question
On Wed, 9 Jul 2003 08:26:58 -0400, "Joe Kinsella"
wrote:
[color=blue]
>2) I'm concerned about the overhead of relying on
>a SocketException being thrown then caught for each socket connection I
>make.[/color]

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

Tian Min Huang's Avatar
Tian Min Huang
Guest
n/a Posts
November 13th, 2005
08:13 PM
# 5

Re: Non-blocking socket question
Hi Joe,

Thanks for your response.

1) the Connect() call does not return immediately but inexplicably seems to
take several seconds

I did some test, it will prompt the exception immediately after issuing the
Connect() call on my side. In addition, I stil recommend you use the
asynchronous socket calls.

I look forward to your feedback.

Have a nice day!

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

你可能感兴趣的:(.NET,园地)