.txt-通过网络(客户端/服务器)传输文件 - 服务器上的.txt包含错误的内容

时间:2014-03-13 12:36:26

标签: c# client-server file-transfer

我正在研究客户端 - 服务器程序。客户端向服务器发送.txt文件,服务器接收此.txt文件并保存。

我的问题: 如果我发送包含以下内容的.txt文件:“Hello world”(不带“”) 然后服务器接收/保存一个.txt文件,其中包含以下内容“ÿÿÿÿÿÿÿÿÿÿÿ”(不带“”)。

两个文件的文件大小完全相同。

这是我的代码:

客户端: Program.cs的

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;

            namespace FTP_3___Client {
                public class Program {
                    static void Main(string[] args) {
                        new Client();
                    }
                }
            }

客户端: Client.cs:

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.IO;
            using System.Linq;
            using System.Net;
            using System.Net.Sockets;
            using System.Text;
            using System.Threading.Tasks;
            using FTP_3___Client;

            namespace FTP_3___Client {
                public class Client {

                    public string name { get; set; }
                    public TcpClient clientConnection { get; set; }
                    public NetworkStream nws { get; set; }
                    public StreamReader sr { get; set; }
                    public StreamWriter sw { get; set; }

                    public int sizeOfConnectedClients { get; set; }
                    public string clientnamelistString { get; set; }

                    public Client() {
                        connectToServer();
                        sendFile("zzz.txt");
                    }

                    public void connectToServer() {
                        Console.WriteLine("connect to server...");
                        clientConnection = new TcpClient();
                        IPAddress ipadress = IPAddress.Parse("127.0.0.1");
                        clientConnection.Connect(ipadress, 5555);
                        Console.WriteLine("connected to server!");

                        nws = clientConnection.GetStream();
                        sr = new StreamReader(nws);
                        sw = new StreamWriter(nws);
                    }

                    private void sendFile(string filename) {

                        FileStream fs = File.OpenRead(filename);

                        FileInfo fo = new FileInfo(filename);
                        long filesize = fo.Length;
                        Console.WriteLine("Filesize: " + filesize);

                        sw.WriteLine("/sendFile");
                        sw.Flush();

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

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

                        byte[] fileBytes = File.ReadAllBytes(filename);
                        sw.WriteLine(fileBytes.Length);
                        sw.Flush();
                        for (int i = 0; i < fileBytes.Length; i++) {
                            nws.WriteByte(fileBytes[i]);
                        }

                        sr.Close();
                        sw.Close();
                        nws.Close();

                        Console.WriteLine("File sends!");
                        Console.ReadLine();
                    }

                }
            }

服务器:Program.cs

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;

            namespace FTP_3___Server {
                class Program {
                    static void Main(string[] args) {
                        new Server();
                    }
                }
            }

服务器:Server.cs

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using System.Threading.Tasks;
            using System.Threading;
            using System.Net;
            using System.Net.Sockets;
            using System.IO;

            namespace FTP_3___Server {
                public class Server {

                    private IPAddress ipadress;
                    private TcpListener serverconnection;
                    private TcpClient clientconnection;
                    public NetworkStream nws { get; set; }
                    public StreamReader sr { get; set; }
                    public StreamWriter sw { get; set; }

                    public Server() {
                        startServer();
                        waitForClient();
                    }

                    public void startServer() {
                        ipadress = IPAddress.Parse("127.0.0.1");
                        serverconnection = new TcpListener(ipadress, 5555);
                        serverconnection.Start();
                    }

                    public void waitForClient() {
                        Console.WriteLine("Server startet. Waiting for incoming client connections...");
                        clientconnection = serverconnection.AcceptTcpClient();
                        nws = clientconnection.GetStream();
                        sr = new StreamReader(nws);
                        sw = new StreamWriter(nws);
                        Console.WriteLine("Client connected with Server!");

                        receiveMessage();
                    }

                    public void receiveMessage() {

                        string receivedMessage = sr.ReadLine();

                        switch (receivedMessage) {
                            case "/sendFile": {
                                    receiveFile();
                                    break;
                                }
                            default: {
                                    Console.WriteLine("Received undefined message: " + receivedMessage);
                                    break;
                                }
                        }

                    }

                    public void receiveFile() {
                        // Recieve filename
                        string filename = sr.ReadLine().Remove(0, 10);

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

                        Console.WriteLine("Filename: " + filename);
                        Console.WriteLine("Filesize: " + filesize);

                        long count = filesize;
                        int arrayLength = Convert.ToInt32(sr.ReadLine());
                        byte[] filebytes = new byte[arrayLength];

                        for (int i = 0; i < filebytes.Length; i++) {
                            filebytes[i] = (byte)nws.ReadByte();
                        }

                        try {
                            File.WriteAllBytes(filename, filebytes);
                        } catch (IOException e) {
                            Console.WriteLine(e.StackTrace);
                        }

                        sr.Close();
                        sw.Close();
                        nws.Close();

                        Console.WriteLine("File was written on HDD. Finish!");
                        Console.ReadLine();
                    }

                }
            }

1 个答案:

答案 0 :(得分:0)

C.Evenhuis在评论中给出了答案。