private void TestSocket_Click(object sender, EventArgs e) { if (!OnlineModel.Checked) { SocketLogs.AppendText("服务器连接尚未启动,请先启动在线模式! " + DateTime.Now.ToString() + "\r\n\n"); } SendDataToSocket("TryConnect"); }
public void SendDataToSocket(string SendData) { try { byte[] ArrClientMessage = Encoding.UTF8.GetBytes(SendData); SocketClient.Send(ArrClientMessage); Debug.WriteLine("\r\n" + "数据: " + SendData + "已向服务器发送完成," + DateTime.Now.ToString() + "\r\n" + "\r\n\n"); SocketLogs.AppendText("\r\n" + "数据: " + SendData + "已向服务器发送完成," + DateTime.Now.ToString() + "\r\n" + "\r\n\n"); } catch (System.Exception) { SocketLogs.AppendText("服务器连接已中断,发送失败!,请重新连接...... " + DateTime.Now.ToString() + "\r\n\n"); Debug.WriteLine("服务器连接已中断,发送失败!,请重新连接...... " + DateTime.Now.ToString() + "\r\n\n"); SocketClient.Close(); OnlineModel.Checked = false; } }
private void OnlineModel_CheckedChanged(object sender, EventArgs e) { if (UserClass.IsRegeditExit()) { if (OnlineModel.Checked) { SocketClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress address = IPAddress.Parse(ServerIP.Text); IPEndPoint Point = new IPEndPoint(address, Convert.ToInt32(ServerPort.Text)); try { SocketClient.Connect(Point); OnlineModel.Checked = true; SocketLogs.AppendText("服务器:" + ServerIP.Text + ":" + ServerPort.Text + "连接成功!" + DateTime.Now.ToString() + "\r\n\n"); } catch (System.Exception) { Debug.WriteLine("连接失败!"); SocketLogs.AppendText("服务器:" + ServerIP.Text + ":" + ServerPort.Text + "连接失败!!!" + DateTime.Now.ToString() + "\r\n\n"); OnlineModel.Checked = false; //throw; } String Str = string.Empty; ThreadClient = new Thread(SocketRecive); ThreadClient.IsBackground = true; ThreadClient.Start(); } else { SocketClient.Close(); SocketLogs.AppendText("您已断开和PlantSimulation链接,无法执行后续操作!" + DateTime.Now.ToString() + "\r\n\n"); } } else { SocketLogs.AppendText("未检测到您安装了PlantSimulation 15.1 无法执行后续操作!" + DateTime.Now.ToString() + "\r\n\n"); } }