/// <summary> /// Initializes a new instance of the <see cref="file_client"/> class. /// </summary> /// <param name='args'> /// The command-line arguments. First ip-adress of the server. Second the filename /// </param> private file_client(string[] args) { // TO DO Your own code TcpClient clientSocket = new TcpClient(); Console.WriteLine(" >> Client connecting"); clientSocket.Connect(args[0], PORT); NetworkStream serverStream = clientSocket.GetStream(); LIB.writeTextTCP(serverStream, args[1]); long size = LIB.getFileSizeTCP(serverStream); if (size != 0) { string fileName = LIB.extractFileName(args[1]); receiveFile(fileName, serverStream, size); } else { Console.WriteLine(LIB.readTextTCP(serverStream)); } serverStream.Flush(); clientSocket.Close(); }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { IPAddress IP = IPAddress.Parse("10.0.0.1"); var Socket = new TcpListener(IP, PORT); TcpClient ClientSocket = default(TcpClient); Socket.Start(); Console.WriteLine("SERVER STARTED"); while (true) { Console.WriteLine("Waiting for Client"); ClientSocket = Socket.AcceptTcpClient(); Console.WriteLine("Client accepted"); NetworkStream networkStream = ClientSocket.GetStream(); string filepath = LIB.readTextTCP(networkStream); // modtager fil-sti long filesize = LIB.check_File_Exists(filepath); // checker fil if (filesize != 0) { Console.WriteLine("Sending file"); LIB.writeTextTCP(networkStream, filesize.ToString()); // sender filesize sendFile(filepath, filesize, networkStream); // sender fil Console.WriteLine("File sent!"); } else { LIB.writeTextTCP(networkStream, "Kan ikke finde fil"); } ClientSocket.Close(); } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { TcpListener serverSocket = null; TcpClient clientSocket = null; try{ //SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1); //TcpListener serverSocket = new TcpListener(PORT); serverSocket = new TcpListener(IPAddress.Any, PORT); clientSocket = default(TcpClient); serverSocket.Start(); while (true) { Console.WriteLine(" >> Server Started listening for request"); clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine($" Forbindelse til {clientSocket.ToString()}"); NetworkStream networkStream = clientSocket.GetStream(); //byte[] bytesFrom = new byte[1000]; Console.WriteLine((int)clientSocket.ReceiveBufferSize); string dataFromClient = LIB.readTextTCP(networkStream); dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$")); long length = LIB.check_File_Exists(dataFromClient); Console.WriteLine(" >> Data from client - " + dataFromClient); string serverResponse = length.ToString(); if (length < 1) { serverResponse = "Filen kunne ikke lokaliseres"; } LIB.writeTextTCP(networkStream, serverResponse); Console.WriteLine(" >> " + serverResponse); if (length > 0) { sendFile(dataFromClient, length, networkStream); } } } catch (SocketException e) { Console.WriteLine("SocketException: {0}", e); } finally { // Stop listening for new clients. clientSocket.Close(); } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { String clientData = null; IPAddress localAddress = IPAddress.Any; TcpListener serverSocket = new TcpListener(localAddress, PORT); serverSocket.Start(); while (true) { try { Console.WriteLine("Waiting for connection..."); TcpClient client = serverSocket.AcceptTcpClient(); Console.WriteLine("Client connected!"); NetworkStream stream = client.GetStream(); Console.WriteLine("Stream created."); clientData = LIB.readTextTCP(stream); Console.WriteLine("Read data from client."); String requestedFile = LIB.extractFileName(clientData); Console.WriteLine("Extracted " + requestedFile + "from client."); //til filer på vilkårlige placeringer long FileLength = LIB.check_File_Exists(clientData); if (FileLength > 0) //tjekker om filen findes på den givne sti { Console.WriteLine($"Fuld sti:{clientData}" + $"\nstørrelse:{FileLength}"); LIB.writeTextTCP(stream, FileLength.ToString()); //sender størrelsen på filen til client SendFile(clientData, stream); Console.WriteLine("File sent."); } else { LIB.writeTextTCP(stream, 0.ToString()); //hvis filen ikke findes skrives der blot 0 til clienten } client.GetStream().Close(); Console.WriteLine("Close stream"); client.Close(); Console.WriteLine("Close client"); } catch (Exception e) { Console.WriteLine(e); throw; } } }
private file_server() { TcpListener server = null; try { server = new TcpListener(PORT); server.Start(); TcpClient client = null; byte[] from = new byte[BUFSIZE]; string fileReq = null; while (true) { Console.WriteLine("Waiting for connection"); client = server.AcceptTcpClient(); Console.WriteLine("Someone connected"); //resetting string for new request. fileReq = null; NetworkStream nStream = client.GetStream(); //File request from client fileReq = LIB.readTextTCP(nStream); Console.WriteLine("got: {0}", fileReq); /*Getting filename and then * Checking if file exist on system, els returns 0.*/ string file = LIB.extractFileName(fileReq); long file_len = LIB.check_File_Exists(file); sendLength(file_len, nStream); if (file_len > 0) { //Delay to ensure nStream is ready. System.Threading.Thread.Sleep(10); sendFile(file, file_len, nStream); } client.Close(); } } catch (Exception e) { Console.WriteLine("Socket expection: {0}", e); } finally { server.Stop(); } }
/// <summary> /// Receives the file. /// </summary> /// <param name='fileName'> /// File name. /// </param> /// <param name='io'> /// Network stream for reading from the server /// </param> private void receiveFile(String fileName, NetworkStream io) { LIB.writeTextTCP(io, fileName + "$"); string lenghtString = LIB.readTextTCP(io); long fileSize; try { fileSize = long.Parse(lenghtString); } catch (System.FormatException) { Console.WriteLine(lenghtString); return; } if (fileSize < 1) { Console.WriteLine("Vi er stødt på en fejl. længden er rapporteret som 0 :("); return; } Console.WriteLine("length= " + lenghtString); if (File.Exists(fileName)) { File.Delete(fileName); } //https://stackoverflow.com/a/2398471 using (var fileStream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.None)) using (var bw = new BinaryWriter(fileStream)) { int i; for (i = 0; i < fileSize - 1000; i += 1000) { byte[] bytearray = new byte[1000]; io.Read(bytearray, 0, 1000); Console.WriteLine(i.ToString()); bw.Write(bytearray); Console.WriteLine($"Wrote: {bytearray[900]}\n"); } byte[] lastChunk = new byte[1000]; io.Read(lastChunk, 0, 1000); Console.WriteLine(i.ToString()); bw.Write(lastChunk, 0, (int)fileSize % 1000); Console.WriteLine($"Wrote: {lastChunk[900]}\n"); } }
/// <summary> /// The entry point of the program, where the program control starts and ends. /// </summary> /// <param name='args'> /// The command-line arguments. /// </param> public static void Main(string[] args) { Console.WriteLine("Server starts..."); new file_server(); TcpListener serverSocket = new TcpListener(PORT); serverSocket.Start(); Console.WriteLine("Server socket started"); while (true) { try { TcpClient clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine("Client acceped"); NetworkStream networkStream = clientSocket.GetStream(); Console.WriteLine("Got stream"); string path = LIB.readTextTCP(networkStream); Console.WriteLine("Path received:" + path); string fileName = LIB.extractFileName(path); long fileLength = LIB.check_File_Exists(fileName); Console.WriteLine(fileName); if (fileLength == 0) //File not found { Console.WriteLine("File length: " + fileLength.ToString()); LIB.writeTextTCP(networkStream, fileLength.ToString()); clientSocket.Close(); } else // File found { Console.WriteLine("File length sent: " + fileLength.ToString()); LIB.writeTextTCP(networkStream, fileLength.ToString()); Thread.Sleep(1); sendFile(path, fileLength, networkStream); clientSocket.Close(); } } catch (Exception e) { Console.WriteLine(e.ToString()); } finally { } } serverSocket.Stop(); Console.WriteLine(" >> exit"); Console.ReadLine(); }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { // Create the local endpoint for the socket and the TCP/IP socket IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, PORT); Socket server = new Socket(SocketType.Stream, ProtocolType.Tcp); // Bind the socket to the local endpoint try { server.Bind(endPoint); // Listen for connections server.Listen(10); while (true) { Console.WriteLine("Listening..."); Socket handler = server.Accept(); // Create networkstream and connect it to socket NetworkStream networkStream = new NetworkStream(handler); string fileNameAndPath = LIB.readTextTCP(networkStream); Console.WriteLine("Requested file: " + fileNameAndPath); long fileSize = LIB.check_File_Exists(fileNameAndPath); // If file doesnt exist, close the networkstream, shutdown the socket and close it if (fileSize < 1) { Console.WriteLine("File not found!"); LIB.writeTextTCP(networkStream, "-1"); networkStream.Close(); handler.Shutdown(SocketShutdown.Both); handler.Close(); } else { sendFile(fileNameAndPath, fileSize, networkStream); networkStream.Close(); handler.Shutdown(SocketShutdown.Both); handler.Close(); } } } catch (Exception e) { Console.WriteLine(e.ToString()); throw; } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { TcpListener serverSocket = new TcpListener(new IPAddress(ipAddress), PORT); try { serverSocket.Start(); while (true) { TcpClient newClient = serverSocket.AcceptTcpClient(); Console.WriteLine("Client Accepted..."); // Recieve filename from client NetworkStream networkStream = newClient.GetStream(); String filename = LIB.readTextTCP(networkStream); // Check to see if filename exists and notify client long filesize = LIB.check_File_Exists(filename); //Console.WriteLine (filesize); string filesizeascii = filesize.ToString(); LIB.writeTextTCP(networkStream, filesizeascii); if (filesize != 0) { sendFile(filename, filesize, networkStream); } newClient.Close(); } } catch (ArgumentException e) { Console.WriteLine("ArgumentException: " + e.Message); } catch (SocketException se) { Console.WriteLine("SocketException: " + se.Message); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } finally { serverSocket.Stop(); } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { // TO DO Your own code TcpListener serverSocket = new TcpListener(localAddr, PORT); serverSocket.Start(); while (true) { try { Console.WriteLine("Waiting for a connection..."); // Accept requests TcpClient client = serverSocket.AcceptTcpClient(); Console.WriteLine("Connected to client!"); NetworkStream stream = client.GetStream(); string fileName = LIB.readTextTCP(stream); Console.WriteLine($"Client asks for file: {fileName}"); Console.WriteLine("Cheking if file exists..."); long fileSize = LIB.check_File_Exists(fileName); if (fileSize > 0) { Console.WriteLine($"File exists an has size: {fileSize}"); LIB.writeTextTCP(stream, fileSize.ToString()); sendFile(fileName, fileSize, stream); } else { Console.WriteLine("File does not exist"); LIB.writeTextTCP(stream, fileSize.ToString()); } client.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// </summary> private file_server() { // Starts server and accepts a client Console.WriteLine("Starting server - waiting for client --->"); //Opretter en socket TcpListener Server_Socket = new TcpListener(IPAddress.Any, PORT); TcpClient Client_Socket = default(TcpClient); //Starter serveren og connectecter til client vha Accept funktionen Server_Socket.Start(); Client_Socket = Server_Socket.AcceptTcpClient(); Console.WriteLine("Connected to client!"); // getter client stream NetworkStream client = Client_Socket.GetStream(); //Istantiering af filename til at læse og checke om file size eksistere string File_Name = LIB.readTextTCP(client); long File_Size = LIB.check_File_Exists(File_Name); // loop - der tjekker om filen er fundet. while (File_Size == 0) { string msg = "File '" + File_Name + "' not found"; Console.WriteLine(msg); LIB.writeTextTCP(client, File_Size.ToString()); File_Name = LIB.readTextTCP(client); File_Size = LIB.check_File_Exists(File_Name); } //Udskriver filnavn og størrelsen som er initialiseret Console.WriteLine("Filename: " + File_Name); Console.WriteLine("Size: " + File_Size); //Skriver texten til client stream LIB.writeTextTCP(client, File_Size.ToString()); sendFile(File_Name, client); //Lukker programmet Client_Socket.Close(); Server_Socket.Stop(); }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { TcpListener serverSocket = new TcpListener(IPAddress.Any, PORT); TcpClient clientSocket = default(TcpClient); serverSocket.Start(); byte[] bytes = new byte[BUFSIZE]; string data; while (true) { clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine("Accept conn from client"); NetworkStream network = clientSocket.GetStream(); try { data = LIB.readTextTCP(network); Console.WriteLine("{0}", data); long fileLength = LIB.check_File_Exists(data); string fileName = LIB.extractFileName(data); Console.WriteLine("File name: {0}", fileName); Console.WriteLine("File length: {0}", fileLength); LIB.writeTextTCP(network, fileLength.ToString()); sendFile(data, fileLength, network); Console.WriteLine("Closing connection to client..."); Console.WriteLine("Returning to idle."); clientSocket.GetStream().Close(); clientSocket.Close(); } catch (FileNotFoundException ex) { Console.WriteLine("File not found..!"); throw ex; } } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { // TO DO Your own code IPAddress ipAddress = IPAddress.Parse("10.0.0.1"); TcpListener serverSocket = new TcpListener(ipAddress, PORT); TcpClient clientSocket = default(TcpClient); serverSocket.Start(); Console.WriteLine(" >> Server Started"); while (true) { try { clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine(" >> Accept connection from client"); NetworkStream networkStream = clientSocket.GetStream(); Console.WriteLine(" >> Filename requested from client!"); string fileName = LIB.readTextTCP(networkStream); Console.WriteLine($" >> Filename from client: {fileName}"); Console.WriteLine(" >> Filesize requested from client!"); long fileSize = LIB.check_File_Exists(fileName); Console.WriteLine($" >> Filesize from server: {fileSize}"); LIB.writeTextTCP(networkStream, fileSize.ToString()); if (fileSize != 0) { Console.WriteLine(" >> Sending file..."); sendFile(fileName, fileSize, networkStream); Console.WriteLine(" >> File sent!"); } else { LIB.writeTextTCP(networkStream, "File not found"); Console.WriteLine(" >> Oops, file not found"); } networkStream.Flush(); clientSocket.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { //initialiser variabler string filePath = string.Empty; long fileSizeLong = 0; string fileSizeStr = string.Empty; TcpListener ServerSocket = new TcpListener(PORT); TcpClient ClientSocket = default(TcpClient); ServerSocket.Start(); Console.WriteLine("Server started"); ClientSocket = ServerSocket.AcceptTcpClient(); Console.WriteLine("Accepted connection"); NetworkStream networkStream = ClientSocket.GetStream(); filePath = LIB.readTextTCP(networkStream); Console.WriteLine("The client wants " + filePath); //Gets file size, and checks whether it exists fileSizeLong = LIB.check_File_Exists(filePath); fileSizeStr = fileSizeLong.ToString(); //converts it to string LIB.writeTextTCP(networkStream, fileSizeStr); // sending the size of the file to client while (fileSizeLong == 0) { Console.WriteLine("Error, file does not exist"); filePath = LIB.readTextTCP(networkStream); Console.WriteLine(filePath); //Gets file size, and checks whether it exists fileSizeLong = LIB.check_File_Exists(filePath); fileSizeStr = fileSizeLong.ToString(); LIB.writeTextTCP(networkStream, fileSizeStr); } Console.WriteLine("The size of the file is {0} bytes", fileSizeLong); sendFile(filePath, fileSizeLong, networkStream); //kalder sendFile funktionen ClientSocket.Close(); ServerSocket.Stop(); }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { // TO DO Your own code TcpListener serverSocket = new TcpListener(IPAddress.Any, PORT); serverSocket.Start(); Console.WriteLine("Server started"); while (true) { try { TcpClient clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine("Accept connection from client"); NetworkStream networkStream = clientSocket.GetStream(); string fileName = LIB.readTextTCP(networkStream); long fileSize = LIB.check_File_Exists(fileName); if (fileSize == 0) { Console.WriteLine("File does not exist"); } else { LIB.writeTextTCP(networkStream, fileSize.ToString()); sendFile(fileName, fileSize, networkStream); Console.WriteLine("File send"); } clientSocket.Close(); networkStream.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { TcpListener serverSocket = new TcpListener(PORT); TcpClient clientSocket = default(TcpClient); serverSocket.Start(); while (running) { requestCount++; string returnText = LIB.readTextTCP(clientSocket.GetStream()); long fileSize = LIB.check_File_Exists(returnText); if (fileSize != 0) { LIB.writeTextTCP(clientSocket.GetStream(), fileSize.ToString()); } } serverSocket.Stop(); clientSocket.Close(); }
/// <summary> /// Initializes a new instance of the <see cref="file_client"/> class. /// </summary> /// <param name='args'> /// The command-line arguments. First ip-adress of the server. Second the filename /// </param> private file_client(string[] args) { ClientSocket.Connect(args[0], PORT); NetworkStream io = ClientSocket.GetStream(); LIB.writeTextTCP(io, args[1]); // Sender fil-sti filesize = LIB.readTextTCP(io); // Modtager filesize if (filesize == "Kan ikke finde fil") { Console.WriteLine("File not Found "); } else { Console.WriteLine("Receiving file"); receiveFile(LIB.extractFileName(args[1]), io); Console.WriteLine("File received"); Console.WriteLine($"Filessize is: {filesize}"); } }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { TcpListener serverSocket = new TcpListener(new IPEndPoint(IPAddress.Any, PORT)); TcpClient clientSocket = default(TcpClient); serverSocket.Start(); Console.WriteLine(" >> Server Started"); while (true) { try { clientSocket = serverSocket.AcceptTcpClient(); NetworkStream networkStream = clientSocket.GetStream(); string dataFromClient = LIB.readTextTCP(networkStream); Console.WriteLine(" >> Data from client - " + dataFromClient); if (LIB.check_File_Exists(dataFromClient) != 0) { FileInfo fileInfo = new FileInfo(dataFromClient); long fileSize = fileInfo.Length; LIB.writeTextTCP(networkStream, fileSize.ToString()); sendFile(dataFromClient, fileSize, networkStream); } else { LIB.writeTextTCP(networkStream, "" + 0); } networkStream.Flush(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } clientSocket.Close(); serverSocket.Stop(); }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet /// </summary> private file_server() { // Starts server and accepts a client Console.WriteLine("Listening for client .."); TcpListener serverSocket = new TcpListener(IPAddress.Any, PORT); TcpClient clientSocket = default(TcpClient); serverSocket.Start(); clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine("Accepted connection from client"); // Get the network file stream NetworkStream clientStream = clientSocket.GetStream(); string filename = LIB.readTextTCP(clientStream); long fileSize = LIB.check_File_Exists(filename); while (fileSize == 0) { string errorMsg = "File '" + filename + "' not found"; Console.WriteLine(errorMsg); LIB.writeTextTCP(clientStream, fileSize.ToString()); filename = LIB.readTextTCP(clientStream); fileSize = LIB.check_File_Exists(filename); } Console.WriteLine("Filename: " + filename); Console.WriteLine("Size: " + fileSize); LIB.writeTextTCP(clientStream, fileSize.ToString()); sendFile(filename, clientStream); clientSocket.Close(); serverSocket.Stop(); }
private file_server() { Socket ourSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); ourSocket.Bind(new IPEndPoint(IPAddress.Any, PORT)); while (true) { ourSocket.Listen(0); Socket acceptSocket = ourSocket.Accept(); Console.WriteLine("Someone connected!"); NetworkStream myNetworkStream = new NetworkStream(acceptSocket); string ReceivedFilePath = LIB.readTextTCP(myNetworkStream); long fileSize = LIB.check_File_Exists(ReceivedFilePath); Console.WriteLine($"Received file path: {ReceivedFilePath}"); Console.WriteLine($"Filesize: {fileSize} bytes"); if (fileSize == 0) { Console.WriteLine("Could not find file."); LIB.writeTextTCP(myNetworkStream, "Error: Could not find file."); } else { sendFile(ReceivedFilePath, fileSize, myNetworkStream); } acceptSocket.Close(); } //ourSocket.Close(); }
/// <summary> /// Initializes a new instance of the <see cref="file_server"/> class. /// Opretter en socket. /// Venter på en connect fra en klient. /// Modtager filnavn /// Finder filstørrelsen /// Kalder metoden sendFile /// Lukker socketen og programmet... IKKE /// </summary> private file_server() { String dataDir = "/root/ServerData/"; var serverSocket = new TcpListener(IPAddress.Any, PORT); //Listens on PORT var clientSocket = default(TcpClient); serverSocket.Start(); Console.WriteLine("Server started"); clientSocket = serverSocket.AcceptTcpClient(); Console.WriteLine("Accept connection from client"); var networkStream = clientSocket.GetStream(); //Open a stream String fileName; long fileSize; do { Console.WriteLine("Reading filename"); fileName = LIB.readTextTCP(networkStream); Console.WriteLine("Filename: " + fileName); fileSize = LIB.check_File_Exists(dataDir + fileName); Console.WriteLine("Filepath: " + dataDir + fileName); Console.WriteLine("Filesize: " + fileSize); LIB.writeTextTCP(networkStream, fileSize.ToString("0")); } while (fileSize == 0); sendFile(dataDir + fileName, fileSize, networkStream); //Cleaning clientSocket.Close(); serverSocket.Stop(); Console.WriteLine("Exit"); }
private file_server() { var localAddress = IPAddress.Parse("0.0.0.0"); Console.WriteLine("CHOOSE IP ADDRESS FOR THE SERVER..."); Console.WriteLine("1. Default Local Host IP (127.0.0.1)"); Console.WriteLine("2. Default Remote Host IP (10.0.0.1)"); Console.WriteLine("3. Enter Custom IP Address.\n"); Console.Write("Your choice: "); var choice = int.Parse(Console.ReadLine()); switch (choice) { case 1: localAddress = IPAddress.Parse("127.0.0.1"); Console.WriteLine("Server IP address is now: 127.0.0.1\n"); break; case 2: localAddress = IPAddress.Parse("10.0.0.1"); Console.WriteLine("Server IP address is now: 10.0.0.1\n"); break; case 3: Console.Write("Type custom IP address here: "); while (!IPAddress.TryParse(Console.ReadLine(), out localAddress)) { Console.WriteLine("Invalid IP address, please try again.\n"); Console.Write("Type custom IP address here: "); } Console.WriteLine("Server IP Address is now: " + localAddress); break; } try { var server = new TcpListener(localAddress, PORT); while (true) { server.Start(); Console.WriteLine("Server: Started!"); Console.WriteLine("Server: Waiting for new connection..."); var client = server.AcceptTcpClient(); Console.WriteLine("Server: Client connected!"); // Modtager besked fra Client: var stream = client.GetStream(); // Reads data from the client and prints it to console: var fileName = LIB.readTextTCP(stream); Console.WriteLine($"Stream received: {fileName}"); // Checks to see if file exists and returns its size: var fileSize = LIB.check_File_Exists(fileName); LIB.writeTextTCP(stream, fileSize.ToString()); if (fileSize != 0) { SendFile(fileName, fileSize, stream); } else { Console.WriteLine("File doesn't exist!"); } client.Close(); server.Stop(); } } catch (SocketException e) { Console.WriteLine($"Socket exception: {e}"); Console.ReadKey(); // Does server.Stop() run? Or does this potentially create memory leak??? } }