private void action_private_message() { var s = _readdata.Split(':'); var name = "null_name"; var temp = ""; if (s.Length >= 3) { name = s[2].Trim(); } if (s.Length >= 4) { temp = s[3].Trim(); } TcpClient t = null; if (_chatserver.FindUserRoom(name) != 0) { t = (TcpClient)_chatserver.ClientConnections[name.ToUpper()]; } if (t != null) { _chatserver.Write(t.GetStream(), ChatProtocolValues.NORMAL_MSG(Nickname, temp)); _chatserver.Write(_client.GetStream(), ChatProtocolValues.NORMAL_MSG(Nickname, temp)); } else { _chatserver.Write(_client.GetStream(), ChatProtocolValues.USER_NOT_FOUND_MSG(name)); } }
//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)); } }
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)); } }
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)); } }
//PRIVATE MESSAGE private void action_private_message() { string[] s = readdata.Split(':'); string name = "null_name"; //give a default dummy name string temp = ""; //hold the message //format is //:private:<target>:<message> if (s.Length >= 3) { name = s[2].Trim(); } if (s.Length >= 4) { temp = s[3].Trim(); } TcpClient t = null; if (chatserver.FindUserRoom(name) != 0) { t = (TcpClient)chatserver.ClientConnections[name.ToUpper()]; } if (t != null) { //to target chatserver.Write(t.GetStream(), ChatProtocolValues.NORMAL_MSG(nickname, temp)); //to inform sender chatserver.Write(client.GetStream(), ChatProtocolValues.NORMAL_MSG(nickname, temp)); } else { chatserver.Write(client.GetStream(), ChatProtocolValues.USER_NOT_FOUND_MSG(name)); } }
//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)); } }