Exemplo n.º 1
0
        private void Receive(ref OnlineNode s)
        {
            Socket thisClient = s.user_socket;
            string uname      = s.username;
            bool   connected  = true;

            while (connected && !terminating)
            {
                try
                {
                    Byte[] buffer = new Byte[64]; // username is received here
                    thisClient.Receive(buffer);

                    string incomingMessage = Encoding.Default.GetString(buffer);
                    incomingMessage = incomingMessage.Substring(0, incomingMessage.IndexOf("\0"));
                    logs.AppendText("Client: " + incomingMessage + "\n");
                    bool should_i_accept = check_Allowance(ref incomingMessage);

                    Thread deliverThread = new Thread(() => deliver_Message_From_User(ref uname, ref incomingMessage));
                    deliverThread.Start();
                }
                catch
                {
                    if (!terminating)
                    {
                        logs.AppendText("A client has disconnected\n");
                    }
                    thisClient.Close();
                    onlineUsers.Remove(s);
                    connected = false;
                }
            }
        }
Exemplo n.º 2
0
 private void Accept()
 {
     while (listening)
     {
         try
         {
             Socket     newClient = serverSocket.Accept();
             OnlineNode user      = Receive_Username(ref newClient);
             logs.AppendText("A client is connected.\n");
             Thread receiveThread = new Thread(() => Receive(ref user));
             receiveThread.Start();
         }
         catch
         {
             if (terminating)
             {
                 listening = false;
             }
             else
             {
                 logs.AppendText("The socket stopped working.\n");
             }
         }
     }
 }
Exemplo n.º 3
0
        private OnlineNode Receive_Username(ref Socket s)
        {
            Socket thisClient = s;
            bool   connected  = true;

            while (connected && !terminating)
            {
                try
                {
                    Byte[] buffer = new Byte[64]; // username is received here
                    thisClient.Receive(buffer);

                    string incomingMessage = Encoding.Default.GetString(buffer);
                    incomingMessage = incomingMessage.Substring(0, incomingMessage.IndexOf("\0"));
                    logs.AppendText("Client: " + incomingMessage + "\n");
                    bool should_i_accept = check_Allowance(ref incomingMessage);
                    if (should_i_accept)
                    {
                        OnlineNode u = new OnlineNode(ref incomingMessage, ref thisClient);
                        return(u);

                        logs.AppendText("Client: " + incomingMessage + " is accepted to the network. \n");
                    }
                    else
                    {
                        logs.AppendText("Client: " + incomingMessage + "is rejected : not registered in the Database. \n");
                    }
                }
                catch
                {
                    if (!terminating)
                    {
                        logs.AppendText("A client has disconnected\n");
                    }
                    thisClient.Close();
                    connected = false;
                }
            }
            throw new Exception();
        }