private void ReadCallback(IAsyncResult ar) { // Retrieve the state object and the handler socket // from the asynchronous state object. var state = (StateObject)ar.AsyncState; var handler = state.workSocket; // Read data from the client socket. int bytesRead = handler.EndReceive(ar); if (bytesRead > 0) { RaiseAction?.Invoke(state.buffer); handler.BeginSend(state.buffer, 0, state.buffer.Length, 0, new AsyncCallback(SendCallback), handler); } }
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)); } } }