/// <summary> /// 建立所有连接 /// </summary> public void connectAllcon() { //用于标识是否有连接建立成功 bool flag = false; //PLC连接需要额外判断复选框是否勾选 if (conn1En.Checked && plcSocket1 == null && !isEmpty(plcIp1.Text) && !isEmpty(plcPort1.Text) && !isEmpty(trigger1.Text) && !isEmpty(result1.Text)) { plcConnectArr[0].ip = plcIp1.Text; plcConnectArr[0].port = int.Parse(plcPort1.Text); plcSocket1 = SocketUtils.connectToTarget(plcConnectArr[0].ip, plcConnectArr[0].port); if (plcSocket1 != null) { flag = true; } } if (conn2En.Checked && plcSocket2 == null && !isEmpty(plcIp2.Text) && !isEmpty(plcPort2.Text) && !isEmpty(trigger2.Text) && !isEmpty(result2.Text)) { plcConnectArr[1].ip = plcIp2.Text; plcConnectArr[1].port = int.Parse(plcPort2.Text); plcSocket2 = SocketUtils.connectToTarget(plcConnectArr[1].ip, plcConnectArr[1].port); if (plcSocket2 != null) { flag = true; } } if (conn3En.Checked && plcSocket3 == null && !isEmpty(plcIp3.Text) && !isEmpty(plcPort3.Text) && !isEmpty(trigger3.Text) && !isEmpty(result3.Text)) { plcConnectArr[2].ip = plcIp3.Text; plcConnectArr[2].port = int.Parse(plcPort3.Text); plcSocket3 = SocketUtils.connectToTarget(plcConnectArr[2].ip, plcConnectArr[2].port); if (plcSocket3 != null) { flag = true; } } //判断标志位状态,没有发生更改,说明一个连接都没有建立 if (flag) { //只有在有连接参数、有PLC连接成功时才连接Inspect if (inspectSocket == null && !isEmpty(inspectIp.Text) && !isEmpty(inspectPort.Text)) { //获取连接参数 inspectConnectInfo.ip = inspectIp.Text; inspectConnectInfo.port = int.Parse(inspectPort.Text); //建立连接 inspectSocket = SocketUtils.connectToTarget(inspectConnectInfo.ip, inspectConnectInfo.port); //inspectSocket.SendTimeout = 500; //连接状态更新 } testMsg.Text = "无"; testMsg.BackColor = Color.Silver; connectAll.Text = "关闭连接"; connectAll.BackColor = Color.LimeGreen; connectStatus = true; conn1En.Enabled = false; conn2En.Enabled = false; conn3En.Enabled = false; } else { MessageBox.Show("连接失败"); } }
private void connect() { Socket socket = SocketUtils.connectToTarget(serverIp, serverPort); }