private void SP_ReadData_DataReceived(object sender, SerialDataReceivedEventArgs e) { // 接收数据 List <byte> buffer = new List <byte>( ); byte[] data = new byte[1024]; while (true) { System.Threading.Thread.Sleep(20); if (SP_ReadData.BytesToRead < 1) { break; } int recCount = SP_ReadData.Read(data, 0, Math.Min(SP_ReadData.BytesToRead, data.Length)); byte[] buffer2 = new byte[recCount]; Array.Copy(data, 0, buffer2, 0, recCount); buffer.AddRange(buffer2); } if (buffer.Count == 0) { return; } try { byte[] send = buffer.ToArray( ); client.Send(send, 0, send.Length, SocketFlags.None); } catch (Exception ex) { MessageBox.Show("Client Send Failed! " + ex.Message); return; } Invoke(new Action(() => { string msg = string.Empty; if (checkBox1.Checked) { msg = HslCommunication.BasicFramework.SoftBasic.ByteToHexString(buffer.ToArray( ), ' '); } else { msg = HslCommunication.BasicFramework.SoftBasic.GetAsciiStringRender(buffer.ToArray( )); } DemoUtils.AppendTextBox(textBox6, "Serial-Tcp", msg); })); }
private void ReceiveCallBack(IAsyncResult ar) { if (ar.AsyncState is ClientSession client) { try { int length = client.Socket.EndReceive(ar); if (length == 0) { Invoke(new Action(() => { client.Socket.Close( ); DemoUtils.AppendTextBox(textBox6, client.EndPoint.ToString( ), "Client Offline"); })); client = null; return; } ; client.Socket.BeginReceive(buffer, 0, 2048, SocketFlags.None, new AsyncCallback(ReceiveCallBack), client); byte[] data = new byte[length]; Array.Copy(buffer, 0, data, 0, length); remote?.Send(data, 0, data.Length, SocketFlags.None); Invoke(new Action(() => { string msg = string.Empty; if (checkBox1.Checked) { msg = HslCommunication.BasicFramework.SoftBasic.ByteToHexString(data, ' '); } else { msg = HslCommunication.BasicFramework.SoftBasic.GetAsciiStringRender(data); } DemoUtils.AppendTextBox(textBox6, "Client->Remote", msg); })); } catch { Invoke(new Action(() => { this.client = null; DemoUtils.AppendTextBox(textBox6, "Client Offline", Program.Language == 1 ? "服务器断开连接。" : "DisConnect from remote"); })); } } }
/// <summary> /// 异步传入的连接申请请求 /// </summary> /// <param name="iar">异步对象</param> protected void AsyncAcceptCallback(IAsyncResult iar) { //还原传入的原始套接字 if (iar.AsyncState is Socket server_socket) { ClientSession session = new ClientSession( ); try { // 在原始套接字上调用EndAccept方法,返回新的套接字 client = server_socket.EndAccept(iar); session.Socket = client; session.EndPoint = (IPEndPoint)client.RemoteEndPoint; client.BeginReceive(buffer, 0, 2048, SocketFlags.None, new AsyncCallback(ReceiveCallBack), session); Invoke(new Action(() => { DemoUtils.AppendTextBox(textBox6, session.EndPoint.ToString( ), "Client Online"); textBox4.Text = client.RemoteEndPoint.ToString( ); })); } catch (ObjectDisposedException) { // 服务器关闭时候触发的异常,不进行记录 client = null; return; } catch { // 有可能刚连接上就断开了,那就不管 client = null; client?.Close( ); } server_socket.BeginAccept(new AsyncCallback(AsyncAcceptCallback), server_socket); } }
private void RemoteSocketEndReceive(IAsyncResult ar) { if (ar.AsyncState is Socket socket) { byte[] buffer = new byte[1024]; try { int length = socket.Receive(buffer, 0, 1024, SocketFlags.None); if (length == 0) { Invoke(new Action(() => { DemoUtils.AppendTextBox(textBox6, remote.RemoteEndPoint.ToString( ), "Remote Offline"); })); return; } else { buffer = buffer.SelectBegin(length); } remote.BeginReceive(new byte[0], 0, 0, SocketFlags.None, new AsyncCallback(RemoteSocketEndReceive), remote); } catch (Exception ex) { Invoke(new Action(() => { DemoUtils.AppendTextBox(textBox6, remote?.RemoteEndPoint.ToString( ), "Remote Offline Exception: " + ex.Message); })); return; } try { client?.Send(buffer, 0, buffer.Length, SocketFlags.None); Invoke(new Action(() => { string msg = string.Empty; if (checkBox1.Checked) { msg = HslCommunication.BasicFramework.SoftBasic.ByteToHexString(buffer, ' '); } else { msg = HslCommunication.BasicFramework.SoftBasic.GetAsciiStringRender(buffer); } DemoUtils.AppendTextBox(textBox6, "Remote->Client", msg); })); } catch (Exception ex) { Invoke(new Action(() => { if (client != null) { DemoUtils.AppendTextBox(textBox6, client.RemoteEndPoint.ToString( ), "Client Offline Exception: " + ex.Message); } })); return; } } }