C# TcpClient網(wǎng)絡編程傳輸文件的示例
一、簡述
利用C# TcpClient在局域網(wǎng)內(nèi)傳輸文件,可是文件發(fā)送到對面的時候卻要重新命名文件的。那可不可以連著文件名與文件一起發(fā)過去呢?
二、內(nèi)容

如上圖,把文件名字符流的長度的值的字符流(這個文件名字符流長度的值固定是11位的字符串,不足11位前面補0)與文件名的字符流合為一個byte數(shù)組然后與文件發(fā)送到對面。對面接收后解析出文件名字符流長度的值后,再根據(jù)長度解析出文件名,接下來再獲取文件流。
服務端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace TCPSendFile
{
public partial class Form1 : Form
{
public delegate void TxtReceiveAddContentEventHandler(string txtValue);
public Form1()
{
InitializeComponent();
}
public void TxtAddContent(string txtValue)
{
if (textBox1.InvokeRequired)
{
TxtReceiveAddContentEventHandler addContent = TxtAddContent;
textBox1.Invoke(addContent, new object[] { txtValue });
}
else
{
textBox1.Text = txtValue + "\r\n" + textBox1.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
TcpListener tcpListener = new TcpListener(IPAddress.Any, 18001);
tcpListener.Start();
textBox1.Text = "開始偵聽...";
Thread thread = new Thread(SendFileFunc);
thread.Start(tcpListener);
thread.IsBackground = true;
}
public void SendFileFunc(object obj)
{
TcpListener tcpListener = obj as TcpListener;
while (true)
{
try
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
if (tcpClient.Connected)
{
NetworkStream stream = tcpClient.GetStream();
string fileName = "testfile.rar";
byte[] fileNameByte = Encoding.Unicode.GetBytes(fileName);
byte[] fileNameLengthForValueByte = Encoding.Unicode.GetBytes(fileNameByte.Length.ToString("D11"));
byte[] fileAttributeByte = new byte[fileNameByte.Length + fileNameLengthForValueByte.Length];
fileNameLengthForValueByte.CopyTo(fileAttributeByte, 0); //文件名字符流的長度的字符流排在前面。
fileNameByte.CopyTo(fileAttributeByte, fileNameLengthForValueByte.Length); //緊接著文件名的字符流
stream.Write(fileAttributeByte, 0, fileAttributeByte.Length);
FileStream fileStrem = new FileStream(Application.StartupPath + "\\WebFile\\" + fileName, FileMode.Open);
int fileReadSize = 0;
long fileLength = 0;
while (fileLength < fileStrem.Length)
{
byte[] buffer = new byte[2048];
fileReadSize = fileStrem.Read(buffer, 0, buffer.Length);
stream.Write(buffer, 0, fileReadSize);
fileLength += fileReadSize;
}
fileStrem.Flush();
stream.Flush();
fileStrem.Close();
stream.Close();
TxtAddContent(string.Format("{0}文件發(fā)送成功", fileName));
}
}
catch (Exception ex)
{
}
}
}
}
}
客戶端
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace TCPReceiveFile
{
public partial class Form1 : Form
{
public delegate void TxtReceiveAddContentEventHandler(string txtValue);
public Form1()
{
InitializeComponent();
}
public void TxtReceiveAddContent(string txtValue)
{
if (txtReceive.InvokeRequired)
{
TxtReceiveAddContentEventHandler addContent = TxtReceiveAddContent;
txtReceive.Invoke(addContent, new object[] { txtValue });
}
else
{
txtReceive.Text = txtValue + "\r\n" + txtReceive.Text;
}
}
private void button1_Click(object sender, EventArgs e)
{
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.101"), 18001);
TxtReceiveAddContent("連接中。。。。。");
Thread th = new Thread(ReceiveFileFunc);
th.Start(ipEndPoint);
th.IsBackground = true;
}
private void ReceiveFileFunc(object obj)
{
IPEndPoint ipEndPoint = obj as IPEndPoint;
TcpClient tcpClient = new TcpClient();
try
{
tcpClient.Connect(ipEndPoint);
}
catch
{
TxtReceiveAddContent("連接失敗,找不到服務器!");
}
if (tcpClient.Connected)
{
NetworkStream stream = tcpClient.GetStream();
if (stream != null)
{
byte[] fileNameLengthForValueByte = Encoding.Unicode.GetBytes((256).ToString("D11"));
byte[] fileNameLengByte = new byte[1024];
int fileNameLengthSize = stream.Read(fileNameLengByte, 0, fileNameLengthForValueByte.Length);
string fileNameLength = Encoding.Unicode.GetString(fileNameLengByte, 0, fileNameLengthSize);
TxtReceiveAddContent("文件名字符流的長度為:" + fileNameLength);
int fileNameLengthNum = Convert.ToInt32(fileNameLength);
byte[] fileNameByte = new byte[fileNameLengthNum];
int fileNameSize = stream.Read(fileNameByte, 0, fileNameLengthNum);
string fileName = Encoding.Unicode.GetString(fileNameByte, 0, fileNameSize);
TxtReceiveAddContent("文件名為:" + fileName);
string dirPath = Application.StartupPath + "\\WebFile";
if(!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}
FileStream fileStream = new FileStream(dirPath + "\\" + fileName, FileMode.Create, FileAccess.Write);
int fileReadSize = 0;
byte[] buffer = new byte[2048];
while ((fileReadSize = stream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, fileReadSize);
}
fileStream.Flush();
fileStream.Close();
stream.Flush();
stream.Close();
tcpClient.Close();
TxtReceiveAddContent("接收成功");
}
}
}
}
}
實例圖


