//static void SetPortForward(int port) //{ // try // { // var discoverer = new NatDiscoverer(); // var cts = new CancellationTokenSource(10000); // var device = discoverer.DiscoverDeviceAsync(PortMapper.Upnp, cts).Result; // device.CreatePortMapAsync(new Mapping(Protocol.Udp, 5001, port, "Portunus")).Wait(); // } // catch // { // Console.Write(' '); // } //} static async void StartTCPClient() { TCPSocket socket = new TCPSocket(); new Thread(() => { socket.StartListening(config.Client.Port); }); socket.Send(config.Servers[0].Address, config.Servers[0].Port, "Hello Server 1, this is the client"); while (true) { try { socket.Send(config.Client.Address, config.Client.Port, "Hello Client, this is the host"); Thread.Sleep(500); } catch (Exception ex) { Console.WriteLine(ex.Message); } } }
/* * const int Server_Port = 5001; * const string SERVER_IP = "69.6.36.79"; * * const int CLIENT_PORT_NO = 5001; * const string CLIENT_SERVER_IP = "84.255.45.106"; * * const int Source_PORT_NO = 5001; * const string Source_SERVER_IP = "18.223.23.99"; */ static async void StartTCPClient() { TCPSocket socket = new TCPSocket(); socket.StartListening(config.Servers[0].Port); UDPSocket sock = new UDPSocket(); await sock.StartListening(config.Servers[0].Port); }
async Task Start() { try { TCPSocket.ToDo t = new CommandManager().Execute; await socket.StartListening(t); } catch (Exception ex) { Console.WriteLine(ex.Message + "\n" + ex.StackTrace); } }
/* * const int Server_Port = 5001; * const string SERVER_IP = "69.6.36.79"; * * const int CLIENT_PORT_NO = 5001; * const string CLIENT_SERVER_IP = "84.255.45.106"; * * const int Source_PORT_NO = 5001; * const string Source_SERVER_IP = "18.223.23.99"; */ static void StartTCPClient() { TCPSocket socket = new TCPSocket(); socket.StartListening(config.Servers[0].Port); }
public void StartListening(int incomingBuffer = 1000) { String prt = m_port.ToString(); SByte[] sprt = new SByte[prt.Length]; for (int i = 0; i < prt.Length; ++i) { sprt[i] = (sbyte)prt[i]; } unsafe { fixed(SByte *ppp = sprt) { m_sock = new TCPSocket(ppp); } } ThreadPool.SetMaxThreads(40, 40); ThreadPool.SetMinThreads(40, 40); if (false == m_sock.InError()) { m_sock.StartListening(); } while (m_sock.Accept()) { if (true == m_sock.InError()) { continue; } UInt32 handler = m_sock.ReturnClientSocket(); var ThreadMain = System.Diagnostics.Stopwatch.StartNew(); //ThreadPool.QueueUserWorkItem(Socket.StarterFunction, handler); ThreadPool.QueueUserWorkItem(HttpStreamReader.ListenSocketHandler, handler); ThreadMain.Stop(); } // Create a TCP/IP socket. //m_listeningSocket = new SystemSocket(m_ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections. /*try * { * m_listeningSocket.Bind(m_ipEndPoint); * m_listeningSocket.Listen(incomingBuffer); * * ThreadPool.SetMaxThreads(40, 40); * ThreadPool.SetMinThreads(40, 40); * * // Start listening for connections. * while (true) * { * // Program is suspended while waiting for an incoming connection. * SystemSocket handler = m_listeningSocket.Accept(); * System.Console.WriteLine(handler.Handle); * * var ThreadMain = System.Diagnostics.Stopwatch.StartNew(); * //ThreadPool.QueueUserWorkItem(Socket.StarterFunction, handler); * ThreadPool.QueueUserWorkItem(HttpStreamReader.ListenSocketHandler, handler); * ThreadMain.Stop(); * System.Console.WriteLine("Request Time " + ThreadMain.ElapsedMilliseconds); * * //log * //EndPoint endpt = handler.LocalEndPoint; * //long result = IPGlobalProperties.GetIPGlobalProperties() * // .GetTcpIPv4Statistics() * // .CurrentConnections; * //System.Console.WriteLine(result); * * } * * } * catch (Exception e) * { * Console.WriteLine(e.ToString()); * Environment.Exit(-1); * }*/ }