Connection timeout of tcpclient. Connect Function

Source: Internet
Author: User
Tags getstream

Original problem:

Http://topic.csdn.net/t/20060616/15/4825920.html

 

Call the tcpclient. Connect function to connect to other machines. I tested the program on a machine. For a machine that cannot be connected at all (physical connection fails), the function took about 5 seconds to return and caught a socketexception exception. When I tested on another machine, the connect function took about 26 seconds to return. Is there any way to set the connection timeout time of the connect function? If the connection has not been connected for a certain period of time, it will be returned.

 

Solution:

Wrote a help class and used the thread pool

Class tcpclientconnector <br/>{< br/> /// <summary> <br/> // try to connect to the specified port on the specified host within the specified time. <Br/> /// </Summary> <br/> /// <Param name = "hostname"> the DNS name of the remote host to be connected. </Param> <br/> // <Param name = "Port"> the port number of the remote host to connect. </Param> <br/> // <Param name = "millisecondstimeout"> the number of milliseconds to wait, or-1 indicates an indefinite wait. </Param> <br/> // <returns> A connected tcpclient instance. </Returns> <br/> /// <remarks> This method may throw an exception and one of tcpclient constructor overloading <br/> // public tcpclient (string, INT) is the same. If the specified wait time is negative and not equal to <br/> //-1, an argumentoutofrangeexception is thrown. </Remarks> <br/> Public static tcpclient connect (string hostname, int port, int millisecondstimeout) <br/>{< br/> connectorstate cs = new connectorstate (); <br/> CS. hostname = hostname; <br/> CS. port = port; <br/> threadpool. queueuserworkitem (New waitcallback (connectthreaded), CS); <br/> If (CS. completed. waitone (millisecondstimeout, false) <br/>{< br/> If (CS. tcpclient! = NULL) return CS. tcpclient; <br/> throw CS. exception; <br/>}< br/> else <br/>{< br/> CS. abort (); <br/> throw new socketexception (11001 ); // cannot connect <br/>}</P> <p> Private Static void connectthreaded (object state) <br/> {<br/> connectorstate cs = (connectorstate) State; <br/> CS. thread = thread. currentthread; <br/> try <br/> {<br/> tcpclient Tc = new tcpclient (CS. hostname, CS. por T); <br/> If (CS. aborted) <br/>{< br/> try {TC. getstream (). close () ;}< br/> catch {}< br/> try {TC. close () ;}< br/> catch {}< br/>}< br/> else <br/>{< br/> CS. tcpclient = tc; <br/> CS. completed. set (); <br/>}< br/> catch (exception e) <br/>{< br/> CS. exception = E; <br/> CS. completed. set (); <br/>}</P> <p> private class connectorstate <br/>{< br/> Public String hostname; <B R/> Public int port; <br/> Public volatile thread; <br/> Public readonly manualresetevent completed = new manualresetevent (false); <br/> Public volatile tcpclient; <br/> Public volatile exception; <br/> Public volatile bool aborted; <br/> Public void abort () <br/>{< br/> If (aborted! = True) <br/>{< br/> aborted = true; <br/> try {thread. abort () ;}< br/> catch {}< br/>}< br/>

======================================
Usage example:

Try <br/> {<br/> console. writeline ("connecting to nonexistenthost... "); <br/> tcpclient Tc = tcpclientconne. connect ("nonexistent", 80,100 0); <br/> console. writeline ("Returned"); <br/> try {TC. getstream (). close () ;}< br/> catch {}< br/> try {TC. close () ;}< br/> catch {}< br/>}< br/> catch (exception e) <br/>{< br/> console. writeline ("exception:" + E. message); <br/>}< br/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.