-
Notifications
You must be signed in to change notification settings - Fork 0
/
TcpClientWithTimeout.cs
58 lines (55 loc) · 1.89 KB
/
TcpClientWithTimeout.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Net.Sockets;
using System.Threading;
namespace Appboxstudios.ClipboardBroadcaster
{
public class TcpClientWithTimeout
{
protected string _hostname;
protected int _port;
protected int _timeout_milliseconds;
protected TcpClient connection;
protected bool connected;
protected Exception exception;
public TcpClientWithTimeout(string hostname, int port, int timeout_milliseconds)
{
_hostname = hostname;
_port = port;
_timeout_milliseconds = timeout_milliseconds;
}
public bool Connect(out TcpClient client)
{
// kick off the thread that tries to connect
connected = false;
exception = null;
Thread thread = new Thread(new ThreadStart(BeginConnect));
thread.IsBackground = true; // So that a failed connection attempt
// wont prevent the process from terminating while it does the long timeout
thread.Start();
// wait for either the timeout or the thread to finish
thread.Join(_timeout_milliseconds);
client = connection;
if (connected)
{
thread.Abort();
return connected;
}
return false;
}
protected void BeginConnect()
{
try
{
connection = new TcpClient();
connection.Connect(_hostname, _port);
// record that it succeeded, for the main thread to return to the caller
connected = true;
}
catch (Exception ex)
{
// record the exception for the main thread to re-throw back to the calling code
exception = ex;
}
}
}
}