using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Sockets; using System.Text; using System.Threading.Tasks; using System.Net; using Newtonsoft.Json; namespace sikFtpClient.Connection { class Client : Connection { public Client(string server, int port) { this.server = server; this.port = port; } public bool Connect() { try { client = new TcpClient(server, port); stream = client.GetStream(); //clientsocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //clientsocket.Connect(server, port); return true; } catch { return false; } } public void SendFile(string file, string destPath = @"C:\Users\Lenovo\Desktop\") { int bufferSize = 1024; byte[] filebuff = new byte[bufferSize]; string fileName = destPath + file; //send to TcpServer request to send file //clientsocket.Send(textToBytes("RECEIVEFILE")); stream.Write(textToBytes("RECEIVEFILE"), 0, 11); //jak to ładniej zapisać? try { FileStream streamFile = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader binReader = new BinaryReader(streamFile); //get file size long filesize = streamFile.Length; //send file name to TcpServer //clientsocket.Send(textToBytes(file)); stream.Write(textToBytes(file), 0, file.Length); //send file size to TcpServer // sendData(stream, BitConverter.GetBytes(filesize)); // clientsocket.Send(BitConverter.GetBytes(filesize)); //if file doesn't exist if (file == null) { Console.WriteLine("Błąd przy otwarciu pliku."); } //if file is empty if (filesize == 0) { Console.WriteLine("Rozmiar pliku: 0"); return; } //totallength? int totalLength = Convert.ToInt32(filesize); long numberOfPackets = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(streamFile.Length) / bufferSize)); int currentPacketLength; Console.WriteLine("Liczba pakietów: " + numberOfPackets); for (int i = 0; i < numberOfPackets; i++) { if (filesize > bufferSize) { currentPacketLength = bufferSize; totalLength = totalLength - currentPacketLength; } else currentPacketLength = totalLength; filebuff = new byte[currentPacketLength]; //streamFile.Read(filebuff, 0, currentPacketLength); binReader.Read(filebuff, 0, currentPacketLength); stream.Write(filebuff, 0, filebuff.Length); // clientsocket.Send(filebuff); Console.WriteLine("Wysłano " + totalLength + "bajtów..."); } streamFile.Close(); } catch { Console.WriteLine("Nie ma takiego pliku..."); } } public List ListFiles() { List plikiPliki = new List(); byte[] recBuffer = new byte[1024]; //byte[] receivedFileName; while (true) { // clientsocket.Receive(recBuffer, recBuffer.Length, 0); stream.Read(recBuffer, 0, recBuffer.Length); DiscElement file = new DiscElement(); dynamic json = JsonConvert.DeserializeObject(ASCIIEncoding.ASCII.GetString(recBuffer)); if (json.Type == "End") { break; } DiscElement pliczek = new DiscElement() { Type = json.Type, Filename = json.Filename, Filepath = json.Filepath, Filesize = json.Filesize, Fileext = json.Fileext }; plikiPliki.Add(pliczek); } return plikiPliki; } //public void DownloadFile(string fileName) //{ // byte[] recBuffer = new byte[1024]; // byte[] fileNameBytes; // fileNameBytes = Encoding.UTF8.GetBytes(fileName); // // clientSocket.Send(fileNameBytes); // stream.Write(fileNameBytes, 0, fileNameBytes.Length); // byte[] fileSizeBytes = new byte[4]; // // clientSocket.Receive(fileSizeBytes, fileSizeBytes.Length, 0); // stream.Read(fileSizeBytes, 0, fileSizeBytes.Length); // int bytes = BitConverter.ToInt32(fileSizeBytes, 0); // Console.WriteLine("Plik ma długość: " + bytes); // long received = 0; // long receivedAll = 0; // byte[] fileData = new byte[1024]; // Console.WriteLine("Pobieram plik..."); // while (receivedAll < bytes) // { // // received = clientSocket.Receive(fileData, fileData.Length, 0);\ // received = stream.Read(fileData, 0, fileData.Length); // if (received < 0) // { // Console.WriteLine("Błąd przy pobieraniu pliku!"); // break; // } // BinaryWriter bWrite = new BinaryWriter(File.Open("C:\\Users\\Roman\\Desktop\\" + fileName, FileMode.Append)); //nazwa podana przez użytkownika!!!! zmienic // bWrite.Write(fileData); // bWrite.Close(); // receivedAll += received; // // Console.WriteLine(ASCIIEncoding.ASCII.GetString(fileData)); // } //} public void Disconnect() { stream.Close(); client.Close(); // clientsocket.Close(); } /* public void SendFile(string file, string destPath = "C:\\Users\\Lenovo\\Desktop\\") { FileStream streamFile = new FileStream(file, FileMode.Open, FileAccess.Read); while(true) { byte[] buff = new byte[1024]; int read = 0; while(read < buff.Length) { int bytesRead = streamFile.Read(buff, read, buff.Length - read); if(bytesRead == 0) { break; } if(bytesRead < 1024) { byte[] temp = new byte[bytesRead]; for (int i = 0; i < bytesRead; i++) temp[i] = buff[i]; buff = temp; } read += bytesRead; } if (read != 0) { sendData(stream, buff); } if (read != buff.Length) { sendData(stream, System.Text.Encoding.ASCII.GetBytes("".ToUpper())); return; } } }*/ } }