static void Main(string[] args) { CommandLineArguments cla = CommandLineArguments.Parse(args); if (cla == null || (!cla.Sender && !cla.Receiver)) { CommandLineArguments.ShowUsage(); Environment.Exit(1); return; } else if (cla.Sender && String.IsNullOrEmpty(cla.File)) { CommandLineArguments.ShowUsage(); Environment.Exit(1); return; } if (cla.File != null && (!(File.Exists(cla.File)))) { Console.Error.WriteLine($"File {cla.File} does not exist"); Environment.Exit(2); return; } string remoteIp; int remotePort; Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); try { if (cla.LocalPort != -1) { Console.WriteLine("Using local port: {0}", cla.LocalPort); } else { cla.LocalPort = 0; } socket.Bind(new IPEndPoint(IPAddress.Any, cla.LocalPort)); P2pEndPoint p2pEndPoint = GetExternalEndPoint(socket); if (p2pEndPoint == null) { return; } Console.WriteLine("Tell this to your peer: {0}", p2pEndPoint.External.ToString()); Console.WriteLine(); Console.WriteLine(); Console.Write("Enter the ip:port of your peer: "); string peer = Console.ReadLine(); if (string.IsNullOrEmpty(peer)) { Console.WriteLine("Invalid ip:port entered"); return; } // try again to connect to external to "reopen" port GetExternalEndPoint(socket); ParseRemoteAddr(peer, out remoteIp, out remotePort); UdtSocket connection = PeerConnect(socket, remoteIp, remotePort); if (connection == null) { Console.Error.WriteLine("Failed to establish P2P conn to {0}", remoteIp); Environment.Exit(3); return; } try { if (cla.Sender) { Sender.Run(connection, cla.File, cla.Verbose); return; } Receiver.Run(connection); } finally { connection.Close(); } } finally { socket.Close(); } }
static void Main(string[] args) { CommandLineArguments cla = CommandLineArguments.Parse(args); if (cla == null || (!cla.Sender && !cla.Receiver)) { CommandLineArguments.ShowUsage(); return; } string remoteIp; int remotePort; Socket socket = new Socket( AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); try { if (cla.LocalPort != -1) { Console.WriteLine("Using local port: {0}", cla.LocalPort); } else { cla.LocalPort = 0; } socket.Bind(new IPEndPoint(IPAddress.Any, cla.LocalPort)); P2pEndPoint p2pEndPoint = GetExternalEndPoint(socket); Console.WriteLine("p2pEndPoint external=" + p2pEndPoint.External); Console.WriteLine("p2pEndPoint internal=" + p2pEndPoint.Internal); if (p2pEndPoint == null) { return; } Console.WriteLine("Tell this to your peer: {0}", p2pEndPoint.External.ToString()); Console.WriteLine(); Console.WriteLine(); Console.Write("Enter the ip:port of your peer: "); string peer = Console.ReadLine(); if (string.IsNullOrEmpty(peer)) { Console.WriteLine("Invalid ip:port entered"); return; } // try again to connect to external to "reopen" port GetExternalEndPoint(socket); ParseRemoteAddr(peer, out remoteIp, out remotePort); PseudoTcpSocket connection = PeerConnect( p2pEndPoint.External.Address.ToString(), p2pEndPoint.External.Port, socket, remoteIp, remotePort, cla); Console.WriteLine("Called PeerConnect"); if (connection == null) { Console.WriteLine("Failed to establish P2P conn to {0}", remoteIp); return; } try { if (args[0] == "sender") { Sender.Run(connection, cla.File, cla.Verbose); } else { Receiver.Run(connection); } } finally { connection.Close(false); } } finally { socket.Close(); } }