Question

I'm working on a little console "Send file to Server" program.

The client (tcpClient) reads a file from his folder and sends it to the server (tcpServer), and the server receives it and writes this file into his folder.

I still have 2 problems:

  • If the client sends a ~19MB .txt file (which is full of random chars but no blanks!) to the server, the server receives the file, writes a ~19MB .txt file in his folder, but the .txt file in the server folder contains only in the first few lines of the original file, and the large rest contains only blanks. The .txt file is on the server side not complete...

  • If the client sends a 9KB .png file, the server receives it, writes it to his folder. But if I try to open this .png file from the server folder, then I get an error message which says, that this file is too large or maybe damaged... (the .png file in the server folder has the right .png extension and the typical icon).

Server:

public void recieveFile() {
    NetworkStream nws = clientconnection.GetStream();
    StreamReader sr = new StreamReader(nws);
    StreamWriter sw = new StreamWriter(nws);

    // Recieve filename
    string filename = sr.ReadLine().Remove(0, 10);

    // Recieve filesize
    long filesize = Convert.ToInt64(sr.ReadLine().Remove(0, 10));

    Byte[] fileBytes = new Byte[filesize];
    nws.Read(fileBytes, 0, fileBytes.Length);
    nws.Close();

    var a = File.OpenWrite(filename);
    a.Write(fileBytes, 0, fileBytes.Length);
    a.Flush();
    a.Close();
}

Client:

public void sendFileToServer(String filename) {
    var a = File.OpenRead(filename);

    FileInfo fo = new FileInfo(filename);
    long filesize = fo.Length;

    // Write file into fileBytes-Array
    Byte[] fileBytes = new Byte[filesize];
    a.Read(fileBytes, 0, fileBytes.Length);
    a.Close();

    // Send filename to server
    sw.WriteLine("Filename: " + filename);
    sw.Flush();

    // Send filesize to server
    sw.WriteLine("Filesize: " + filesize);
    sw.Flush();

    // Send fileBytes to server
    NetworkStream nws = clientConnection.GetStream();
    nws.Write(fileBytes, 0, fileBytes.Length);
    nws.Flush();
    nws.Close();

    sw.Close();
    sr.Close();
    clientConnection.Close();
}
Était-ce utile?

La solution

My startpost-version is full of several mistakes. Here the now 100% working version:

Client:

                    public void sendFileToServer(String filename) {

                        var a = File.OpenRead(filename);

                        FileInfo fo = new FileInfo(filename);
                        long filesize = fo.Length;

                        // Send filename to server
                        sw.WriteLine("Filename: " + filename);
                        sw.Flush();

                        // Send filesize to server
                        sw.WriteLine("Filesize: " + filesize);
                        sw.Flush();

                        // Write file into fileBytes-Array and sends it in parts
                        Byte[] fileBytes = new Byte[1024];
                        long count = filesize;
                        while (count > 0) {
                            int recieved = a.Read(fileBytes, 0, fileBytes.Length);
                            a.Flush();
                            nws.Write(fileBytes, 0, recieved);
                            nws.Flush();
                            count -= recieved;
                        }
                        a.Close();
                        nws.Close();

                        sw.Close();
                        sr.Close();
                        clientConnection.Close();

                    }

Server:

            public void recieveFile() {

                        NetworkStream nws = clientconnection.GetStream();
                        StreamReader sr = new StreamReader(nws);
                        StreamWriter sw = new StreamWriter(nws);

                        // Recieve filename
                        string filename = sr.ReadLine().Remove(0, 10);

                        // Recieve filesize
                        long filesize = Convert.ToInt64(sr.ReadLine().Remove(0, 10));

                        long count = filesize;
                        Byte[] fileBytes = new Byte[1024];
                        var a = File.OpenWrite(filename);
                        while (count > 0) {
                            int recieved = nws.Read(fileBytes, 0, fileBytes.Length);
                            nws.Flush();
                            a.Write(fileBytes, 0, recieved);
                            a.Flush();
                            count -= recieved;
                        }
                        nws.Close();
                        a.Close();

                        sr.Close();
                        sr.Close();
                        clientconnection.Close();

                    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top