コード例 #1
0
        /// <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)
        {
            int fileSize = (int)LIB.getFileSizeTCP(io);

            //File.WriteAllText(fileName,LIB.readTextTCP(io);
            if (fileSize > 0)
            {
                Byte[] receivingBuffer = new byte[BUFSIZE];
                Console.WriteLine(fileSize);
                FileStream DataWeWant = new FileStream(fileName, FileMode.Create, FileAccess.Write);


                int totalNumberOfReceivedBytes = 0;

                // læs mens der stadig er ting tilbage
                while (totalNumberOfReceivedBytes < fileSize)
                {
                    int bytesRead = io.Read(receivingBuffer, 0, receivingBuffer.Length);

                    DataWeWant.Write(receivingBuffer, 0, bytesRead);
                    totalNumberOfReceivedBytes += bytesRead;

                    Array.Clear(receivingBuffer, 0, BUFSIZE);
                }

                DataWeWant.Close();
                io.Close();

                Console.WriteLine("File Received: {0} Bytes", fileSize);
            }
            else
            {
                Console.WriteLine("No file found");
            }
        }
コード例 #2
0
ファイル: file_client.cs プロジェクト: SWTTeam14/IknJournal1
        /// <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();

            clientSocket.Connect(args[0], PORT);

            Console.WriteLine("Client connected...");

            NetworkStream network = clientSocket.GetStream();

            LIB.writeTextTCP(network, args[1]);

            fileSize = LIB.getFileSizeTCP(network);
            Console.WriteLine("File size from server: {0}", fileSize, " bytes");

            if (fileSize == 0)
            {
                throw new Exception("File not found..!");
            }
            else
            {
                receiveFile(args[1], network);
                Console.WriteLine("Received file(s)...");
            }
        }
コード例 #3
0
        /// <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)
        {
            try
            {
                TcpClient     client = new TcpClient(args[0], PORT);
                NetworkStream stream = client.GetStream();
                Console.WriteLine("Client stream connected");

                LIB.writeTextTCP(stream, args[1]);
                Console.WriteLine("Write " + args[1] + " to server");

                long fileSize = LIB.getFileSizeTCP(stream);
                Console.WriteLine("Get filesize: " + fileSize);

                string fileName = LIB.extractFileName(args[1]); //Hvis en sti indtastes findes filnavnet fra denne.

                if (fileSize > 0)                               //Check if file exists on server, and recieve.
                {
                    receiveFile(AppDomain.CurrentDomain.BaseDirectory + "/" + fileName, stream);
                }
                else
                {
                    Console.WriteLine("File does not exist on server");
                }

                client.GetStream().Close();
                Console.WriteLine("Close stream");
                client.Close();
                Console.WriteLine("Close client");
            }
            catch (System.Net.Sockets.SocketException)
            {
                Console.WriteLine($"Fejl: Kunne ikke oprette forbindelse til {args[0]}, indtast en valid IP adresse.");
            }
        }
コード例 #4
0
        /// <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();
        }
コード例 #5
0
        private void ReceiveFile(string fileName, NetworkStream io)
        {
            var fileSize = (int)LIB.getFileSizeTCP(io);

            if (fileSize != 0)
            {
                var totalAmountReceived = 0;

                var bytes = new byte[BUFSIZE];
                var fs    = new FileStream(fileName, FileMode.Create, FileAccess.Write);

                while (totalAmountReceived < fileSize)
                {
                    // Kunne man ikke også bare have brugt BUFSIZE her?
                    var bytesRead = io.Read(bytes, 0, bytes.Length);

                    fs.Write(bytes, 0, bytesRead);
                    totalAmountReceived += bytesRead;

                    Array.Clear(bytes, 0, BUFSIZE);
                }

                fs.Close();
                //io.Close();   // Det behøver man vel ikke?
            }
            else
            {
                Console.WriteLine("No file found!");
            }
        }
コード例 #6
0
        /// <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)
        {
            // Adding null terminator to fileName
            LIB.writeTextTCP(io, fileName + "\0");
            long fileSize = LIB.getFileSizeTCP(io);

            // If getting filesize fails, return
            if (fileSize == -1)
            {
                Console.WriteLine("File not found!");
                return;
            }

            byte[] file     = new byte[fileSize];
            int    received = 0;

            // Loop till all files received
            while (received < fileSize)
            {
                if (fileSize - received < BUFSIZE)
                {
                    int lastRead = io.Read(file, received, (int)fileSize - received);
                    received = received + lastRead;
                }
                else
                {
                    int read = io.Read(file, received, BUFSIZE);
                    received = received + read;
                }
            }

            File.WriteAllBytes(LIB.extractFileName(fileName), file);
            Console.WriteLine(LIB.extractFileName(fileName) + " received!");
        }
