客户端代码:
1 byte[] ReadBytes = new byte[1024]; 2 private void ConnectServer_Camera(string ip, int port) 3 { 5 tcpclient= new TcpClient(); 6 tcpclient.BeginConnect(IPAddress.Parse(ip), port, CameraProcess, null); 8 } 9 private async void CameraProcess(IAsyncResult ar) 10 { 11 if (tcpclient.Connected) 12 { 13 tcpclient.EndConnect(ar); 14 tcpclient.GetStream().BeginRead(ReadBytes , 0, ReadBytes .Length, ReceiveCallBack_Camera, null); 15 } 16 else 17 { 18 await Task.Delay(2000); 19 20 tcpclient.BeginConnect(IPAddress.Parse(cameraIP), cameraPort, CameraProcess, null); 21 } 22 } 24 private void ReceiveCallBack_Camera(IAsyncResult ar) 25 { 27 int len = tcpclient.GetStream().EndRead(ar); 28 if (len > 0) 29 { 30 string str = Encoding.UTF8.GetString(ReadBytes , 0, len); 32 } 33 else 34 { 35 tcpclient= null; 37 ConnectServer_Camera(cameraIP, cameraPort); 38 } 39 41 }
private void ConnectServer_Camera(string ip, int port) { try { mTcpClient_Camera = new TcpClient(); mTcpClient_Camera.BeginConnect(IPAddress.Parse(ip), port, CameraProcess, null); } catch (Exception e) { mOP01ViewModel.AddRecordInfo(RecordLevel.Error, $"视觉服务连接异常:{e.Message}"); } } 标签:tcpclient,通讯,C#,void,TCP,Camera,ReadBytes,null,port From: https://www.cnblogs.com/follow-discoverer/p/18069279