Ejemplo n.º 1
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            string recipient = null;

            try
            {
                recipient = listViewUsers.SelectedItems[0].Text;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Please select one recipient");
                return;
            }

            Chatroom.Message msg = new Chatroom.Message("MSG", textboxInput.Text);
            msg.Sender    = username;
            msg.Recipient = recipient;

            byte[] inputStream = Chatroom.SocketHelper.Serialize(msg);
            stream.Write(inputStream, 0, inputStream.Length);
            stream.Flush();

            PrintMsg(username + ":" + textboxInput.Text);
            textboxInput.Text = string.Empty;
        }
Ejemplo n.º 2
0
        private void ReceiveMessageListener(Object obj)
        {
            int i;

            byte[] inputBytes = new byte[1024];

            try
            {
                while ((i = stream.Read(inputBytes, 0, inputBytes.Length)) != 0)
                {
                    Chatroom.Message msg = Chatroom.SocketHelper.Deserialize(inputBytes) as Chatroom.Message;

                    switch (msg.Header)
                    {
                    case "MSG":
                        PrintMsg("From" + msg.Sender + ": " + msg.Data as string);
                        break;

                    case "UPDATEUSER":
                        InitializeListView(msg.Data as string[]);
                        break;

                    default:
                        PrintMsg(msg.Data as string);
                        break;
                    }
                    stream.Flush();
                    inputBytes = new byte[1024];
                }
            }
            catch (IOException ex)
            {
                PrintMsg("Server is shutted down!");
            }
        }
Ejemplo n.º 3
0
 private void SendMessage(TcpClient socket, string header, object data, string sender = null, string recipient = null)
 {
     try
     {
         NetworkStream    nwStream = socket.GetStream();
         Chatroom.Message msg      = new Chatroom.Message(header, data);
         msg.Sender    = sender;
         msg.Recipient = recipient;
         Byte[] replyBytes = Chatroom.SocketHelper.Serialize(msg);
         nwStream.Write(replyBytes, 0, replyBytes.Length);
         nwStream.Flush();
     }
     catch (Exception e)
     {
         Console.WriteLine("Exception: {0}", e.ToString());
     }
 }
Ejemplo n.º 4
0
        private bool LoginByNewUsername(string username)
        {
            NetworkStream stream = client.GetStream();

            Chatroom.Message msg  = new Chatroom.Message("CREATEUSER", username);
            Byte[]           data = Chatroom.SocketHelper.Serialize(msg);

            stream.Write(data, 0, data.Length);
            stream.Flush();
            Console.WriteLine("CREATEUSER:{0}", username);

            data = new Byte[1];
            String responseCode = String.Empty;
            Int32  bytes        = stream.Read(data, 0, data.Length);

            responseCode = Encoding.ASCII.GetString(data);

            if (responseCode == "1")
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 5
0
        private void HandleDevice(Object obj)
        {
            TcpClient client = (TcpClient)obj;
            var       stream = client.GetStream();

            Byte[] bytes      = new Byte[1024];
            Byte[] replyBytes = new Byte[1024];
            int    i;

            try
            {
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    Chatroom.Message msg = Chatroom.SocketHelper.Deserialize(bytes) as Chatroom.Message;

                    switch (msg.Header)
                    {
                    case "CREATEUSER":
                        string newUser = msg.Data as string;
                        Console.WriteLine(">>Register new user: {0}", newUser);
                        string replyCode;
                        if (AddUser(msg.Data as string, client))
                        {
                            replyCode = "1";
                        }
                        else
                        {
                            replyCode = "0";
                        }

                        replyBytes = Encoding.ASCII.GetBytes(replyCode);
                        stream.Write(replyBytes, 0, replyBytes.Length);
                        stream.Flush();
                        Console.WriteLine(">>Reply code: {0}", replyCode);

                        UpdateUserListInAllClients();
                        Broadcast("NOTICE", newUser + " is online");

                        break;

                    case "MSG":
                        TcpClient recipientClient = clientsTable[msg.Recipient] as TcpClient;
                        SendMessage(recipientClient, "MSG", msg.Data as string, msg.Sender, msg.Recipient);
                        Console.WriteLine(msg.Sender + " => " + msg.Recipient + ": " + msg.Data as string);
                        break;

                    default:
                        Console.WriteLine(">>" + msg.Data as string);
                        break;
                    }

                    stream.Flush();
                    bytes = new Byte[1024];
                }
            }
            catch (IOException e)
            {
                if (!client.Connected)
                {
                    string user = GetUserName(client);
                    Console.WriteLine(">>Client " + user + " disconnect");
                    if (user != null)
                    {
                        DeleteUser(user);
                        UpdateUserListInAllClients();
                        Broadcast("NOTICE", user + " is offline");
                    }
                    Console.WriteLine(">>" + user + " is removed");
                }
                else
                {
                    Console.WriteLine("Exception: {0}", e.ToString());
                }
            }
        }