By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,330 Members | 1,277 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,330 IT Pros & Developers. It's quick & easy.

Non-blocking socket question

P: n/a
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

Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/de...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/de...us/cpguide/htm
l/cpconusingnon-blockingclientsocket.asp?frame=true

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

Nov 13 '05 #2

P: n/a
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
Nov 13 '05 #3

P: n/a
On Wed, 9 Jul 2003 08:26:58 -0400, "Joe Kinsella" <jk*******@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
Nov 13 '05 #4

P: n/a
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.

Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.