public void SendFile(string filePath, NetworkStream stream) { if (!string.IsNullOrEmpty(filePath)) { // файл есть, отдаём using (var fileIO = File.OpenRead(filePath)) { //stream.Write(BitConverter.GetBytes(fileIO.Length), 0, 8); var b = BitConverter.GetBytes(fileIO.Length); //fileIO.Length.ToString().Length //stream.Write(b, 0, fileIO.Length.ToString().Length); stream.Write(b, 0, b.Length); var buffer = new byte[1024 * 8]; int count; long allSize = fileIO.Length; long howTransmit = 0; while ((count = fileIO.Read(buffer, 0, buffer.Length)) > 0) { stream.Write(buffer, 0, count); howTransmit += count; SendingEvent?.Invoke(howTransmit / allSize * 100); } } } else {// файл отсутствует var data = BitConverter.GetBytes((int)RequestError.FileNotExist); stream.Write(data, 0, data.Length); } }
public void SendData(ControlsUpdate controlUpdate) { JavaScriptSerializer serializer = new JavaScriptSerializer();//this is the way we start serializer //serializer is getting the controlUpdate struct that has been passed onto the SendData function in the class sender to update controlUpdate variables byte[] Data = Encoding.ASCII.GetBytes(serializer.Serialize(controlUpdate)); //this is an array of rawData that we receive from the deserialization networkStream.Write(Data, 0, Data.Length); //this is just like writing to a file however we write to a another program via socket programming using network stream SendingEvent?.Invoke(controlUpdate); //we use the event to add the data into controlUpdate }
public void SendData(ControlsUpdate controlUpdate) //function called to encode data to be sent to simulator { string dataToSend; //string to hold data being sent dataToSend = JsonConvert.SerializeObject(controlUpdate); //converting the control instructions to json and storing it in dataToSend byte[] rawData = Encoding.ASCII.GetBytes(dataToSend); //converts string to bytes passing in string to get bytes function stream.Write(rawData, 0, rawData.Length); // write to stream (ASCII data, starting at inex 0, ending at last index.) SendingEvent?.Invoke(controlUpdate); //invokes a new sending event with instance of controls update }
public async void SendFileRequestFromMe(object obj) { await Task.Run(() => { try { string[] mes = obj.ToString().Split('|'); string localPathFile = mes[1]; string remotePathToSave = mes[0]; var reciverEP = new IPEndPoint(RemoteIP, port); client.Connect(reciverEP);// Подключаемся к удаленному пк while (!client.Connected) { ; } var connectedStream = client.GetStream(); //Request.DoSendingRequest() var mess = $"Request|{(int)RequestTipe.SendFileFromMe}|{remotePathToSave}"; var buf = Encoding.UTF8.GetBytes(mess); connectedStream.Write(buf, 0, buf.Length);//Отсылаем сообщение куда будем сохранять файл Int64 bytesReceived = 0; byte[] buffer = new byte[1024 * 8]; //connectedStream.Read(buffer, 0, 1024);//Ожидаем ответа byte[] mes_result1 = new byte[1024 * 8]; var requestLen1 = connectedStream.Read(mes_result1, 0, 1024);//Ждем ответ string request1 = Encoding.UTF8.GetString(mes_result1, 0, requestLen1); if (request1 == "ready")//Если он готов получить файл { if (!string.IsNullOrEmpty(localPathFile)) { // файл есть, отдаём using (var fileIO = File.OpenRead(localPathFile)) { //stream.Write(BitConverter.GetBytes(fileIO.Length), 0, 8); var b = BitConverter.GetBytes(fileIO.Length); //fileIO.Length.ToString().Length //stream.Write(b, 0, fileIO.Length.ToString().Length); connectedStream.Write(b, 0, b.Length);//Отсылаем размер файла var buffer1 = new byte[1024 * 8]; int count; double allSize = fileIO.Length; double howTransmit = 0; while ((count = fileIO.Read(buffer, 0, buffer.Length)) > 0) { connectedStream.Write(buffer, 0, count); howTransmit += count; double res = howTransmit / allSize * 100; SendingEvent?.Invoke(res); } } } else {// файл отсутствует var data = BitConverter.GetBytes((int)RequestError.FileNotExist); connectedStream.Write(data, 0, data.Length); } } else { FailEvent?.Invoke(); } byte[] mes_result = new byte[1024 * 8]; var requestLen = connectedStream.Read(mes_result, 0, 1024);//Ждем ответ string request = Encoding.UTF8.GetString(mes_result, 0, requestLen); if (request == "successfully") { ReadyEvent?.Invoke(); } else { FailEvent?.Invoke(); } connectedStream.Close(); client.Close(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); FailEvent?.Invoke(); } }); }
/// <summary> /// куда|откуда /// </summary> /// <param name="obj"></param> public async void SendFileRequest(object obj) { await Task.Run(() => { try { string[] mes = obj.ToString().Split('|'); string remoteFilePath = mes[1]; string localPathToSave = mes[0]; var reciverEP = new IPEndPoint(RemoteIP, port); client.Connect(reciverEP); while (!client.Connected) { ; } var connectedStream = client.GetStream(); //Request.DoSendingRequest() var mess = $"Request|{(int)RequestTipe.SendFile}|{remoteFilePath}"; var buf = Encoding.UTF8.GetBytes(mess); connectedStream.Write(buf, 0, buf.Length); Int64 bytesReceived = 0; int count; byte[] buffer = new byte[1024 * 8]; connectedStream.Read(buffer, 0, 1024); Int64 fileBytesSize = BitConverter.ToInt64(buffer, 0); if (fileBytesSize == -1) { } // файл был не найден if (fileBytesSize == -2) { }// запрос небыл обработан bool peremen = Execute_(localPathToSave); if (peremen) { using (var fileIO = File.Create(localPathToSave)) { while (bytesReceived < fileBytesSize && (count = connectedStream.Read(buffer, 0, buffer.Length)) > 0) { fileIO.Write(buffer, 0, count); bytesReceived += count; double first = bytesReceived; double second = fileBytesSize; SendingEvent?.Invoke(first / second * 100); } SendingEvent?.Invoke(100); } ReadyEvent?.Invoke(); } else { FailEvent?.Invoke(); } connectedStream.Close(); client.Close(); } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.Message); FailEvent?.Invoke(); } }); }