예제 #1
0
파일: UDPListener.cs 프로젝트: Ikthios/P2P
        private static void OnTimedEvent(Object source, ElapsedEventArgs e)
        {
            ServDatabase test = new ServDatabase();
            Console.WriteLine("Server checked list of peers at {0:HH:mm:ss.fff}",
                              e.SignalTime);
            Console.WriteLine("Printing list of active peer IP Addresses:");
            foreach(string ip in checkedIPAddresses)
            {
                Console.WriteLine(ip);
            }

            updatePingList();

            Console.WriteLine("Printing list of users still in after update");
            test.PrintDataList();
            checkedIPAddresses.Clear();
        }
예제 #2
0
        private void ServerHandler()
        {
            Boolean loop = true;
            ASCIIEncoding encoding = new ASCIIEncoding();
            while (loop)
            {
                ServDatabase servDB = new ServDatabase();

                try
                {
                    Console.WriteLine("Connection accepted from " + clientSocket.RemoteEndPoint);

                    byte[] b = new byte[1500];           // Create byte array for receiving client data
                    int csr = clientSocket.Receive(b);  // Receive the client data
                    Console.WriteLine("Received connection...");
                    string dataString = "";     // This will hold the raw data coming in from the connected peer

                    // Build the message
                    for (int i = 0; i < csr; i++)
                    {
                        dataString += Convert.ToChar(b[i]);
                    }
                    /*
                    This will split/organize the raw data into a format
                    the server can understand.
                    */
                    string[] tokens = dataString.Split(',');

                    /*
                    (REG)istration
                    The 'REG' keyword is used by the server to identify that this is a registration call
                    made by the client form and that it is to be registered in the serverside database.

                    (C)lient (F)ile (R)equest
                    The 'CFR' keyword is used by the server to identify that this is a request call
                    made by the client form for a file hosted by another client.

                    (S)top (C)lient (L)oop
                    The 'SCL' keyword is used by the client to identify when the server is done sending
                    information during a connection request.

                    (U)pdate (P)eer (E)ntry
                    The 'UPE' keyword is used by the server to update the peerList when a peer receives
                    a new file entry.
                    */
                    if (tokens[0].Equals("REG")) {
                        // Register the client
                        clientSocket.Send(encoding.GetBytes("IP " + tokens[1] + " registered."));
                        clientSocket.Send(encoding.GetBytes("SCL"));
                        servDB.RegisterPeer(dataString.Remove(0, 4));
                        Console.WriteLine("Printing the data list...");
                        servDB.PrintDataList();
                    }
                    else if (tokens[0].Equals("CFR"))
                    {
                        /*
                        CFR Request
                        [0] = Keyword
                        [1] = Requested File
                        [2] = Requesting Peer IP Address
                        */
                        // Search data list for files
                        Console.WriteLine("File search for " + tokens[1] + " received.");
                        string hostingPeer = servDB.RetreivePeer(tokens[2], tokens[1]);   // Remove the 'CFR' keyword
                        clientSocket.Send(encoding.GetBytes(hostingPeer));
                        clientSocket.Send(encoding.GetBytes("SCL"));
                        servDB.PrintDataList();
                    }
                    else if (tokens[0].Equals("UPE"))
                    {
                        /*
                        UPE Request
                        [0] = Keyword
                        [1] = File
                        [2] = Requesting Peer IP Address
                        */
                        Console.WriteLine("Peer list udpate for " + tokens[2] + " with " + tokens[1]);
                        servDB.UpdatePeer(tokens[2], tokens[1]);
                    }
                    else
                    {
                        Console.WriteLine(dataString);  // Print client data to the console
                        /* Start data sent to client
                        This is how the peer list from the server DB will be sent.
                        */
                        clientSocket.Send(encoding.GetBytes("String received by server"));
                        clientSocket.Send(encoding.GetBytes("SCL"));
                        /* End data sent to client */
                    }
                    // Clear the datastring
                    dataString = "";
                }
                catch(SocketException socketException)
                {
                    // Get IP Address:Port of client on socket
                    IPEndPoint remoteIpEndPoint = clientSocket.RemoteEndPoint as IPEndPoint;

                    // Split Endpoint object to get just IP without port on end
                    string[] IPHolster = remoteIpEndPoint.ToString().Split(':');

                    // Call remove IP of client that caused exception
                    servDB.RemovePeer(IPHolster[0]);
                    Console.WriteLine("Client at IP: " + IPHolster[0] + " disconnected. Peer list updated.");
                    servDB.PrintDataList();
                    loop = false;
                }
                catch (Exception e)
                {
                    Console.WriteLine("Client Connection Error: " + e.ToString());
                    loop = false;
                }
            }

            //Stop(); // Stop the client socket connection
        }
예제 #3
0
파일: UDPListener.cs 프로젝트: Ikthios/P2P
        private static void updatePingList()
        {
            ServDatabase disBass = new ServDatabase();
            List<string> databaseList = disBass.retrieveList();
            List<string> removalList = new List<string>();

            foreach(string bassEntry in databaseList)
            {
                bool found = false;
                string[] holster = bassEntry.Split(',');
                foreach(string listenerEntry in checkedIPAddresses)
                {
                    if(holster[0].Equals(listenerEntry))
                    {
                        found = true;
                    }
                }
                if(!found)
                {
                    removalList.Add(holster[0]);
                }
            }

            foreach(string entry in removalList)
            {
                disBass.RemovePeer(entry);
            }
        }