Esempio n. 1
0
        private void action_send_media()
        {
            var s    = _readdata.Split(':');
            var name = "";

            if (s.Length == 3)
            {
                name = s[2];
            }
            TcpClient t = null;

            if (_chatserver.FindUserRoom(name) != 0)
            {
                t = (TcpClient)_chatserver.ClientConnections[name.ToUpper()];
            }
            if (t != null)
            {
                _chatserver.Write(_client.GetStream(), ChatProtocolValues.SendMediaMsg);
                var ext       = _chatserver.Read(_client.GetStream());
                var snumbytes = _chatserver.Read(_client.GetStream());
                var numbytes  = int.Parse(snumbytes);
                if (numbytes == 0)
                {
                    _chatserver.Write(_client.GetStream(),
                                      "server> No media file to send");
                    return;
                }
                if (numbytes > 51200000)
                {
                    _chatserver.Write(_client.GetStream(),
                                      "server> Media File is larger than 5 MB");
                    return;
                }
                var b = _chatserver.ReadBinary(_client.GetStream(), numbytes);
                if (b == null)
                {
                    _chatserver.Write(_client.GetStream(),
                                      "server> Transmission Error");
                    return;
                }
                var fext    = new FileStream(Nickname + "_" + name, FileMode.Create);
                var unicode = new UnicodeEncoding();
                var bytes   = unicode.GetBytes(ext);
                fext.Write(bytes, 0, bytes.Length);
                fext.Close();
                var f = new FileStream(Nickname + "_" + name + "." + ext, FileMode.Create);
                f.Write(b, 0, b.Length);
                f.Close();
                _chatserver.Write(t.GetStream(),
                                  ChatProtocolValues.MEDIA_FROM_MSG(Nickname, name));
                _chatserver.Write(_client.GetStream(),
                                  ChatProtocolValues.MEDIA_SEND_MSG(Nickname));
            }
            else
            {
                _chatserver.Write(_client.GetStream(),
                                  ChatProtocolValues.USER_NOT_FOUND_MSG(name));
            }
        }
Esempio n. 2
0
        //read and response to mesaages
        public void Listen()
        {
            //To ensure that the form is activated
            while (!form_activated)
            {
                Thread.Sleep(0);
            }

            try
            {
                while (true)
                {
                    action = null;

                    //waiting to read
                    responseData = _chatstream.Read();

                    //if asked to pause
                    //while paused data may have been used by another method
                    while (pause_listening)
                    {
                        Thread.Sleep(0);
                    }

                    //check if data had been cleared
                    if (responseData == "")
                    {
                        continue;
                    }

                    if (this.WindowState == FormWindowState.Minimized)
                    {
                        FlashWindow(this.Handle.ToInt32(), true);
                    }


                    //default asction
                    if (responseData != "")
                    {
                        action = new ClientAction(action_message);
                    }

                    //Perform this only if checkbox is checked
                    if (checkbox.Checked)
                    {
                        if (responseData.IndexOf(ChatProtocolValues.PIC_FROM_MSG("", nickname)) == 0)
                        {
                            action = new ClientAction(action_auto_get_pic);
                        }
                    }

                    if (checkbox.Checked)
                    {
                        if (responseData.IndexOf(ChatProtocolValues.MEDIA_FROM_MSG("", nickname)) == 0)
                        {
                            action = new ClientAction(action_auto_get_media);
                        }
                    }

                    //Special messages from server
                    //signal to quit from server
                    if (responseData.IndexOf(ChatProtocolValues.QUIT_MSG) == 0)
                    {
                        action = new ClientAction(action_server_says_quit);
                    }

                    //Connection established
                    if (responseData.IndexOf(ChatProtocolValues.CONNECTION_HEADER_MSG) == 0)
                    {
                        action = new ClientAction(action_connection);
                    }

                    //signal to get picture
                    if (responseData == ChatProtocolValues.GET_PIC_MSG)
                    {
                        action = new ClientAction(action_server_get_pic);
                    }

                    //Signal to send picture
                    if (responseData == ChatProtocolValues.SEND_PIC_MSG)
                    {
                        action = new ClientAction(action_server_send_pic);
                    }

                    //signal to get media
                    if (responseData == ChatProtocolValues.GET_MEDIA_MSG)
                    {
                        action = new ClientAction(action_server_get_media);
                    }

                    //Signal to send media
                    if (responseData == ChatProtocolValues.SEND_MEDIA_MSG)
                    {
                        action = new ClientAction(action_server_send_media);
                    }


                    //perform the action
                    if (action != null)
                    {
                        action();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());

                action = new ClientAction(action_handle_error);
                action();
            }
        }        //LISTEN
Esempio n. 3
0
        //SEND MEDIA
        private void action_send_media()
        {
            string[] s    = readdata.Split(':');
            string   name = "";

            //format is
            //:send media:<target>
            if (s.Length == 3)
            {
                name = s[2];
            }

            //Locate the target
            TcpClient t = null;

            if (chatserver.FindUserRoom(name) != 0)
            {
                t = (TcpClient)chatserver.ClientConnections[name.ToUpper()];
            }


            //If target is found
            if ((t != null))
            {
                //Inform the sender(client) to send the media
                chatserver.Write(client.GetStream(), ChatProtocolValues.SEND_MEDIA_MSG);

                string ext = chatserver.Read(client.GetStream());
                //Find out the number of byte to read from sender
                string snumbytes = chatserver.Read(client.GetStream());
                int    numbytes  = int.Parse(snumbytes);

                if (numbytes == 0)
                {
                    chatserver.Write(client.GetStream(),
                                     "server> No media file to send");

                    return;
                }

                //must be less than 5 MB
                if (numbytes > 51200000)
                {
                    chatserver.Write(client.GetStream(),
                                     "server> Media File is larger than 5 MB");

                    return;
                }

                //read the bytes
                byte[] b = chatserver.ReadBinary(client.GetStream(), numbytes);


                if (b == null)
                {
                    chatserver.Write(client.GetStream(),
                                     "server> Transmission Error");

                    return;
                }

                //To store the data in a file
                //name convention is <sender>_<target>.ext

                //create a file to hold the extension
                FileStream      fext    = new FileStream(nickname + "_" + name, FileMode.Create);
                UnicodeEncoding Unicode = new UnicodeEncoding();
                byte[]          bytes   = Unicode.GetBytes(ext);
                fext.Write(bytes, 0, bytes.Length);
                fext.Close();

                FileStream f = new FileStream(nickname + "_" + name + "." + ext, FileMode.Create);
                f.Write(b, 0, b.Length);
                f.Close();
                //Inform the target that there is a file from sender
                chatserver.Write(t.GetStream(),
                                 ChatProtocolValues.MEDIA_FROM_MSG(nickname, name));
                //Inform the sender that server had received the media
                chatserver.Write(client.GetStream(),
                                 ChatProtocolValues.MEDIA_SEND_MSG(nickname));
            }
            else
            {
                //If target is not found inform sender
                chatserver.Write(client.GetStream(),
                                 ChatProtocolValues.USER_NOT_FOUND_MSG(name));
            }
        }