-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathClient.cs
More file actions
121 lines (93 loc) · 3.73 KB
/
Client.cs
File metadata and controls
121 lines (93 loc) · 3.73 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Udt;
namespace udtholepunch
{
class Client
{
public static void Run(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("Usage: {0} client local_port server server_port [client|server]",
System.AppDomain.CurrentDomain.FriendlyName);
return;
}
Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
client.ReuseAddress = true;
client.Bind(IPAddress.Any, int.Parse(args[0]));
IPAddress serverAddress;
if (!IPAddress.TryParse(args[1], out serverAddress))
{
Console.WriteLine("Error trying to parse {0}", args[1]);
return;
}
client.Connect(serverAddress, int.Parse(args[2]));
int peerPort;
IPAddress peerAddress;
// recv the other peer info
using (Udt.NetworkStream st = new Udt.NetworkStream(client, false))
using (BinaryReader reader = new BinaryReader(st))
{
int len = reader.ReadInt32();
byte[] addr = reader.ReadBytes(len);
peerPort = reader.ReadInt32();
peerAddress = new IPAddress(addr);
Console.WriteLine("Received peer address = {0}:{1}",
peerAddress, peerPort);
}
bool bConnected = false;
int retry = 0;
while (!bConnected)
try
{
client.Close();
client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
client.ReuseAddress = true;
client.SetSocketOption(Udt.SocketOptionName.Rendezvous, true);
client.Bind(IPAddress.Any, int.Parse(args[0]));
Console.WriteLine("{0} - Trying to connect to {1}:{2}. ",
retry++, peerAddress, peerPort);
client.Connect(peerAddress, peerPort);
Console.WriteLine("Connected successfully to {0}:{1}",
peerAddress, peerPort);
bConnected = true;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
if (args[3] == "client")
{
using (Udt.NetworkStream st = new Udt.NetworkStream(client))
using (BinaryReader reader = new BinaryReader(st))
{
while (true)
{
Console.WriteLine(reader.ReadString());
}
}
}
else
{
using (Udt.NetworkStream st = new Udt.NetworkStream(client))
using (BinaryWriter writer = new BinaryWriter(st))
{
int last = Environment.TickCount;
while (!Console.KeyAvailable)
{
if (Environment.TickCount - last < 1000)
continue;
writer.Write(string.Format("[{0}] my local time is {1}",
Environment.MachineName, DateTime.Now.ToLongTimeString()));
last = Environment.TickCount;
TraceInfo traceInfo = client.GetPerformanceInfo();
Console.WriteLine("Bandwith Mbps {0}", traceInfo.Probe.BandwidthMbps);
}
}
}
}
}
}