//GET PIC private void action_get_pic() { string[] s = readdata.Split(':'); string sender = ""; string picname = ""; //format is //:get pic:<sender> if (s.Length == 3) { sender = s[2]; } //format of saved jpg file is //<sender>_<target>.jpg //In this case the current user is the target picname = sender + "_" + nickname + ".jpg"; //Check for existence of file if (!File.Exists(picname)) { chatserver.Write(client.GetStream(), ChatProtocolValues.PIC_NOT_FOUND_MSG(picname)); } else { //Create a file stream FileStream f = new FileStream(picname, FileMode.Open); //To get the size of the file for purpose of memory allocation FileInfo fi = new FileInfo(picname); 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 pic chatserver.Write(client.GetStream(), ChatProtocolValues.GET_PIC_MSG); //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.PIC_SEND_ACK_MSG); //Locate the sender of the picture TcpClient t = null; if (chatserver.FindUserRoom(sender) != 0) { t = (TcpClient)chatserver.ClientConnections[sender.ToUpper()]; } //Inform the sender that the target has gotten the picture if (t != null) { chatserver.Write(t.GetStream(), ChatProtocolValues.GOTTEN_PIC_MSG(nickname)); } } }
private void action_get_pic() { var s = _readdata.Split(':'); var sender = ""; if (s.Length == 3) { sender = s[2]; } var picname = sender + "_" + Nickname + ".jpg"; if (!File.Exists(picname)) { _chatserver.Write(_client.GetStream(), ChatProtocolValues.PIC_NOT_FOUND_MSG(picname)); } else { var f = new FileStream(picname, FileMode.Open); var fi = new FileInfo(picname); var b = new byte[fi.Length]; f.Read(b, 0, b.Length); f.Close(); _chatserver.Write(_client.GetStream(), ChatProtocolValues.GetPicMsg); _chatserver.Write(_client.GetStream(), "" + b.Length); _chatserver.WriteBinary(_client.GetStream(), b); _chatserver.Write(_client.GetStream(), ChatProtocolValues.PicSendAckMsg); TcpClient t = null; if (_chatserver.FindUserRoom(sender) != 0) { t = (TcpClient)_chatserver.ClientConnections[sender.ToUpper()]; } if (t != null) { _chatserver.Write(t.GetStream(), ChatProtocolValues.GOTTEN_PIC_MSG(Nickname)); } } }