客户端代码:
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
}”); } }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。