private void action_get_media() { var s = _readdata.Split(':'); var sender = ""; var ext = ""; if (s.Length >= 3) { sender = s[2]; } if (s.Length >= 4) { ext = s[3]; } if (File.Exists(sender + "_" + Nickname)) { var f = new FileStream(sender + "_" + Nickname, FileMode.Open); var fi = new FileInfo(sender + "_" + Nickname); var b = new byte[fi.Length]; f.Read(b, 0, b.Length); f.Close(); var unicode = new UnicodeEncoding(); var charCount = unicode.GetCharCount(b, 0, b.Length); var chars = new char[charCount]; unicode.GetChars(b, 0, b.Length, chars, 0); ext = new string(chars); } var medianame = sender + "_" + Nickname + "." + ext; if (!File.Exists(medianame)) { _chatserver.Write(_client.GetStream(), ChatProtocolValues.MEDIA_NOT_FOUND_MSG(medianame)); } else { var f = new FileStream(medianame, FileMode.Open); var fi = new FileInfo(medianame); var b = new byte[fi.Length]; f.Read(b, 0, b.Length); f.Close(); _chatserver.Write(_client.GetStream(), ChatProtocolValues.GetMediaMsg); _chatserver.Write(_client.GetStream(), ext); _chatserver.Write(_client.GetStream(), "" + b.Length); _chatserver.WriteBinary(_client.GetStream(), b); _chatserver.Write(_client.GetStream(), ChatProtocolValues.MediaSendAckMsg); TcpClient t = null; if (_chatserver.FindUserRoom(sender) != 0) { t = (TcpClient)_chatserver.ClientConnections[sender.ToUpper()]; } if (t != null) { _chatserver.Write(t.GetStream(), ChatProtocolValues.GOTTEN_MEDIA_MSG(Nickname)); } } }
//GET MEDIA private void action_get_media() { string[] s = readdata.Split(':'); string sender = ""; string medianame = ""; string ext = ""; //format is //:get media:<sender>:ext if (s.Length >= 3) { sender = s[2]; } if (s.Length >= 4) { ext = s[3]; } //format of saved jpg file is //<sender>_<target>.jpg //In this case the current user is the target //get the extension form the file if (File.Exists(sender + "_" + nickname)) { FileStream f = new FileStream(sender + "_" + nickname, FileMode.Open); FileInfo fi = new FileInfo(sender + "_" + nickname); byte[] b = new byte[fi.Length]; f.Read(b, 0, b.Length); f.Close(); UnicodeEncoding Unicode = new UnicodeEncoding(); int charCount = Unicode.GetCharCount(b, 0, b.Length); char[] chars = new Char[charCount]; Unicode.GetChars(b, 0, b.Length, chars, 0); ext = new string(chars); } medianame = sender + "_" + nickname + "." + ext; //Check for existence of file if (!File.Exists(medianame)) { chatserver.Write(client.GetStream(), ChatProtocolValues.MEDIA_NOT_FOUND_MSG(medianame)); } else { //Create a file stream FileStream f = new FileStream(medianame, FileMode.Open); //To get the size of the file for purpose of memory allocation FileInfo fi = new FileInfo(medianame); byte[] b = new byte[fi.Length]; //Read the content of the file and close f.Read(b, 0, b.Length); f.Close(); //Inform the client to get the media chatserver.Write(client.GetStream(), ChatProtocolValues.GET_MEDIA_MSG); //Inform the client of the extension //chatserver.Write(client.GetStream(),ext); //Inform the client of the ext chatserver.Write(client.GetStream(), ext); //Inform the client of number of bytes chatserver.Write(client.GetStream(), "" + b.Length); //Send the binary data chatserver.WriteBinary(client.GetStream(), b); //Inform the client that all binary data has been send chatserver.Write(client.GetStream(), ChatProtocolValues.MEDIA_SEND_ACK_MSG); //Locate the sender of the media TcpClient t = null; if (chatserver.FindUserRoom(sender) != 0) { t = (TcpClient)chatserver.ClientConnections[sender.ToUpper()]; } //Inform the sender that the target has gotten the media if (t != null) { chatserver.Write(t.GetStream(), ChatProtocolValues.GOTTEN_MEDIA_MSG(nickname)); } } }