//static Dictionary<ushort, TcpServer> servers = new Dictionary<ushort, TcpServer>(); static public void Start(int local_port, IPAddress destination_ip) { if (!NetworkRoutines.IsNetworkAvailable()) throw new Exception("No network available."); IPAddress ipAddress = NetworkRoutines.GetLocalIpForDestination(destination_ip); if (local_port == LocalPort && ipAddress.Equals(LocalIp)) return; Stop(); Log.Main.Inform("Starting TCP listener on " + local_port + " for " + destination_ip); //listeningSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //listeningSocket.Bind(localEndPoint); ////listeningSocket.Listen(100); server = new TcpListener(ipAddress, local_port); server.Start(); thread = ThreadRoutines.StartTry(run, (Exception e) => { if (e is SocketException) { Log.Main.Warning(e); } else { Log.Main.Error(e); CisteraScreenCaptureService.ExposedEvents.UiMessage.Error(Log.GetExceptionMessage(e)); } Stop(); }); }
void connect_socket() { if (socket != null) { if (socket.IsConnectionAlive()) { return; } try { socket.Close(); } finally { socket = null; } if (stream != null) { stream.Close(); } } IPAddress ipAddress = NetworkRoutines.GetLocalIpForDestination(IPAddress.Parse("127.0.0.1")); //IPEndPoint localEndPoint = new IPEndPoint(ipAddress, int.Parse(localTcpPort.Text)); socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, true); //socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true); //_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 500) //_Socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, Timeout) //socket.Bind(localEndPoint); socket.Connect(remoteHost, int.Parse(remotePort)); stream = new NetworkStream(socket); ThreadRoutines.StartTry(() => { while (socket != null) { Thread.Sleep(5000); poll(); } }); }
void connect_socket() { if (socket == null) { IPAddress ipAddress = NetworkRoutines.GetLocalIpForDestination(IPAddress.Parse("127.0.0.1")); IPEndPoint localEndPoint = new IPEndPoint(ipAddress, int.Parse(localTcpPort.Text)); socket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); socket.Bind(localEndPoint); } if (!socket.Connected || !socket.Poll(1000, SelectMode.SelectWrite)) { socket.Connect(remoteHost, int.Parse(remotePort)); } if (stream != null) { stream.Dispose(); } stream = new NetworkStream(socket); }
//static Dictionary<ushort, TcpServer> servers = new Dictionary<ushort, TcpServer>(); static public void Start(int local_port, IPAddress destination_ip) { if (!NetworkRoutines.IsNetworkAvailable()) { throw new Exception("No network available."); } IPAddress ipAddress = NetworkRoutines.GetLocalIpForDestination(destination_ip); if (local_port == LocalPort && ipAddress.Equals(LocalIp)) { return; } Stop(); Log.Main.Inform("Starting TCP listener on " + local_port + " for " + destination_ip); //listeningSocket = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); //listeningSocket.Bind(localEndPoint); ////listeningSocket.Listen(100); server = new TcpListener(ipAddress, local_port); server.Start(); thread = ThreadRoutines.StartTry(run, (Exception e) => { if (e is SocketException) { Log.Main.Warning(e); } else { Log.Main.Error(e); InfoWindow.Create(Log.GetExceptionMessage(e), null, "OK", null, Settings.View.ErrorSoundFile, System.Windows.Media.Brushes.WhiteSmoke, System.Windows.Media.Brushes.Red); } Stop(); }); }