0
我正在處理客戶端服務器程序。客戶端向服務器發送一個.txt文件,服務器接收這個.txt文件並保存。.txt - 通過網絡(客戶端/服務器)傳輸文件 - 服務器上的.txt包含錯誤內容
我的問題: 如果我發送包含以下內容的文本文件:「世界,你好」(沒有「」) 然後服務器recevies /保存包含以下內容的文本文件「ÿÿÿÿÿÿÿÿÿÿÿ」(沒有「」)。
兩個文件的文件大小是精確相同。
這裏我的代碼:
客戶: 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();
}
}
}
不要nws.ReadByte()''的返回值盲目地轉換爲'byte',檢查其是否返回-1作爲表示流的結束。 –
我改變了它,現在收到的服務器只包含11個空格的.txt文件.... – krokvskrok
這意味着文件內容從未在服務器端收到*或*您已經讀過它。也許'StreamReader'已經讀取了這些字節並緩存了它們?我不認爲你應該從'StreamReader' *和*直接從底層流讀取。 –