public void AcceptConnections() { while (true) { Honeypots.Add(Listener.AcceptTcpClient()); Console.WriteLine("Client " + IPAddress.Parse(((IPEndPoint)Honeypots.Last().Client.RemoteEndPoint).Address.ToString()) + " has connected!"); } }
public void Receive() { ArrayList read_socket = new ArrayList(); Byte[] recvbuff; string recv_data; int recvbuflen = 1024; while (true) { for (int i = 0; i < Honeypots.Count; i++) { read_socket.Clear(); if (Honeypots[i] != null) { read_socket.Add(Honeypots[i].Client); try { Socket.Select(read_socket, null, null, 500000); if (read_socket.Count > 0) { recvbuff = new Byte[recvbuflen]; Honeypots[i].Client.Receive(recvbuff); recv_data = Encoding.ASCII.GetString(recvbuff); if (!string.IsNullOrEmpty(recv_data)) { if (recv_data != "-1") { Console.WriteLine("Incoming message, IP: " + ((IPEndPoint)Honeypots[i].Client.RemoteEndPoint).Address); InMessages.Enqueue(new Message(recv_data, Honeypots[i].Client)); recv_data = "-1"; // "-1" is a special string, used to tell if the buffer was read in the current iteration. } } else { Honeypots[i].Client.Close(); Honeypots[i].Close(); Honeypots.RemoveAt(i); // remove the problematic socket from the clients list break; // break from the current for, so the loop would initiate according to the removal of the socket } } } catch (Exception) { Honeypots[i].Client.Close(); Honeypots[i].Close(); Honeypots.RemoveAt(i); // remove the problematic socket from the clients list break; // break from the current for, so the loop would initiate according to the removal of the socket } } } } }