public ServerThread(TcpClient connection) { dataBase = new LocalDB(); fileController = new FileController(); tcpConnection = connection; connectionStream = tcpConnection.GetStream(); StandardRequestModel request = readStream(); if (request == null) { stopConnection(); return; } ClientInfoModel Client = request.Client; if (Client == null) { stopConnection(); } loginClient(request); //Verarbeitung des Request StandardResultModel result = getResult(request); sendStringStream(result); if (result.type == ResultType.sendPackage) { List<PackageInfoModel> packageList = (List<PackageInfoModel>)result.result; GroupInfoModel group = null; if (packageList.Count != 0) { ClientInfoModel resultClient = (ClientInfoModel)result.result; if (resultClient != null) { sendFile(group, packageList[0]); dataBase.ClientGetSoftware(resultClient, packageList[0]); } } } if (result.type == ResultType.readPackage) { string strInfo =(string)request.requestData; char[] delimiter1 = new char[] { ',' }; string[] strArray = strInfo.Split(delimiter1, StringSplitOptions.None); if (strArray.Length == 2) { readFile(strArray[0], strArray[1]); } } stopConnection(); }
private void readFile(string strGroupName, string strPackageName) { Socket clientSock = tcpConnection.Client; byte[] ClientData = new byte[1024 * 5000]; FileController fileController = new FileController(); string receivedPath = fileController.getPathFromGroup(strGroupName); int receivedBytesLen = clientSock.Receive(ClientData); int fileNameLen = BitConverter.ToInt32(ClientData, 0); string fileName = Encoding.ASCII.GetString(ClientData, 4, fileNameLen); Console.WriteLine("Client:{0} connected & File {1} started received.", clientSock.RemoteEndPoint, fileName); BinaryWriter bWrite = new BinaryWriter(File.Open(receivedPath + fileName, FileMode.Append)); try { bWrite.Write(ClientData, 4 + fileNameLen, receivedBytesLen - 4 - fileNameLen); } catch (Exception ex) { Diagnostics.WriteToEventLog(ex.Message, EventLogEntryType.Error, 3002); } bWrite.Close(); Console.WriteLine("File: {0} received & saved at path: {1}", fileName, receivedPath); }
private void sendFile(GroupInfoModel group, PackageInfoModel package) { FileController fileController = new FileController(); string filePath = fileController.getPathFromFile(group.Name, package.Name); if (!File.Exists(filePath)) { return; } byte[] fileName = Encoding.ASCII.GetBytes(package.Name); byte[] fileData = File.ReadAllBytes(filePath); byte[] fileNameLen = BitConverter.GetBytes(fileName.Length); byte[] file = new byte[4 + fileName.Length + fileData.Length]; fileNameLen.CopyTo(file, 0); fileName.CopyTo(file, 4); fileData.CopyTo(file, 4 + fileName.Length); try { tcpConnection.Client.Send(file); Console.WriteLine("File: {0} has been sent.", fileName); } catch (Exception ex) { Diagnostics.WriteToEventLog(ex.Message, EventLogEntryType.Error, 3003); } stopConnection(); }
static void Main(string[] args) { //List<ClientInfoModel> olist = new List<ClientInfoModel>(); LocalDB oDB = new LocalDB(); FileController file = new FileController(oDB.Converter.GetGroupInfoModels()); Controller.Diagnostics.EventName = "Softwareverteilung2013"; Controller.Diagnostics.WriteToEventLog("Server wird gestartet", System.Diagnostics.EventLogEntryType.Information, 1000); Console.WriteLine("starting..."); connection = new ServerConnection(); String cmd = ""; while (cmd.ToLower() != "stop") { cmd = Console.ReadLine(); if (cmd.ToLower().Equals("help")) { string commands = CommandModel.getCommands(); Console.WriteLine(commands); } else if (cmd.ToLower().Equals("clear")) { Console.Clear(); } else if (cmd.ToLower().Equals("delPackage")) { Console.WriteLine("Noch nicht Implementiert"); } else if (cmd.ToLower().Equals("delGroup")) { Console.WriteLine("Noch nicht Implementiert"); } else if (cmd.ToLower().Equals("packages")) { Console.WriteLine("Noch nicht Implementiert"); } else if (cmd.ToLower().Equals("groups")) { List<GroupInfoModel> groups = oDB.Converter.GetGroupInfoModels(); string groupNames = ""; foreach (var group in groups) { groupNames = "ID:" + group.ID + " Name:" + group.Name; Console.WriteLine(groupNames); } } else if (cmd.ToLower().Equals("groupclients")) { List<GroupInfoModel> groups = oDB.Converter.GetGroupInfoModels(); for (int i = 0; i < groups.Count; i++) { Console.WriteLine("ID:" + groups[i].ID + " Name:" + groups[i].Name); Console.WriteLine(); List<ClientInfoModel> clients = oDB.Converter.GetGroupClients(groups[i]); string groupNames = ""; foreach (var client in clients) { groupNames = "ID:" + client.ID + " pcName:" + client.pcName; Console.WriteLine(groupNames); } Console.WriteLine(); } } else if (cmd.ToLower().Equals("clients")) { List<ClientInfoModel> oList = oDB.Converter.GetClientInfoModels(); for (int i = 0; i < oList.Count; i++) { Console.WriteLine(oList[i].pcName.ToString() + " " + oList[i].group.ToString()); } } else if (!cmd.ToLower().Equals("stop")) Console.WriteLine("Unbekannter Befehl: " + cmd); } connection.stopServer(); }