public void Listen() { try { var ipHostInfo = Dns.GetHostEntry(Config.Host); var ipAddress = ipHostInfo.AddressList .First(ip => ip.AddressFamily == AddressFamily.InterNetwork); var localEndPoint = new IPEndPoint(ipAddress, Config.Port); var listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); listener.Bind(localEndPoint); listener.Listen(100); while (true) { AllDone.Reset(); listener.BeginAccept( AcceptCallback, listener); AllDone.WaitOne(); } } catch (Exception e) { Abort(null, e); } }
public object Cmd(ProtoCommand cmd, string uri, long start, long len, string file = "") { if (file == "") { file = uri; } var state = new AgentState(Config, Fs) { FileName = file, Gram = new ProtoGram(cmd, start, len, uri) }; try { var ipHostInfo = Dns.GetHostEntry(Config.Host); var ipAddress = ipHostInfo.AddressList .First(ip => ip.AddressFamily == AddressFamily.InterNetwork); var remoteEp = new IPEndPoint(ipAddress, Config.Port); var client = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); state.WorkSocket = client; client.BeginConnect(remoteEp, ConnectCallback, state); ConnectDone.WaitOne(); Send(state); SendDone.WaitOne(); Receive(state); AllDone.WaitOne(); if (state.Gram.Status == ProtoStatus.Success) { return(Complete(state)); } else { throw new ApplicationException(state.Url); } } catch (Exception e) { Abort(state, e); return(null); } }
public static void StartListening() { // Establish the local endpoint for the socket. // The DNS name of the computer // running the listener is "host.contoso.com". IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, Port); // Create a TCP/IP socket. Socket listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint and listen for incoming connections. try { listener.Bind(localEndPoint); listener.Listen(100); Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Async Socket Server ONLINE at " + ipAddress.ToString() + ":" + Port, ConsoleColor.Green); while (true) { // Set the event to nonsignaled state. AllDone.Reset(); // Start an asynchronous socket to listen for connections. Sistem.printF(Sistem.GetLogTag(Sistem.EnumLogTags.SERVER) + "Waiting for a connection...", ConsoleColor.Gray); listener.BeginAccept(new AsyncCallback(AcceptCallback), listener); // Wait until a connection is made before continuing. AllDone.WaitOne(); } } catch (Exception e) { Sistem.printF(e.ToString(), ConsoleColor.Red); } Sistem.printF("\nPress ENTER to continue...", ConsoleColor.Gray, false, false, true, true, true); }