[英]C# file transfer
我對使用C#語言發送和接收文件的方式有一個疑問。 我創建了一個簡單的文件傳輸Windows窗體應用程序,但僅支持.txt文件格式。 如果我嘗試發送圖像文件或ms word文檔文件,則可能會在接收方完全接收到它。 但是,收到的文件不可讀,無法打開。 我已經做了類似的Java應用程序,但是它適用於任何文件格式,並且我使用相同的邏輯在C#應用程序中實現。 有人可以給我一些建議嗎?
SENDING SIDE:
// establish connection
int port = Convert.ToInt32(txtLocalPort.Text) - 5;
TcpListener listener = new TcpListener(IPAddress.Parse(txtLocalIP.Text), port);
listener.Start();
// get file size
byte[] data = File.ReadAllBytes(downFile);
int fSize = data.Length;
// calculate block numbers, 1024 bytes each block
int block = fSize / 1024;
// leftover file bytes, less than 1024 bytes
int byteLeft = fSize % 1024;
// convert String to byte
String cmd = "SEND_FILE" + fSize.ToString();
buff = new byte[1024];
buff = Encoding.ASCII.GetBytes(cmd);
// send message in byte
msgSocket.Send(buff);
// accept connection
TcpClient client = listener.AcceptTcpClient();
// remote host connected
if (client.Connected == true)
{
// medium to read file bytes from file chosen
BinaryReader readByte = new BinaryReader(File.Open(downFile, FileMode.Open));
// medium to send file bytes
NetworkStream dataOUT = client.GetStream();
// send file bytes based on calculated block numbers
for (int i = 0; i < block; i++)
{
// buffer size
buff = new byte[1024];
// read file bytes from file chosen
readByte.Read(buff, 0, buff.Length);
// send file bytes
dataOUT.Write(buff, 0, buff.Length);
dataOUT.Flush();
}
// leftover file bytes
// buffer size
buffLeft = new byte[byteLeft];
// read leftover file bytes from file chosen
readByte.Read(buffLeft, 0, buffLeft.Length);
// send leftover file bytes
dataOUT.Write(buff, 0, buffLeft.Length);
dataOUT.Flush();
MessageBox.Show("File sent successfully.", "File Share", MessageBoxButtons.OK, MessageBoxIcon.Information);
// close the medium
readByte.Close();
dataOUT.Close();
client.Close();
listener.Stop();
接收端:
// establish connection
int port = Convert.ToInt32(txtRemotePort.Text) - 5;
TcpClient client = new TcpClient(Dns.GetHostEntry(IPAddress.Parse(txtRemoteIP.Text)).HostName.ToString(), port);
// connected to remote host
if (client.Connected == true)
{
// get invalid character
String invalid = new String(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
// remove invalid character
foreach (char c in invalid)
{
fileName = fileName.Replace(c.ToString(), "");
}
String downFile = Path.Combine(@"D:\", fileName);
// file size
int fSize = fileSize;
// calculate block numbers, 1024 bytes each block
int block = fSize / 1024;
// leftover file bytes, less than 1024 bytes
int byteLeft = fSize % 1024;
// medium to receive file bytes
NetworkStream dataIN = client.GetStream();
// medium to write file bytes to new file
BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Create));
// receive file bytes based on calculated block numbers
for (int i = 0; i < block; i++)
{
// buffer size
buff = new byte[1024];
// receive file bytes
dataIN.Read(buff, 0, buff.Length);
dataIN.Flush();
// write file bytes to new file
writeByte.Write(buff, 0, buff.Length);
writeByte.Flush();
}
// receive leftover file bytes
// buffer size
buffLeft = new byte[byteLeft];
// receive file bytes
dataIN.Read(buffLeft, 0, buffLeft.Length);
dataIN.Flush();
// write file bytes to new file
writeByte.Write(buffLeft, 0, buffLeft.Length);
writeByte.Flush();
MessageBox.Show("File downloaded successfully.", "File Share", MessageBoxButtons.OK, MessageBoxIcon.Information);
// close the medium
writeByte.Close();
dataIN.Close();
client.Close();
我想我已經發現您做錯了什么:
BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Create));
使用BinaryWriter writeByte = new BinaryWriter(File.Open(downFile, FileMode.Append));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.