473,385 Members | 1,341 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,385 software developers and data experts.

Connect a socket with timeout

How does one put a timeout on an attempt to connect a socket? There's no
timeout parameter to Socket.Connect(), and while there are socket options
for send and receive timeouts, there is none for connect timeout.
Apr 3 '06 #1
3 39498
Mike,

In order to do this, you will have to time the asynchronous event.
Basically, make a call to BeginConnect, and start a timer at the same time,
with an interval of your timeout.

In the timer function, set a flag indicating if the timer has fired.

Then, in the callback for the BeginConnect method, you check the flag.
If it is set, then you timed out, if not, then you haven't timed out.

You can then elaborate, by creating an Event that is set when the
timeout occurs, or the connection is created. Then, you have your thread
wait on both of those events. Depending on the event fired, you would then
know if there is a timeout, or if the connection succeeded.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Mike Schilling" <ap@newsgroup.nospam> wrote in message
news:Oy**************@TK2MSFTNGP15.phx.gbl...
How does one put a timeout on an attempt to connect a socket? There's no
timeout parameter to Socket.Connect(), and while there are socket options
for send and receive timeouts, there is none for connect timeout.

Apr 4 '06 #2

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u5**************@TK2MSFTNGP14.phx.gbl...
Mike,

In order to do this, you will have to time the asynchronous event.
Basically, make a call to BeginConnect, and start a timer at the same
time, with an interval of your timeout.

In the timer function, set a flag indicating if the timer has fired.

Then, in the callback for the BeginConnect method, you check the flag.
If it is set, then you timed out, if not, then you haven't timed out.
I thought about something like that, but couldn't find a way to abort the
connection attempt if the timer fires and it's still pending. EndConnect is
documented as synchronously completing the connection, and it's not clear
from the docs what Close or Shutdown would do.

You can then elaborate, by creating an Event that is set when the
timeout occurs, or the connection is created. Then, you have your thread
wait on both of those events. Depending on the event fired, you would
then know if there is a timeout, or if the connection succeeded.

Apr 4 '06 #3
Hi Mike,

The Close method will release all the resource on the Socket. Shutdown
method will disable the socket from sending and receiving. So, I suggest
you call Close method on the Socket to cancel connecting.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Apr 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Raja | last post by:
Hello Im trying to catch timeouts from a socket and my code looks like try: timeoutsocket.setDefaultSocketTimeout(10) s = timeoutsocket.timeoutsocket(timeoutsocket.AF_INET,...
1
by: Gareth Crispin | last post by:
Good afternoon, I'm rewriting a piece of perl code in C++ that simply checks availability of a port. It's fairly simple, and returns UP, DOWN or NODNS. This works great, and is just what I...
3
by: cocla | last post by:
I try to connect to server using following codes: (smart device application) Socket s=new Socket(); try { s.connect(host);//host is an object of IPEndPoint class } catch (SocketException) {
2
by: Ted Burhan | last post by:
I was wondering if there is a way to adjust the timeout period when doing Socket.Connect() I have been looking at the docs, found nothing
2
by: Ted Burhan | last post by:
I was wondering if there is a way to adjust the timeout period when doing Socket.Connect() I have been looking at the docs, found nothing
4
by: gaddamreddy | last post by:
Hai to all frns, Reqirement 1: 1.I need to send a Request to one server through UDP socket.at that time i have to start a timer (setted to 2 sec) if i wont get the Response/ACK from that...
0
by: mmcgee00 | last post by:
Hi, Currently, I am trying to get different service banner by connecting to different ports using python (code below). The versions I am working with are python 4.2.1 and fedora core 4. I am...
0
by: doc | last post by:
Hi Hard to know where to post this and I don't want to double post it - please move it if you need to, I am trying to connect a Flash client socket (XML) to a php socket server (using Flash 8 and...
3
by: doc | last post by:
What will a flash xml client socket connect to? I have a working php TCP/IP server socket bound to a port >1023 and the flash client will not even connect to it. I can connect to it with non-xml...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.