public StatusTransaction sendMessageToUnitDisplay(string ipAddress, int port, List <string> list_message) { try { Socket m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); RemoteIPAddress objRemoteIPAddress = new RemoteIPAddress(); System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(ipAddress); System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(remoteIPAddress, port); m_socClient.Connect(remoteEndPoint); foreach (string msg in list_message) { byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg); m_socClient.Send(byData); if (list_message.IndexOf(msg) != list_message.Count() - 1) { System.Threading.Thread.Sleep(500); } } m_socClient.Close(); return(StatusTransaction.True); } catch (Exception ex) { Program.MessageError("ClsTCPClient", "sendMessegeToUnitDisplay", ex, false); return(StatusTransaction.Error); } }
public StatusTransaction sendMessageTCPServer(string message, int limitLoop = 10) { try { Socket m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); RemoteIPAddress objRemoteIPAddress = new RemoteIPAddress(); System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(objRemoteIPAddress.GetObjectIPAddress(), GetPort()); byte[] byData = System.Text.Encoding.ASCII.GetBytes(message); int loopSend = 0; do { try { loopSend++; m_socClient.Connect(remoteEndPoint); if (m_socClient.Connected) { m_socClient.Send(byData); return(StatusTransaction.True); } else { System.Threading.Thread.Sleep(100); } } catch { } finally { if (m_socClient.Connected) { m_socClient.Close(); } } }while (loopSend < limitLoop); return(StatusTransaction.False); } catch (Exception ex) { Program.MessageError("ClsTCPClient", "sendMessageTCPServer", ex, false); return(StatusTransaction.Error); } }
public StatusTransaction sendMessegeToUnitDisplay(List <string> list_message) { Socket socket = null; //try //{ socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.DontLinger, false); string ipAddress = new RemoteIPAddress().GetRemoteIPAddress(); IAsyncResult result = socket.BeginConnect(ipAddress, GetPort(), null, null); bool success = result.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, 30), true); if (success) { if (socket.Connected) { foreach (string msg in list_message) { byte[] byData = System.Text.Encoding.ASCII.GetBytes(msg); socket.Send(byData); } } } else { Program.MessageError("ClsTCPClient", "sendMessegeToUnitDisplay", "connect port not success", false); } if (null != socket) { socket.Close(); } return(StatusTransaction.True); //} //catch (Exception ex) //{ // Program.MessageError("ClsTCPClient", "sendMessegeToUnitDisplay", ex.Message, false); // return StatusTransaction.Error; //} //finally //{ // if (null != socket) socket.Close(); //} }
public void sendMessegeToUnitDisplay(string Messege) { Socket m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); RemoteIPAddress objRemoteIPAddress = new RemoteIPAddress(); System.Net.IPEndPoint remoteEndPoint = new System.Net.IPEndPoint(objRemoteIPAddress.GetObjectIPAddress(), GetPort()); sendNew: try { m_socClient.Connect(remoteEndPoint); byte[] byData = System.Text.Encoding.ASCII.GetBytes(Messege); m_socClient.Send(byData); m_socClient.Close(); } catch { goto sendNew; //Program.MessageError("ClsTCPClient", "sendMessegeToUnitDisplay", ex.Message, false); //if (m_socClient.Connected == false) { return; }; } }