public void OnReecieveData(IAsyncResult ar) { //Socket sock = (Socket)ar.AsyncState; SocketChatClient client = (SocketChatClient)ar.AsyncState; Socket sock = client.Sock; byte[] aryRet = client.GetRecievedData(ar); Indus.ASN_1.Services.BER.Decoder decoder= new Indus.ASN_1.Services.BER.Decoder(aryRet ); string decoded=decoder.Decode(); _logger.WriteLog( decoded ); // If no data was recieved then the connection is probably dead if (aryRet.Length < 1) { Console.WriteLine("Client " + client.Sock.RemoteEndPoint.ToString() + " disconnected"); client.Sock.Close(); m_aryClients.Remove(client); return; } //_logger.WriteLog("Data Received at Server --Sending to all clients--You can use a session ID to send to one specific client"); //Send only to the one who send data foreach (SocketChatClient clientSend in m_aryClients) { if (client.Sock.RemoteEndPoint == clientSend.Sock.RemoteEndPoint) { try { //Command Handling demponstrated here if (decoded.Contains("GetServerDirectory_Request")) { string[] lD = _iecserver.GetServerDirectory(Indus.ACSI.Core.ObjectClassEnum.LogicalDevice); Indus.ASN_1.Services.BER.Encoder encode= new Indus.ASN_1.Services.BER.Encoder(lD[0] ); clientSend.Sock.Send(encode.Encode()); } else { Indus.ASN_1.Services.BER.Encoder encode= new Indus.ASN_1.Services.BER.Encoder("Response"); clientSend.Sock.Send(encode.Encode()); } } catch { // If the send fails the close the connection Console.WriteLine("Send to client {0} failed", client.Sock.RemoteEndPoint); clientSend.Sock.Close(); m_aryClients.Remove(client); return; } } } // Broadcast:Send the recieved data to all clients (including sender for echo) //foreach (SocketChatClient clientSend in m_aryClients) //{ // try // { // if clientSend.Sock.Send(aryRet); // } // catch // { // // If the send fails the close the connection // Console.WriteLine("Send to client {0} failed", client.Sock.RemoteEndPoint); // clientSend.Sock.Close(); // m_aryClients.Remove(client); // return; // } //} ServerDataReceived(); client.SetupRecieveCallback(this); }
public void OnReecieveData(IAsyncResult ar) { //Socket sock = (Socket)ar.AsyncState; SocketChatClient client = (SocketChatClient)ar.AsyncState; Socket sock = client.Sock; byte[] aryRet = client.GetRecievedData(ar); Indus.ASN_1.Services.BER.Decoder decoder = new Indus.ASN_1.Services.BER.Decoder(aryRet); string decoded = decoder.Decode(); _logger.WriteLog(decoded); // If no data was recieved then the connection is probably dead if (aryRet.Length < 1) { Console.WriteLine("Client " + client.Sock.RemoteEndPoint.ToString() + " disconnected"); client.Sock.Close(); m_aryClients.Remove(client); return; } //_logger.WriteLog("Data Received at Server --Sending to all clients--You can use a session ID to send to one specific client"); //Send only to the one who send data foreach (SocketChatClient clientSend in m_aryClients) { if (client.Sock.RemoteEndPoint == clientSend.Sock.RemoteEndPoint) { try { //Command Handling demponstrated here if (decoded.Contains("GetServerDirectory_Request")) { string[] lD = _iecserver.GetServerDirectory(Indus.ACSI.Core.ObjectClassEnum.LogicalDevice); Indus.ASN_1.Services.BER.Encoder encode = new Indus.ASN_1.Services.BER.Encoder(lD[0]); clientSend.Sock.Send(encode.Encode()); } else { Indus.ASN_1.Services.BER.Encoder encode = new Indus.ASN_1.Services.BER.Encoder("Response"); clientSend.Sock.Send(encode.Encode()); } } catch { // If the send fails the close the connection Console.WriteLine("Send to client {0} failed", client.Sock.RemoteEndPoint); clientSend.Sock.Close(); m_aryClients.Remove(client); return; } } } // Broadcast:Send the recieved data to all clients (including sender for echo) //foreach (SocketChatClient clientSend in m_aryClients) //{ // try // { // if clientSend.Sock.Send(aryRet); // } // catch // { // // If the send fails the close the connection // Console.WriteLine("Send to client {0} failed", client.Sock.RemoteEndPoint); // clientSend.Sock.Close(); // m_aryClients.Remove(client); // return; // } //} ServerDataReceived(); client.SetupRecieveCallback(this); }