public void ProcessCommInitPacket(PacketClass PacketObj, IPAddress Ipaddr) { /* Communication Init Packet */ bool IsDeviceExists = false; long ParamDeviceId = Convert.ToInt64(PacketObj.DeviceId); // Converting Byte[] Array to Long for (int i = 0; i <= Devices_Buffer.Count - 1; i++) { if (Devices_Buffer[i].DeviceId.Equals(ParamDeviceId)) { IsDeviceExists = true; /* Comm had failed and it is trying with the new IP. */ /* Check IsConnected */ if (Devices_Buffer[i].IsConnected) // Because Connection_Checking Method still not Removed Old Socket { RemoveSocketByusingIP(Devices_Buffer[i].Ipaddr); } SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address if (Socketobj != null) { Devices_Buffer[i].IsConnected = true; Devices_Buffer[i].LastActivetime = DateTime.Now; Devices_Buffer[i].Ipaddr = Socketobj.IpAddr; // New Ip Address Devices_Buffer[i].ClientObj = Socketobj.Clientobj; // New Socket } /* Frame Ack for Communication Initialization Packet */ byte[] CommAckdata = FrameCommunicationAck(PacketObj); SendDatatoClient(CommAckdata, Devices_Buffer[i].ClientObj); // Device is Re Connected with new Ip Address break; } } if (!IsDeviceExists) // If No Device Exists { DeviceInfo Device = new DeviceInfo(); Device.DeviceId = ParamDeviceId; // It is Dummy Device.IsConnected = true; Device.LastActivetime = DateTime.Now; SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address Device.Ipaddr = Socketobj.IpAddr; Device.ClientObj = Socketobj.Clientobj; Devices_Buffer.Add(Device); // Adding New Device to the Devices_buff /* Frame Ack for Communication Initialization Packet */ byte[] CommAckdata = FrameCommunicationAck(PacketObj); SendDatatoClient(CommAckdata, Device.ClientObj); // Device is New & Sending Bytes to the Client (Device ). } }
public void ListenforClients() { TcpServer.Start(); // To Start The TCP Server while (true) { try { TcpClient Client = TcpServer.AcceptTcpClient(); IPEndPoint Endpoint = Client.Client.RemoteEndPoint as IPEndPoint; IPAddress Ip_addr = Endpoint.Address; bool IsIp_exists = false; for (int i = 0; i <= Sockets_arr.Count - 1; i++) { if (Sockets_arr[i].IpAddr.Equals(Ip_addr)) // If Ip Already Exists { IsIp_exists = true; break; } } if (!(IsIp_exists)) //New Client so need to process It. { SocketClass Socketobj = new SocketClass(); Socketobj.IpAddr = Ip_addr; Socketobj.Clientobj = Client; Sockets_arr.Add(Socketobj); // Add New Socket Client_thread = new Thread(new ParameterizedThreadStart(HandleComm)); Client_thread.Start(Client); // To Start the Thread } else /* Existed Tcp Client & trying to update the TCp Cient Whether with Same Ipaddress Tcp Client will try or not */ { } } catch (Exception ex) { DateTime now_time = DateTime.Now; string time = Convert.ToString(now_time); if (ex != null) { Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured While Listening For Clients: " + ex.Message + time); // Logging the Execptions Ocuured } } } }
public void ProcessCommInitPacket(PacketClass PacketObj, IPAddress Ipaddr) { /* Communication Init Packet */ try { bool IsDeviceExists = false; for (int i = 0; i <= Devices_Buffer.Count - 1; i++) { if (Devices_Buffer[i].DeviceId.SequenceEqual(PacketObj.DeviceId)) { IsDeviceExists = true; /* Comm had failed and it is trying with the new IP. */ /* Check IsConnected */ if (Devices_Buffer[i].IsConnected) // Because Connection_Checking Method still not Removed Old Socket { RemoveSocketByusingIP(Devices_Buffer[i].Ipaddr); } SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address if (Socketobj != null) // Socketobj will be null when same IP requests { Devices_Buffer[i].IsConnected = true; Devices_Buffer[i].LastActivetime = DateTime.Now; Devices_Buffer[i].Ipaddr = Socketobj.IpAddr; // New Ip Address // Devices_Buffer[i].ClientStream.Close(); // To Close the Previous Network Stream bw Client & Server. Devices_Buffer[i].ClientStream = Socketobj.Clientobj.GetStream(); // New Socket Stream /* Frame Ack for Communication Initialization Packet */ byte[] CommAckdata = FrameCommunicationAck(PacketObj); SendDatatoClient(CommAckdata, Devices_Buffer[i].ClientStream); // Device is Re Connected with new Ip Address } break; } } if (!IsDeviceExists) // If No Device Exists { DeviceInfo Device = new DeviceInfo(); Device.DeviceId = new byte[PacketObj.DeviceId.Length]; // Need to Initialize the DeviceID array PacketObj.DeviceId.CopyTo(Device.DeviceId, 0); // It is Dummy Device.IsConnected = true; Device.LastActivetime = DateTime.Now; SocketClass Socketobj = FindSocketbyusingIp(Ipaddr); // To Find the Socket by using new IP Address Device.Ipaddr = Socketobj.IpAddr; Device.ClientStream = Socketobj.Clientobj.GetStream(); Devices_Buffer.Add(Device); // Adding New Device to the Devices_buff /* Frame Ack for Communication Initialization Packet */ byte[] CommAckdata = FrameCommunicationAck(PacketObj); // Don't know why DeviceID is getting reversed when i reverse the DeviceID in AckPacket // Array.Reverse(Device.DeviceId); SendDatatoClient(CommAckdata, Device.ClientStream); // Device is New & Sending Bytes to the Client (Device ). } } catch (Exception ex) { DateTime now_time = DateTime.Now; string time = Convert.ToString(now_time); Global.AppendTexttoFile(Global.exception_filepath, "Exception Ocuured in ProcessCommInitPacket: " + ex.Message + time); // Logging the Execptions Ocuured } }