void DropFile(string path, int useID) { try { string filename = path.Replace("/GetFile ", ""); FileInfo fi = new FileInfo(filename); using (FileStream fs = new FileStream(filename, FileMode.Open)) { } EmigrationFileInfo efi = new EmigrationFileInfo() { FileName = fi.Name, Size = fi.Length }; EmigrationObject getfile = new EmigrationObject() { type = "file", message = efi }; byte[] infofilebytes = ObjectToByteArray(getfile); Clients[useID].stream.Write(infofilebytes, 0, infofilebytes.Length); AddConsoleMessage($"{Clients[useID].name} begin to download {fi.Name}"); using (FileStream fs = new FileStream(filename, FileMode.Open)) { byte[] PieceOfFile = new byte[fs.Length]; fs.Read(PieceOfFile, 0, PieceOfFile.Length); Clients[useID].stream.Write(PieceOfFile, 0, PieceOfFile.Length); } } catch (Exception ex) { //Если не можем открыть файл } }
//Отправка обычного сообщения private void ConsoleMessage_KeyDown(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { EmigrationObject obj = new EmigrationObject() { type = "string", message = ConsoleMessage.Text }; byte[] data = ObjectToByteArray(obj); for (int i = 0; i < Clients.Count; i++) { try { Clients[i].stream.Write(data, 0, data.Length); } catch (Exception ex) { Title = ex.Message; AddConsoleMessage($"{Clients[i].name} отключен"); Clients.RemoveAt(i); } ConsoleMessage.Text = ""; } } }
void TakeFile(int userID) { NetworkStream currStrm = Clients[userID].stream; byte[] buf = new byte[11000]; currStrm.Read(buf, 0, buf.Length); EmigrationFileInfo efi = (EmigrationFileInfo)ByteArrayToObject(buf); string path = efi.FileName; int cntFiles = (int)efi.Size; for (int i = 0; i < cntFiles; i++) { buf = new byte[11000]; currStrm.Read(buf, 0, buf.Length); efi = (EmigrationFileInfo)ByteArrayToObject(buf); using (FileStream fs = new FileStream(path + "\\" + efi.FileName, FileMode.Create)) { long currentBytes = 0; byte[] data; while (currentBytes < efi.Size) { data = new byte[1]; currStrm.Read(data, 0, data.Length); fs.WriteByte(data[0]); currentBytes++; } } } //DownloadComplete EmigrationObject em = new EmigrationObject() { type = "DownloadComplete" }; buf = ObjectToByteArray(em); Clients[userID].stream.Write(buf, 0, buf.Length); Clients[userID].IsBusy = false; }
void useCommand(string message, int useID) { string command = message.Split()[0]; switch (command) { case "/GetFile": DropFile(message, useID); break; case "/SayHello": EmigrationObject obj = new EmigrationObject() { type = "string", message = "Hello user!!!" }; byte[] bytes = ObjectToByteArray(obj); Clients[useID].stream.Write(bytes, 0, bytes.Length); AddConsoleMessage($"{Clients[useID].name} use say hello"); break; case "/FolderInfo": if (message == "/FolderInfo") { List <EmigrationPathInfo> allfilesDirectory = new List <EmigrationPathInfo>(); DirectoryInfo di = new DirectoryInfo(ServerFolderPath); allfilesDirectory.Add(new EmigrationPathInfo() { Name = System.IO.Path.GetFullPath(ServerFolderPath) }); di.GetDirectories().ToList().ForEach(a => allfilesDirectory.Add(new EmigrationPathInfo() { Name = a.Name, isFile = false })); di.GetFiles().ToList().ForEach(a => allfilesDirectory.Add(new EmigrationPathInfo() { Name = a.Name, isFile = true, ImagePath = IconToBytes(System.Drawing.Icon.ExtractAssociatedIcon(a.FullName)) })); EmigrationObject listobjj = new EmigrationObject() { type = "List<EmigrationPathInfo>", message = allfilesDirectory }; bytes = ObjectToByteArray(listobjj); Clients[useID].stream.Write(bytes, 0, bytes.Length); AddConsoleMessage($"{Clients[useID].name} use fileinfo"); } else { string path = message.Replace("/FolderInfo ", ""); if (File.Exists(path)) { DropFile(path, useID); } else if (Directory.Exists(path + "\\") || DriveInfo.GetDrives().Where(a => a.Name == path + "\\").Count() != 0) { path = path + "\\"; List <EmigrationPathInfo> allfilesDirectory = new List <EmigrationPathInfo>(); try { DirectoryInfo di = new DirectoryInfo(path); allfilesDirectory.Add(new EmigrationPathInfo() { Name = System.IO.Path.GetFullPath(message.Replace("/FolderInfo ", "") + @"\") }); di.GetDirectories().ToList().ForEach(a => allfilesDirectory.Add(new EmigrationPathInfo() { Name = a.Name, isFile = false })); di.GetFiles().ToList().ForEach(a => allfilesDirectory.Add(new EmigrationPathInfo() { isFile = true, Name = a.Name, ImagePath = IconToBytes(System.Drawing.Icon.ExtractAssociatedIcon(a.FullName)) })); EmigrationObject throwlist = new EmigrationObject() { type = "List<EmigrationPathInfo>", message = allfilesDirectory }; bytes = ObjectToByteArray(throwlist); Clients[useID].stream.Write(bytes, 0, bytes.Length); //AddConsoleMessage($"{Clients[useID].name} use fileinfo"); } catch (Exception ex) { return; } } else { return; } } break; case "/GetVersion": bytes = Encoding.UTF8.GetBytes(currentVersion); Clients[useID].stream.Write(bytes, 0, bytes.Length); AddConsoleMessage($"{Clients[useID].name} use get version"); break; case "/GetApp": int num = int.Parse(message.Split()[1]); DirectoryInfo dio = new DirectoryInfo(@"C:\Users\SelectiveHoe\Documents\visual studio 2015\Projects\WpfClientServerTest\WpfConnectClient\bin\Debug"); DropFile(dio.GetFiles()[num].FullName, useID); break; case "/HardDriveInfo": var bitmap = new Bitmap("Drive-icon.png"); var iconHandle = bitmap.GetHicon(); var icon = System.Drawing.Icon.FromHandle(iconHandle); List <EmigrationPathInfo> alldrivers = new List <EmigrationPathInfo>(); alldrivers.Add(new EmigrationPathInfo() { Name = "" }); DriveInfo.GetDrives().ToList().ForEach(a => { alldrivers.Add(new EmigrationPathInfo() { Name = a.Name, ImagePath = IconToBytes(icon), isFile = false }); }); EmigrationObject listobj = new EmigrationObject() { type = "List<EmigrationPathInfo>", message = alldrivers }; bytes = ObjectToByteArray(listobj); Clients[useID].stream.Write(bytes, 0, bytes.Length); break; case "/DropFile": Clients[useID].IsBusy = true; Task.Run(() => TakeFile(useID)); break; case "/FileExist": string pathF = message.Replace("/FileExist", ""); if (File.Exists(pathF)) { bytes = Encoding.UTF8.GetBytes("true"); Clients[useID].stream.Write(bytes, 0, bytes.Length); } else { bytes = Encoding.UTF8.GetBytes("false"); Clients[useID].stream.Write(bytes, 0, bytes.Length); } break; default: AddConsoleMessage(message); break; } }
static void Main() { try { _Client.Connect("77.93.61.5", 21025); stream = _Client.GetStream(); Console.WriteLine("Connection status: Online."); string message = Environment.MachineName; byte[] data = Encoding.UTF8.GetBytes(message); stream.Write(data, 0, data.Length); int bytes = 0; data = new byte[1024]; bytes = stream.Read(data, 0, data.Length); message = Encoding.UTF8.GetString(data, 0, bytes); Console.WriteLine(message); data = Encoding.UTF8.GetBytes("/GetVersion"); stream.Write(data, 0, data.Length); bytes = 0; data = new byte[1024]; bytes = stream.Read(data, 0, data.Length); string clientVersion = Encoding.UTF8.GetString(data, 0, bytes); string currentVersion; using (FileStream fs = new FileStream("version.cfg", FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { currentVersion = sr.ReadToEnd(); } } currentVersion = currentVersion.Replace("version: ", ""); if (clientVersion == currentVersion) { string path = System.Environment.CurrentDirectory + "\\App\\WpfConnectClient.exe"; System.Diagnostics.Process.Start(path); } else { DirectoryInfo di = new DirectoryInfo("App"); di.GetFiles().ToList().ForEach(a => a.Delete()); //Шлём запрос на получение фалов программы for (int i = 0; i < 9; i++) { message = $"/GetApp {i}"; data = Encoding.UTF8.GetBytes(message); stream.Write(data, 0, data.Length); data = new byte[500000]; bytes = stream.Read(data, 0, data.Length); EmigrationObject eo = ByteArrayToObject(data) as EmigrationObject; if (eo.type == "file") { EmigrationFileInfo efi = (EmigrationFileInfo)eo.message; DownloadFile(efi); } } Console.WriteLine("Update download complete..."); using (FileStream fs = new FileStream("version.cfg", FileMode.Create)) { using (StreamWriter sw = new StreamWriter(fs)) { sw.Write("version: " + clientVersion); } } string path = System.Environment.CurrentDirectory + "\\App\\WpfConnectClient.exe"; System.Diagnostics.Process.Start(path); } } catch (Exception ex) { Console.WriteLine("I can`t connect...."); } }