以上就是C# TcpClient網(wǎng)絡編程傳輸文件的示例的詳細內(nèi)容,更多關(guān)于C# TcpClient傳輸文件的資料請關(guān)注腳本之家其它相關(guān)文章!
- C# 網(wǎng)絡編程之UDP
- c# 網(wǎng)絡編程之tcp
- c# 網(wǎng)絡編程之http
- 深入學習C#網(wǎng)絡編程之HTTP應用編程(下)
- 深入學習C#網(wǎng)絡編程之HTTP應用編程(上)
- 淺談C#網(wǎng)絡編程詳解篇
- 詳解C# 網(wǎng)絡編程系列:實現(xiàn)類似QQ的即時通信程序
- 總結(jié)C#網(wǎng)絡編程中對于Cookie的設定要點
- C# Socket網(wǎng)絡編程實例
- C#網(wǎng)絡編程基礎(chǔ)之進程和線程詳解
- c# socket網(wǎng)絡編程接收發(fā)送數(shù)據(jù)示例代碼
- C#開發(fā)之Socket網(wǎng)絡編程TCP/IP層次模型、端口及報文等探討
- C#網(wǎng)絡編程中常用特性介紹
相關(guān)文章
C# MVC 使用LayUI實現(xiàn)下拉框二級聯(lián)動的功能
這篇文章主要介紹了C# MVC 如何使用LayUI實現(xiàn)下拉框二級聯(lián)動,文中示例代碼非常詳細,供大家參考和學習,感興趣的朋友可以了解下2020-06-06
c#橋接模式(bridge結(jié)構(gòu)模式)用法實例
這篇文章主要介紹了c#橋接模式(bridge結(jié)構(gòu)模式)用法,較為詳細的分析了橋接模式的原理與用法實例,具有一定的參考借鑒價值,需要的朋友可以參考下2014-12-12
C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)示例詳解
這篇文章主要給大家介紹了關(guān)于C#中參數(shù)數(shù)組、引用參數(shù)和輸出參數(shù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-05-05
c# WPF設置軟件界面背景為MediaElement并播放視頻
這篇文章主要介紹了c# WPF如何設置軟件界面背景為MediaElement并播放視頻,幫助大家更好的理解和學習使用c#,感興趣的朋友可以了解下2021-03-03

