public static void Send(ICommandBuilder cmdBuilder) { if (cmdBuilder == null) { throw new Exception("Command sending error - cmd builder is NULL!"); } CommandCreator.CreateCommand(cmdBuilder); TcpServerClient.SendCommandToServer(cmdBuilder.GetCommand()); }
static void Main(string[] args) { IPAddress ipAddress = new IPAddress(0x0100007f); int portNumber = 8092; int len = 256; Byte[] buff = new Byte[len]; Byte[] response = new Byte[len]; TcpServerClient.Serve(ipAddress, portNumber, buff, len, response, logger); } /* end void Main(String[]) */
public void OneClientSendMessageExit() { var client = new TcpServerClient("127.0.0.1", 10000); var message = ""; client.SendToServer(message); var expected = ""; var actual = client.ReceiveFromServer(); client.LeaveFromServer(); Assert.AreEqual("", actual); }
public void OneClientSendRussianMessage() { var client = new TcpServerClient("127.0.0.1", 10000); var message = "Редькин Дмитрий"; client.SendToServer(message); var expected = GetSymbolsSeparatedBySpace(message); var actual = client.ReceiveFromServer(); client.LeaveFromServer(); Assert.AreEqual(expected, actual); }
public void OneClientSendEnglishMessage() { var client = new TcpServerClient("127.0.0.1", 10000); var message = "Redkin Dmitry"; client.SendToServer(message); var expected = GetSymbolsSeparatedBySpace(message); var actual = client.ReceiveFromServer(); client.LeaveFromServer(); Assert.AreEqual(expected, actual); }
private async void CheckConnectionAsync() { do { if (MainGameHandler.ApplicationCloseCalled) { break; } if (this.IsUnlocked && !TcpServerClient.CheckIfConnected()) { ShowDisconnectionMessage(); break; } await Task.Factory.StartNew(() => Thread.Sleep(_connCheckIntervalMs)); }while (!MainGameHandler.ApplicationCloseCalled); }
public static async void CloseApplication() { if (MainGameHandler.ApplicationCloseCalled) { return; } MainGameHandler.ApplicationCloseCalled = true; await Task.Factory.StartNew(() => Thread.Sleep(1000)); Debug.Log("Closing app..."); TcpServerClient.StopServerClient(); Application.Quit(); #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #endif }
private async void HandleServerCommandsAsync() { _logger.UpdateLog("TCP server command handling started!"); string serverCommand = ""; while (this.State == WorkingState.Enabled) { await Task.Factory.StartNew(() => Thread.Sleep(_commandHandlingTickRate)); serverCommand = TcpServerClient.GetCommandFromServer(); if (!String.IsNullOrEmpty(serverCommand)) { _commandHandler.ExecuteCommand(serverCommand); } } _logger.UpdateLog("TCP server command handling stopped!"); }
public void StartConnection(TcpConnectionSettings connectionSettings) { _connectionSettings = connectionSettings; _logger.UpdateLog($"Starting connection with IP [{connectionSettings.Ip}] port [{connectionSettings.Port}]"); try { ConnectionData connData = connectionSettings.ToConnectionData(); //TcpServerClient.SetCommandInsertionTimeLimit(15); _tcpClientInstance = TcpServerClient.RunServerClient(connData, _logger); this.State = WorkingState.Enabled; HandleServerCommandsAsync(); } catch (Exception exception) { _logger.UpdateLog($"TCP connection - connection starting error: {exception.Message} | {exception.StackTrace}"); this.State = WorkingState.Error; } }
public static IServerClient CreateClient(IServerListener listeners, Protocols?proto) { IServerClient sc = null; switch (proto) { case (Protocols.Tcp): { sc = new TcpServerClient(listeners.GetClientSocket()); break; } case (Protocols.Udp): { sc = new UdpServerClient(listeners.GetClientSocket()); break; } } return(sc); }
public void TwoClientsInOneTime() { var client = new TcpServerClient("127.0.0.1", 10000); var anotherClient = new TcpServerClient("127.0.0.1", 10000); var clientMessage = "Редькин Дмитрий"; var anotherClientMessage = "Redkin Dmitry"; client.SendToServer(clientMessage); anotherClient.SendToServer(anotherClientMessage); var expectedResponseToClient = GetSymbolsSeparatedBySpace(clientMessage); var actualResponseToClient = client.ReceiveFromServer(); var expectedResponseToAnotherClient = GetSymbolsSeparatedBySpace(anotherClientMessage); var actualResponseToAnotherClient = anotherClient.ReceiveFromServer(); client.LeaveFromServer(); anotherClient.LeaveFromServer(); Assert.AreEqual(expectedResponseToClient, actualResponseToClient); Assert.AreEqual(expectedResponseToAnotherClient, actualResponseToAnotherClient); }
public static bool[] chuanganqi_connect_is = new bool[400]; // 传感器是否连接上 public Form1() { InitializeComponent(); ViewCaoZuo.Max_Form(this); ViewCaoZuo.Object_Position(0.94, 0.05, 0.05, 0.04, button_exit, this.Controls); ViewCaoZuo.Object_Position(0.94, 0.10, 0.05, 0.04, button1, this.Controls); ViewCaoZuo.Object_Position(0.94, 0.15, 0.05, 0.04, button2, this.Controls); ViewCaoZuo.Object_Position(0.94, 0.20, 0.05, 0.04, button3, this.Controls); ViewCaoZuo.Object_Position(0.94, 0.25, 0.05, 0.04, button4, this.Controls); // 换页 allsubview.Clear(); for (int i = 1; i <= 16; i++) { SubView subview = new SubView(); subview.Set_Machine_Num(i); ViewCaoZuo.Object_Position(0.01 + (i - 1) % 4 * 0.2, 0.05 + (i - 1) / 4 * 0.2, 0.19, 0.19, subview, this.Controls); allsubview.Add(subview); } /// 建立数个ip客户端,用以连接 /// for (int i = 1; i < 100; i++) { try { string address_string = SubView.inifile.IniReadValue("station_ip", i.ToString()); IPAddress ip = IPAddress.Parse(address_string); TcpServerClient client = new TcpServerClient(address_string, 8880); allclient.Add(client); client.Data_Arrival_Event += new EventHandler(DataArrival); } catch { } } }
private void DataArrival(object sender, EventArgs e) { TcpServerClient client = (TcpServerClient)sender; string data_string = ""; for (int i = 0; i < client.receive_num; i++) { data_string = data_string + " " + client.receive_byte[i].ToString("X").PadLeft(2, '0'); } /// 判断来的数据是哪里来的数据 /// // 温度数据 for (int i = 1; i < 100; i++) { string address_string = SubView.inifile.IniReadValue("station_ip", i.ToString()); if (address_string == client.ServerIp.ToString()) { connect_time_out[i] = 0; } } if (client.receive_num == 17) { if (client.receive_byte[0] == 0x02 && client.receive_byte[1] == 0x03 && client.receive_byte[2] == 0x00 && client.receive_byte[3] == 0x00 && client.receive_byte[4] == 0x00 && client.receive_byte[5] == 0x02 && client.receive_byte[6] == 0xc4 && client.receive_byte[7] == 0x38) { // B口来的数据 for (int i = 1; i < 100; i++) { string address_string = SubView.inifile.IniReadValue("station_ip", i.ToString()); if (address_string == client.ServerIp.ToString() && SubView.inifile.IniReadValue("station_chuanganqi", i.ToString()) == "B") { chuanganqi_connect_is[i] = true; float wendu_mul_10 = client.receive_byte[11] * 256 + client.receive_byte[12]; wendu[i] = wendu_mul_10 / 10; connect_time_out[i] = 0; } } } if (client.receive_byte[0] == 0x01 && client.receive_byte[1] == 0x03 && client.receive_byte[2] == 0x00 && client.receive_byte[3] == 0x00 && client.receive_byte[4] == 0x00 && client.receive_byte[5] == 0x02 && client.receive_byte[6] == 0xc4 && client.receive_byte[7] == 0x0b) { // A口来的数据 for (int i = 1; i < 100; i++) { string address_string = SubView.inifile.IniReadValue("station_ip", i.ToString()); if (address_string == client.ServerIp.ToString() && SubView.inifile.IniReadValue("station_chuanganqi", i.ToString()) == "A") { chuanganqi_connect_is[i] = true; double wendu_mul_10 = client.receive_byte[11] * 256 + client.receive_byte[12]; wendu[i] = wendu_mul_10 / 10; connect_time_out[i] = 0; } } } } if (client.receive_num == 8) { if (client.receive_byte[0] != 0xff) { for (int i = 1; i < 100; i++) { string address_string = SubView.inifile.IniReadValue("station_ip", i.ToString()); if (address_string == client.ServerIp.ToString() && SubView.inifile.IniReadValue("station_chuanganqi", i.ToString()) == "A" && client.receive_byte[0] == 01) { chuanganqi_connect_is[i] = false; connect_time_out[i] = 0; } if (address_string == client.ServerIp.ToString() && SubView.inifile.IniReadValue("station_chuanganqi", i.ToString()) == "B" && client.receive_byte[0] == 02) { chuanganqi_connect_is[i] = false; connect_time_out[i] = 0; } } } } // 其他数据 if (client.receive_num == 17) { if (client.receive_byte[0] == 0xff) { for (int i = 1; i < 100; i++) { string address_string = SubView.inifile.IniReadValue("station_ip", i.ToString()); if (address_string == client.ServerIp.ToString() && SubView.inifile.IniReadValue("station_chuanganqi", i.ToString()) == "A") { // 发送开始与停止信息 if (start_is[i] == true) { string ip = SubView.inifile.IniReadValue("station_ip", i.ToString()); foreach (TcpServerClient myclient in Form1.allclient) { if (client.ServerIp.ToString() == ip) { //byte[] send_byte = new byte[3]; //send_byte[0] = 0xFF; //send_byte[1] = 0xff; //send_byte[2] = 0xff; //myclient.Send_Data(send_byte); connect_time_out[i] = 0; if (zhuangtai[i] == 1) { start_is[i] = false; } break; } } } if (stop_is[i] == true) { string ip = SubView.inifile.IniReadValue("station_ip", i.ToString()); foreach (TcpServerClient myclient in Form1.allclient) { if (client.ServerIp.ToString() == ip) { //byte[] send_byte = new byte[3]; //send_byte[0] = 0xF0; //send_byte[1] = 0xf0; //send_byte[2] = 0xf0; //myclient.Send_Data(send_byte); connect_time_out[i] = 0; if (zhuangtai[i] == 0) { stop_is[i] = false; } break; } } } float wendu = client.receive_byte[1]; shengwenwendu[i] = wendu / 10; shengwenshijian[i] = client.receive_byte[2]; jiangwenshijian[i] = client.receive_byte[3]; daojishi[i] = client.receive_byte[4] * 3600 + client.receive_byte[5] * 60 + client.receive_byte[6]; if (daojishi[i] != 0) { Console.WriteLine(daojishi[i].ToString()); } duanhao[i] = client.receive_byte[7]; zhuangtai[i] = client.receive_byte[8]; } if (address_string == client.ServerIp.ToString() && SubView.inifile.IniReadValue("station_chuanganqi", i.ToString()) == "B") { //chuanganqi_connect_is[i] = true; if (start_is[i] == true) { string ip = SubView.inifile.IniReadValue("station_ip", i.ToString()); foreach (TcpServerClient myclient in Form1.allclient) { if (client.ServerIp.ToString() == ip) { //byte[] send_byte = new byte[3]; //send_byte[0] = 0xEF; //send_byte[1] = 0xEf; //send_byte[2] = 0xEf; //myclient.Send_Data(send_byte); //start_is[i] = false; connect_time_out[i] = 0; if (zhuangtai[i] == 1) { start_is[i] = false; } break; } } } if (stop_is[i] == true) { string ip = SubView.inifile.IniReadValue("station_ip", i.ToString()); foreach (TcpServerClient myclient in Form1.allclient) { if (client.ServerIp.ToString() == ip) { //byte[] send_byte = new byte[3]; //send_byte[0] = 0xE0; //send_byte[1] = 0xE0; //send_byte[2] = 0xE0; //myclient.Send_Data(send_byte); //stop_is[i] = false; connect_time_out[i] = 0; if (zhuangtai[i] == 0) { stop_is[i] = false; } break; } } } float wendu = client.receive_byte[9]; shengwenwendu[i] = wendu / 10; shengwenshijian[i] = client.receive_byte[10]; jiangwenshijian[i] = client.receive_byte[11]; daojishi[i] = client.receive_byte[12] * 3600 + client.receive_byte[13] * 60 + client.receive_byte[14]; if (daojishi[i] != 0) { Console.WriteLine(daojishi[i].ToString()); } duanhao[i] = client.receive_byte[15]; zhuangtai[i] = client.receive_byte[16]; } } } } Console.WriteLine(data_string); }