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





<script language=JavaScript src="http://bytes.com/casus/as_header.js"></script> <script language=JavaScript src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script> <script language=JavaScript1.1 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-8550604525820387&dt=1221025457570&lmt=1221025457&num_ads=2&output=js&correlator=1221025457460&channel=6808407789&url=http%3A%2F%2Fbytes.com%2Fforum%2Fthread213753.html&ad_type=text&image_size=300x250&feedback_link=on&ref=http%3A%2F%2Fwww.google.com.hk%2Fsearch%3Fhl%3Den%26q%3D.net%2Bsocket%2Bblocking%26meta%3D%26aq%3Df%26oq%3D&frm=0&cc=100&ga_vid=507024314293117000.1220343283&ga_sid=1221025458&ga_hid=1958459604&ga_fc=true&flash=9.0.124.0&u_h=1024&u_w=1280&u_ah=990&u_aw=1280&u_cd=32&u_tz=480&u_java=true"></script>
Source Code Reviews Without the Hassles. Download Free Trial Now!
On Your Mobile Devices - Win and BB Free Download
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" <jkinsella@silverbacktech.com>
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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值