コード例 #1
0
ファイル: SocketHelper.cs プロジェクト: SeppPenner/ChatNew
        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));
            }
        }
コード例 #2
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));
            }
        }