public void Close() { if (!Closed && !Closing) { Closing = true; try { if (UdtConnection != null && !UdtConnection.IsDisposed) { UdtConnection.Close(); } if (ListeningSocket != null && !ListeningSocket.IsDisposed) { ListeningSocket.Close(); ListeningSocket = null; } } catch (Exception e) { Logger.Error("Failed to close socket", e); } Closing = false; Closed = true; Connected = false; } }
private void ShutdownSocket() { try { socket.Close(); socket.Dispose(); } catch (Exception ex) { LOGGER.Error("Socket close exception", ex); } }
static Udt.Socket PeerConnect(Socket socket, string remoteAddr, int remotePort) { bool bConnected = false; int retry = 0; Udt.Socket client = null; while (!bConnected) { try { DateTime now = InternetTime.Get(); int sleepTimeToSync = SleepTime(now); Console.WriteLine("[{0}] - Waiting {1} sec to sync with other peer", now.ToLongTimeString(), sleepTimeToSync); System.Threading.Thread.Sleep(sleepTimeToSync * 1000); GetExternalEndPoint(socket); if (client != null) { client.Close(); } client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream); client.SetSocketOption(Udt.SocketOptionName.Rendezvous, true); client.Bind(socket); Console.Write("\r{0} - Trying to connect to {1}:{2}. ", retry++, remoteAddr, remotePort); client.Connect(remoteAddr, remotePort); Console.WriteLine("Connected successfully to {0}:{1}", remoteAddr, remotePort); bConnected = true; } catch (Exception e) { Console.Write(e.Message.Replace(Environment.NewLine, ". ")); } } return(client); }
static int Main(string[] args) { if ((args.Length != 4) || (0 == int.Parse(args[1]))) { Console.WriteLine("Usage: ReceiveFile server_ip server_port remote_filename local_filename"); return(1); } try { using (Udt.Socket client = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { client.Connect(IPAddress.Parse(args[0]), int.Parse(args[1])); // Send name information of the requested file string name = args[2]; byte[] nameBytes = Encoding.UTF8.GetBytes(name); client.Send(BitConverter.GetBytes(nameBytes.Length), 0, sizeof(int)); client.Send(nameBytes); // Get size information long size; byte[] file = new byte[1024]; client.Receive(file, 0, sizeof(long)); size = BitConverter.ToInt64(file, 0); // Receive the file string localName = args[3]; client.ReceiveFile(localName, size); client.Close(); } Console.ReadKey(true); return(0); } catch (Exception ex) { Console.Error.WriteLine("Error receiving file: {0}", ex.Message); Console.ReadKey(true); return(2); } }
public void Get_State() { ManualResetEvent serverDoneEvent = new ManualResetEvent(false); ManualResetEvent clientDoneEvent = new ManualResetEvent(false); int port = _portNum++; var serverTask = Task.Factory.StartNew(() => { using (Udt.Socket server = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { server.Bind(IPAddress.Loopback, port); Assert.AreEqual(Udt.SocketState.Open, server.State); server.Listen(1); Assert.AreEqual(Udt.SocketState.Listening, server.State); using (Udt.Socket accept = server.Accept()) { Assert.AreEqual(Udt.SocketState.Open, server.State); serverDoneEvent.Set(); Assert.IsTrue(clientDoneEvent.WaitOne(1000)); } } }); using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { Assert.AreEqual(Udt.SocketState.Initial, socket.State); Assert.AreEqual(Udt.SocketState.Initial, socket.GetSocketOption(Udt.SocketOptionName.State)); AssertReadOnly(socket, Udt.SocketOptionName.State); socket.Connect(IPAddress.Loopback, port); Assert.AreEqual(Udt.SocketState.Connected, socket.State); serverDoneEvent.WaitOne(1000); clientDoneEvent.Set(); socket.Close(); Assert.AreEqual(Udt.SocketState.Closed, socket.State); Assert.AreEqual(Udt.SocketState.Closed, socket.GetSocketOption(Udt.SocketOptionName.State)); } }
static int Main(string[] args) { if ((1 < args.Length) || ((1 == args.Length) && (0 == int.Parse(args[0])))) { Console.WriteLine("Usage: SendFile [ServerPort]"); return 1; } try { using (Udt.Socket server = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { int port = 9000; if (1 == args.Length) port = int.Parse(args[0]); server.Bind(IPAddress.Any, port); Console.WriteLine("Server is ready at port: {0}", port); server.Listen(1); using (Udt.Socket client = server.Accept()) { server.Close(); // Receive file name from client byte[] file = new byte[1024]; int length; string name; client.Receive(file, 0, sizeof(int)); length = BitConverter.ToInt32(file, 0); client.Receive(file, 0, length); name = Encoding.UTF8.GetString(file, 0, length); // Send file size information client.Send(BitConverter.GetBytes(new FileInfo(name).Length), 0, sizeof(long)); Udt.TraceInfo trace = client.GetPerformanceInfo(); // Send the file client.SendFile(name); trace = client.GetPerformanceInfo(); PrintProps("Total", trace.Total); PrintProps("Local", trace.Local); PrintProps("Probe", trace.Probe); client.Close(); } } Console.ReadKey(true); return 0; } catch (Exception ex) { Console.Error.WriteLine("Error sending file: {0}", ex.Message); Console.ReadKey(true); return 2; } }
public override void Close() { udtSocket.Close(); }
static int Main(string[] args) { if ((1 < args.Length) || ((1 == args.Length) && (0 == int.Parse(args[0])))) { Console.WriteLine("Usage: SendFile [ServerPort]"); return(1); } try { using (Udt.Socket server = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { int port = 9000; if (1 == args.Length) { port = int.Parse(args[0]); } server.Bind(IPAddress.Any, port); Console.WriteLine("Server is ready at port: {0}", port); server.Listen(1); using (Udt.Socket client = server.Accept()) { server.Close(); // Receive file name from client byte[] file = new byte[1024]; int length; string name; client.Receive(file, 0, sizeof(int)); length = BitConverter.ToInt32(file, 0); client.Receive(file, 0, length); name = Encoding.UTF8.GetString(file, 0, length); // Send file size information client.Send(BitConverter.GetBytes(new FileInfo(name).Length), 0, sizeof(long)); Udt.TraceInfo trace = client.GetPerformanceInfo(); // Send the file client.SendFile(name); trace = client.GetPerformanceInfo(); PrintProps("Total", trace.Total); PrintProps("Local", trace.Local); PrintProps("Probe", trace.Probe); client.Close(); } } Console.ReadKey(true); return(0); } catch (Exception ex) { Console.Error.WriteLine("Error sending file: {0}", ex.Message); Console.ReadKey(true); return(2); } }
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); 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); Udt.Socket connection = PeerConnect(socket, remoteIp, remotePort); 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); return; } Receiver.Run(connection); } finally { connection.Close(); } } finally { socket.Close(); } }
public void Get_State() { ManualResetEvent serverDoneEvent = new ManualResetEvent(false); ManualResetEvent clientDoneEvent = new ManualResetEvent(false); int port = _portNum++; var serverTask = Task.Factory.StartNew(() => { using (Udt.Socket server = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { server.Bind(IPAddress.Loopback, port); Assert.AreEqual(Udt.SocketState.Open, server.State); server.Listen(1); Assert.AreEqual(Udt.SocketState.Listening, server.State); using (Udt.Socket accept = server.Accept()) { Assert.AreEqual(Udt.SocketState.Open, server.State); serverDoneEvent.Set(); Assert.IsTrue(clientDoneEvent.WaitOne(1000)); } } }); using (Udt.Socket socket = new Udt.Socket(AddressFamily.InterNetwork, SocketType.Stream)) { Assert.AreEqual(Udt.SocketState.Initial, socket.State); Assert.AreEqual(Udt.SocketState.Initial, socket.GetSocketOption(Udt.SocketOptionName.State)); AssertReadOnly(socket, Udt.SocketOptionName.State); socket.Connect(IPAddress.Loopback, port); Assert.AreEqual(Udt.SocketState.Connected, socket.State); serverDoneEvent.WaitOne(1000); clientDoneEvent.Set(); socket.Close(); Assert.AreEqual(Udt.SocketState.Closed, socket.State); Assert.AreEqual(Udt.SocketState.Closed, socket.GetSocketOption(Udt.SocketOptionName.State)); } }