コード例 #7
0
        /// <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)
        {
            TcpClient client = new TcpClient();

            //Sikrer connection til det angivne Port
            client.Connect(args[0], PORT);
            Console.WriteLine($"'{args[0]}' Has been connected <---");

            //Requester client streamet
            NetworkStream server = client.GetStream();

            Console.WriteLine($"'{args[1]}'Filename requested <---");

            string File_Name = args[1];

            LIB.writeTextTCP(server, File_Name);

            long File_Size = LIB.getFileSizeTCP(server);

            //Sædvanlige check om filen eksistere eller ej
            while (File_Size == 0)
            {
                Console.WriteLine("File not found!");
                File_Name = Console.ReadLine();

                Console.WriteLine($"Requesting this file ---> '{File_Name}'");

                LIB.writeTextTCP(server, File_Name);
                File_Size = LIB.getFileSizeTCP(server);
            }

            Console.WriteLine("Size of file ---> " + File_Size);

            receiveFile(File_Name, server, File_Size);
        }
コード例 #8
0
        /// <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)
        {
            try
            {
                string ipa = args [0];

                IPAddress ipAddress = IPAddress.Parse(ipa);

                TcpClient clientSocket = new TcpClient();

                clientSocket.Connect(ipAddress, PORT);



                NetworkStream serverStream = clientSocket.GetStream();

                // Send filename to server
                string filename = args [1];
                LIB.writeTextTCP(serverStream, filename);

                // Extract filename
                string extractedFilename = LIB.extractFileName(filename);

                // Recieve notice if filename exists
                long currentfilesize = LIB.getFileSizeTCP(serverStream);
                //Console.WriteLine (currentfilesize);

                // If filename exists recieve file
                if (currentfilesize != 0)
                {
                    receiveFile(extractedFilename, serverStream, currentfilesize);
                }
                else
                {
                    Console.WriteLine("Filename: " + extractedFilename + " does not exist");
                }
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("ArgumentException: " + e.Message);
            }
            catch (SocketException se)
            {
                Console.WriteLine("SocketException: " + se.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
コード例 #9
0
ファイル: file_client.cs プロジェクト: oskarthansen/OPG6-IKN
        private file_client(string[] args)
        {
            // Setting Path and IP
            SetPathAndIP(args);

            try
            {
                TcpClient client = new TcpClient(ServerIP, PORT);


                // Get a client stream for reading and writing.
                //  Stream stream = client.GetStream();
                NetworkStream stream = client.GetStream();
                Console.WriteLine($"Requesting server for file: {FileFromServer}");



                LIB.writeTextTCP(stream, FilePathServer);

                fileSize = LIB.getFileSizeTCP(stream);


                if (fileSize > 0)
                {
                    Console.WriteLine($"File exists and has size {fileSize}");
                    Console.WriteLine("Saving file...");
                    receiveFile(FileSavePath, stream);
                }

                else
                {
                    Console.WriteLine("File does not exist.");
                }


                client.Close();
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("ArgumentNullException: {0}", e);
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: {0}", e);
            }


            // TO DO Your own code
        }
コード例 #10
0
ファイル: file_client.cs プロジェクト: Haugaarden/IKN12
        /// <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)
        {
            //If file does not exist on the server, the user can input another filename. Won't continue untill a correct name is input
            long fileSize = 0;

            do
            {
                LIB.writeTextTCP(io, fileName);                 //Sends filename to server
                fileSize = LIB.getFileSizeTCP(io);              //Reads filesize from server

                //If file does not exist, get new filename
                if (fileSize == 0)
                {
                    Console.WriteLine("File does not exist, write filename:");
                    fileName = Console.ReadLine();
                }
            } while(fileSize == 0);


            Console.WriteLine("Filesize: " + fileSize);

            string dataDir = "/root/ExFiles/";                                                             //filepath to save file to

            Directory.CreateDirectory(dataDir);                                                            //Create directory if it does not exist
            FileStream fileStream = new FileStream(dataDir + fileName, FileMode.Create, FileAccess.Write); //Creates file and return filestream
            var        fileBuffer = new byte [BUFSIZE];                                                    //Buffer for holding filedata packets

            Console.WriteLine("Reading...");

            int bytesRead      = 0;
            int totalBytesRead = 0;

            while (totalBytesRead < fileSize)
            {
                bytesRead = io.Read(fileBuffer, 0, BUFSIZE);
                fileStream.Write(fileBuffer, 0, bytesRead);
                totalBytesRead += bytesRead;
            }

            Console.WriteLine("Done reading");
            fileStream.Close();
        }
コード例 #11
0
ファイル: file_client.cs プロジェクト: EmmaRolsted/IKN
        /// <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)
        {
            string filePath = "";
            long   fileSize = 0;

            TcpClient ClientSocket = new TcpClient();

            try
            {
                ClientSocket.Connect(args[0], PORT);
                Console.WriteLine("Connected to {0}, port {1}", args[0], PORT);
            }
            catch
            {
                Console.WriteLine("No connection");
            }
            NetworkStream serverStream = ClientSocket.GetStream();

            //Saving requested file in variable filePath
            filePath = args[1];
            Console.WriteLine("Requesting file...");
            LIB.writeTextTCP(serverStream, filePath);

            fileSize = LIB.getFileSizeTCP(serverStream);

            //If file does not exist, keep asking for existing file
            while (fileSize == 0)
            {
                Console.WriteLine("!File not found!");
                Console.WriteLine("Enter new file: ");
                filePath = Console.ReadLine();

                Console.WriteLine("Requesting file...");
                LIB.writeTextTCP(serverStream, filePath);
                fileSize = LIB.getFileSizeTCP(serverStream);
            }
            Console.WriteLine("Receiving file...");
            receiveFile(filePath, serverStream);
        }
コード例 #12
0
ファイル: file_client.cs プロジェクト: FatimaAU/IKN
        /// <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)
        {
            TcpClient clientSocket = new TcpClient();

            if (args.Length != 2)
            {
                Console.WriteLine("Please supply two arguments: IP-address of the server and path + filename");
                Environment.Exit(0);
            }

            clientSocket.Connect(args[0], PORT);
            Console.WriteLine($"Connected to '{args[0]}'");

            NetworkStream serverStream = clientSocket.GetStream();

            Console.WriteLine($"Requesting filename '{args[1]}'");

            string filename = args [1];

            LIB.writeTextTCP(serverStream, filename);

            long fileSize = LIB.getFileSizeTCP(serverStream);

            while (fileSize == 0)
            {
                Console.WriteLine("File not found. Input a valid file");
                filename = Console.ReadLine();

                Console.WriteLine($"Requesting filename '{filename}'");

                LIB.writeTextTCP(serverStream, filename);
                fileSize = LIB.getFileSizeTCP(serverStream);
            }

            Console.WriteLine("File size: " + fileSize);

            receiveFile(filename, serverStream, fileSize);
        }
コード例 #13
0
        /// <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)
        {
            long size;

            Console.WriteLine("Client started");
            TcpClient clientSocket = new TcpClient();

            clientSocket.Connect(args[0], PORT);
            NetworkStream serverStream = clientSocket.GetStream();

            LIB.writeTextTCP(serverStream, args[1]);              //Filename skal gives med som argument.
            size = LIB.getFileSizeTCP(serverStream);
            if (size != 0)
            {
                receiveFile(args [1], serverStream, size);
            }
            else
            {
                Console.WriteLine("No such file exist on the server");
            }

            clientSocket.Close();
        }
コード例 #14
0
        /// <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)
        {
            // TO DO Your own code
            string     filename = LIB.extractFileName(fileName);
            long       fileSize = LIB.getFileSizeTCP(io);
            FileStream fs       = new FileStream("/root/Downloads/" + filename, FileMode.Create, FileAccess.Write);

            byte[] recieveBytes = new byte[fileSize];
            int    recieved     = 0;

            if (fileSize == 0)
            {
                Console.WriteLine("File do not exist on server");
                return;
            }
            else
            {
                Console.WriteLine("File size: " + fileSize);
                while (recieved < fileSize)
                {
                    if ((fileSize - recieved) > BUFSIZE)
                    {
                        io.Read(recieveBytes, 0, BUFSIZE);
                        fs.Write(recieveBytes, 0, BUFSIZE);
                        recieved += BUFSIZE;
                    }
                    else
                    {
                        int read = io.Read(recieveBytes, 0, ((int)fileSize - recieved));
                        fs.Write(recieveBytes, 0, read);
                        recieved += read;
                    }
                    Console.WriteLine("Bytes recieved: " + recieved);
                }
            }
        }