Esempio n. 1
0
        private void HandleClientComm(object client)
        {
            TcpClient     tcpClient    = (TcpClient)client;
            NetworkStream clientStream = tcpClient.GetStream();

            allConnections.Add(clientStream);

            byte[] message = new byte[4096];
            int    bytesRead;

            string IP = ((Socket)clientStream.GetType().GetProperty("Socket", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(clientStream, null)).RemoteEndPoint.ToString();

            sendMessage(clientStream, "/Connected");

            cmdHandler = new CommandHandler(console, this, clientStream);

            while (loop)
            {
                bytesRead = 0;

                try
                {
                    //waits for a message
                    bytesRead = clientStream.Read(message, 0, 4096);
                }
                catch (Exception e)
                {
                    //error occured
                    console.writeLine(e.ToString());
                }

                if (bytesRead == 0)
                {
                    //client has left server
                    console.writeLine(IP + " Has left the server");
                    break;
                }

                //message has been recieved
                ASCIIEncoding encoder = new ASCIIEncoding();
                string        msg     = encoder.GetString(message, 0, bytesRead);
                if (msg.Contains("/"))
                {
                    cmdHandler.cmd(msg);
                }
                else
                {
                    console.writeLine(IP + ": " + encoder.GetString(message, 0, bytesRead));
                    broadcastMessage(IP + ": " + msg);
                }
            }

            //close client
            tcpClient.Close();
            clientStream.Close();
        }
Esempio n. 2
0
        private void checkForMessages()
        {
            byte[] message = new byte[4096];
            int    bytesRead;

            bytesRead = 0;

            try
            {
                //waits for a message
                bytesRead = clientStream.Read(message, 0, 4096);
            }
            catch (Exception e)
            {
                //error occured
                console.writeLine(e.ToString());
            }
            if (bytesRead == 0)
            {
                return;
            }
            //message has been recieved
            ASCIIEncoding encoder = new ASCIIEncoding();
            string        msg     = encoder.GetString(message, 0, bytesRead);

            //console.writeLine("DEBUG: Server Sent: " + msg);
            if (msg.Contains('/') != true)
            {
                if (msg.Contains(LocalIPAddress()))
                {
                    for (int i = 0; i < LocalIPAddress().Length; i++)
                    {
                        msg = msg.TrimStart(LocalIPAddress()[i]);
                    }
                    msg = msg.Remove(0, 6);
                    console.writeLine("You: " + msg);
                }
                else
                {
                    console.writeLine(msg);
                }
            }
            else if (msg.TrimStart('/').ToUpper() == "QUIT")
            {
                console.writeLine("Server Disconnected, Quiting...");
                client.Close();
                this.clientStream.Close();
                timer.Stop();
            }
        }
Esempio n. 3
0
 public TCPServer(ChatUtils.Console console)
 {
     this.console = console;
     try
     {
         this.tcpListener  = new TcpListener(IPAddress.Any, 3000);
         this.listenThread = new Thread(new ThreadStart(ListenForClients));
         this.listenThread.Start();
         allConnections = new List <NetworkStream>();
     }
     catch (Exception e)
     {
         console.writeLine(e.ToString());
     }
 }
Esempio n. 4
0
 public TCPClient(string p1, int p2, ChatUtils.Console console)
 {
     try
     {
         client         = new TcpClient();
         serverEndPoint = new IPEndPoint(IPAddress.Parse(p1), p2);
         client.Connect(serverEndPoint);
         clientStream   = client.GetStream();
         timer          = new System.Timers.Timer();
         timer.Interval = 100;
         timer.Elapsed += new ElapsedEventHandler(timerElapsed);
         timer.Start();
         this.console = console;
     }
     catch (Exception e)
     {
         this.console = console;
         console.writeLine(e.ToString());
     }
 }