void DataReceivedNotify(ConnectionClass i_Connection, byte[] i_bytes) { DataEventArgs e = new DataEventArgs(); //for (int i = 0; i < GilConectionsClassList.Count; i++) //{ // if (GilConectionsClassList[i] == i_Connection) // { // e.ConnectionNumber = i; // } //} e.ConnectionNumber = i_Connection.TcpClientConnection.Client.RemoteEndPoint.ToString(); e.BytesData = i_bytes; OnDataReceived(e); }
private void ListenForClients() { try { this.tcpListener.Start(); while (true) { //blocks until a client has connected to the server TcpClient client = this.tcpListener.AcceptTcpClient(); //create a thread to handle communication //with connected client Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm)); //ThreadsList.Add(clientThread); ConnectionClass ConClass = new ConnectionClass(clientThread, client, 120); //ConClass.m_clientThread = clientThread; //ConClass.m_Tcpclient = client; clientThread.Start(ConClass); GilConectionsClassList.Add(ConClass); } } catch (System.Net.Sockets.SocketException) { // LogGeneral.LogMessage(Color.Black, Color.White, ex.ToString(), New_Line = true, Show_Time = true); } catch (System.Threading.ThreadAbortException) { // LogGeneral.LogMessage(Color.Black, Color.White, ex.ToString(), New_Line = true, Show_Time = true); } catch (Exception ex) { InformationNotify(ex.ToString()); // LogGeneral.LogMessage(Color.Black, Color.White, ex.ToString(), New_Line = true, Show_Time = true); } }
private void HandleClientComm(object client) { ConnectionClass connection = (ConnectionClass)client; try { if (m_CloseServer == true) { return; } tcpClient = (TcpClient)connection.TcpClientConnection; clientStream = tcpClient.GetStream(); NetworkStream clientStreamPrivate = tcpClient.GetStream(); ////blocks until a client has connected to the server //TcpClient client = this.tcpListener.AcceptTcpClient(); //string clientIPAddress = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString(); //var port = ((IPEndPoint)tcpClient.Client.RemoteEndPoint).Port; InformationNotify("Connection accepted from " + tcpClient.Client.RemoteEndPoint); //LogGeneral.LogMessage(Color.Green, Color.White, "Connection accepted, " + tcpClient.Client.RemoteEndPoint.ToString(), true, true); byte[] message = new byte[4096]; int bytesRead; //IsTimedOutTimerEnabled = true; //GetDataIntervalCounter = 0; while (true) { if (m_CloseServer == true) { if (clientStream != null) { clientStream.Dispose(); clientStream = null; } return; } bytesRead = 0; Array.Clear(message, 0, message.Length); //blocks until a client sends a message bytesRead = clientStreamPrivate.Read(message, 0, 4096); if (bytesRead == 0) { break; } else { clientStream = clientStreamPrivate; DataReceivedNotify(connection, message); connection.ConnectionTimeOut = 120; } } } //catch (System.Net.Sockets.SocketException) //{ // // LogGeneral.LogMessage(Color.Black, Color.White, ex.ToString(), New_Line = true, Show_Time = true); //} //catch (System.IO.IOException) //{ // // LogGeneral.LogMessage(Color.Black, Color.White, ex.ToString(), New_Line = true, Show_Time = true); //} //catch (System.Threading.ThreadAbortException) //{ // // LogGeneral.LogMessage(Color.Black, Color.White, ex.ToString(), New_Line = true, Show_Time = true); //} catch (Exception ex) { InformationNotify(ex.Message.ToString()); // LogGeneral.LogMessage(Color.Black, Color.White, ex.ToString(), New_Line = true, Show_Time = true); } //if (tcpClient != null) //{ // try // { // tcpClient.Close(); // } // catch // { // } //} if (connection.ConnectionTimeOut == 0) { InformationNotify("Connection Closed due time out: "); } else { InformationNotify("Connection Closed: "); } // LogGeneral.LogMessage(Color.Orange, Color.White, "Connection Closed ", true, true); }