-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathServer.cs
60 lines (46 loc) · 1.72 KB
/
Server.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
59
60
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using Udt;
namespace udtholepunch
{
class Server
{
public static void Run(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: {0} server port",
System.AppDomain.CurrentDomain.FriendlyName);
return;
}
Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream);
socket.Bind(IPAddress.Any, int.Parse(args[0]));
socket.Listen(1);
while (true)
{
Udt.Socket client0 = socket.Accept();
Console.WriteLine("First connection accepted from {0}",
client0.RemoteEndPoint.ToString());
Udt.Socket client1 = socket.Accept();
Console.WriteLine("Second connection accepted from {0}",
client1.RemoteEndPoint.ToString());
IPEndPoint client1EndPoint = client1.RemoteEndPoint;
SendAddressTo(client0.RemoteEndPoint, client1);
SendAddressTo(client1EndPoint, client0);
}
}
static void SendAddressTo(IPEndPoint endPoint, Udt.Socket socket)
{
using (Udt.NetworkStream st = new Udt.NetworkStream(socket))
using (BinaryWriter writer = new BinaryWriter(st))
{
byte[] addrBytes = endPoint.Address.GetAddressBytes();
writer.Write((int)addrBytes.Length);
writer.Write(addrBytes);
writer.Write((int)endPoint.Port);
}
}
}
}