473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Nov 13 '05 #1
4 16798
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
14
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.