/**
         * Deserialises the Packets received from clients and operates on them
         *
         * @param Client which is sent from Client.cs
         */
        public static void SocketMethod(Client client)
        {
            try
            {
                client.SendText("Welcome User: Enter '/clientend' to terminate Chatroom \n");
                int INCOMING_BYTES;

                while ((INCOMING_BYTES = client.BIN_READER.ReadInt32()) != 0)
                {
                    byte[] bytes = client.BIN_READER.ReadBytes(INCOMING_BYTES);
                    MemoryStream MEMORY_STREAM = new MemoryStream(bytes);
                    BinaryFormatter formatter = new BinaryFormatter();

                    PACKET RECEIVED_PACKET = formatter.Deserialize(MEMORY_STREAM) as PACKET;

                    switch (RECEIVED_PACKET.PACKET_TYPE)
                    {
                        case PACKET_TYPE_ENUM.NICKNAME:
                            string nickname = ((NICKNAME_PACKET)RECEIVED_PACKET).nickname;
                            client.SetNickname(nickname);
                            break;
                        case PACKET_TYPE_ENUM.CHAT_MESSAGE:
                            string message = client.GetNickname() + ((CHAT_MESSAGE)RECEIVED_PACKET).message;
                            foreach (Client c in _clients)
                            {
                                if (message != "/clientend")
                                    c.SendText(message);
                                else if (message == "/clientend")
                                    c.Stop();
                            }
                            break;
                        case PACKET_TYPE_ENUM.USER_LIST:
                            List<string> uList = ((USER_LIST)RECEIVED_PACKET).userList;
                            client.SendList(uList);
                            break;
                        default:
                            break;
                    }
                }
             }
            catch (Exception e)
            {
                Console.WriteLine("Error occured: " + e.Message);
            }
            finally
            {
                client.Stop();
            }
        }
Exemplo n.º 2
0
        public static void SocketMethod(Client client)
        {
            try
            {
                Socket socket = client.Socket;

                NetworkStream stream = client.Stream;
                BinaryReader bReader = client.bReader;

                client.SendTextPacket("Successfully Connected", null);

                int noOfIncomingBytes;
                    while ((noOfIncomingBytes = bReader.ReadInt32()) != 0)
                    {
                        byte[] bytes = bReader.ReadBytes(noOfIncomingBytes);
                        MemoryStream memoryStream = new MemoryStream(bytes);
                        BinaryFormatter formatter = new BinaryFormatter();
                        Packet packet = formatter.Deserialize(memoryStream) as Packet;
                        switch (packet.type)
                        {

                                case (PacketType.NICKNAME):
                                string name = ((NicknamePacket)packet).nickName;

                                client.SetUsername(name);
                                string[] clientList = clients.ConvertAll(c=>c.userName).ToArray();
                                foreach (Client c in clients)
                                {
                                    if (c != null)
                                    {
                                        c.SendClientList(clientList);
                                    }
                                }

                                break;

                                case (PacketType.CHATMESSAGE):
                                string sender = ((ChatMessagePacket)packet).sender;
                                string message = sender + " : " + ((ChatMessagePacket) packet).chatMessage;
                                Console.WriteLine(message);
                                foreach (Client c in clients)
                                {
                                    if (c != null)
                                    {
                                        c.SendTextPacket(message, sender);
                                    }
                                }
                                break;

                            case (PacketType.SYSTEM):
                                SysPacket systemPacket = ((SysPacket)packet);
                                string sysMessage = systemPacket.message;
                                if (systemPacket.sysType.Equals(SystemType.DISCONNECTED))
                                {
                                    sysMessage = "[System] " + client.userName + " " + sysMessage;
                                    Client temp = null;
                                    foreach (Client c in clients)
                                    {
                                        if ( c.GetUsername().Equals(systemPacket.sender))
                                        {
                                            temp = c;
                                        }
                                    }

                                    clients.Remove(temp);

                                    String[] clients2 = clients.ConvertAll(con => con.userName).ToArray();
                                    Console.WriteLine(sysMessage);

                                    foreach (Client c in clients)
                                    {
                                        c.SendTextPacket(c.GetUsername(), sysMessage);
                                        c.SendClientList(clients2);
                                    }
                                }
                                break;

                        }
                    }
            }
            catch (Exception e)
            {
                Console.WriteLine("Error occured: " + e.Message);
            }
            finally
            {
                client.Stop();
            }
        }
        /**
         * Starts the server
         */
        public void Start()
        {
            _tcpListener.Start();

            Console.WriteLine("Listening...");
            while(true)
            {
                Socket socket = _tcpListener.AcceptSocket();
                Client client = new Client(socket);
                _clients.Add(client);
                _userList.Add(client.GetNickname());
                client.Start();
            }
        }
Exemplo n.º 4
0
        public void Start()
        {
            _tcpListener.Start();

            Console.WriteLine("Listening...");

            while (true)
            {
                Socket socket = _tcpListener.AcceptSocket();
                Client client = new Client(socket, "User");
                clients.Add(client);
                client.Start();
            }
        }