示例#1
0
        //SEND PIC
        private void action_send_pic()
        {
            string[] s    = readdata.Split(':');
            string   name = "";

            //format is
            //:send pic:<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 picture
                chatserver.Write(client.GetStream(), ChatProtocolValues.SEND_PIC_MSG);

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

                //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 jpg file
                //name convention is <sender>_<target>.jpg
                FileStream f = new FileStream(nickname + "_" + name + ".jpg", FileMode.Create);
                f.Write(b, 0, b.Length);
                f.Close();
                //Inform the target that there is a picture from sender
                chatserver.Write(t.GetStream(),
                                 ChatProtocolValues.PIC_FROM_MSG(nickname, name));
                //Inform the sender that server had received the picture
                chatserver.Write(client.GetStream(),
                                 ChatProtocolValues.PIC_SEND_MSG(nickname));
            }
            else
            {
                //If target is not found inform sender
                chatserver.Write(client.GetStream(),
                                 ChatProtocolValues.USER_NOT_FOUND_MSG(name));
            }
        }
示例#2
0
        private void action_send_pic()
        {
            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.SendPicMsg);
                var snumbytes = _chatserver.Read(_client.GetStream());
                var numbytes  = int.Parse(snumbytes);
                var b         = _chatserver.ReadBinary(_client.GetStream(), numbytes);
                if (b == null)
                {
                    _chatserver.Write(_client.GetStream(),
                                      "server> Transmission Error");
                    return;
                }
                var f = new FileStream(Nickname + "_" + name + ".jpg", FileMode.Create);
                f.Write(b, 0, b.Length);
                f.Close();
                _chatserver.Write(t.GetStream(),
                                  ChatProtocolValues.PIC_FROM_MSG(Nickname, name));
                _chatserver.Write(_client.GetStream(),
                                  ChatProtocolValues.PIC_SEND_MSG(Nickname));
            }
            else
            {
                _chatserver.Write(_client.GetStream(),
                                  ChatProtocolValues.USER_NOT_FOUND_MSG(name));
            }
        }
示例#3
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