public void Start(int port, string savePath) { var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); var ipAddress = ipHostInfo.AddressList[0]; var listener = new TcpListener(IPAddress.Any, port); listener.Start(); RaiseNotification?.Invoke($"File Socket Server listening on Port:{port}"); while (true) { using (var client = listener.AcceptTcpClient()) using (var stream = client.GetStream()) { RaiseAction?.Invoke(ReadAllBytes(stream)); } } }
public void Start(int port) { // Establish the local endpoint for the socket. // The DNS name of the computer IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName()); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint localEndPoint = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. System.Net.Sockets.Socket listener = new System.Net.Sockets.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); while (true) { // Set the event to nonsignaled state. AllDone.Reset(); // Start an asynchronous socket to listen for connections. listener.BeginAccept( new AsyncCallback(AcceptCallback), listener); RaiseNotification?.Invoke($"Server Listening on port {port}"); // Wait until a connection is made before continuing. AllDone.WaitOne(); } } catch (Exception) { throw; } }