基本信息
源码名称:TCP文件传输简例
源码大小:0.35M
文件格式:.zip
开发语言:C#
更新时间:2019-02-11
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 1 元×
微信扫码支付:1 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
C#TCP文件传输简例
{
//IPEndPoint ipEnd;
NetworkStream ns;
TcpClient client;
public Form1()
{
InitializeComponent();
lbMsg.Text = "空闲";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.CheckPathExists = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
tbFile.Text = openFileDialog.FileName;
}
private async void btnConnect_Click(object sender, EventArgs e)
{
// 转换
lbMsg.Text = "准备 ...";
IPAddress ipAddress;
if (!IPAddress.TryParse(tbServerIP.Text, out ipAddress))
{
MessageBox.Show("请输入正确的 IP 地址.");
return;
}
// 连接
lbMsg.Text = "连接中 ...";
client = new TcpClient();
try
{
await client.ConnectAsync(ipAddress, Convert.ToInt32(tbServerPort.Text));
}
catch
{
MessageBox.Show("与服务器连接失败.");
return;
}
lbMsg.Text = "已连接 ...";
ns = client.GetStream();
}
private async void btnSend_Click(object sender, EventArgs e)
{
FileInfo file;
FileStream fileStream;
try
{
file = new FileInfo(tbFile.Text);
fileStream = file.OpenRead();
}
catch
{
MessageBox.Show("无法打开指定的文件");
return;
}
// 发送文件信息
lbMsg.Text = "发送文件信息 ...";
{
byte[] fileName = Encoding.Default.GetBytes(file.Name);
byte[] fileNameLength = BitConverter.GetBytes(fileName.Length);
byte[] fileLength = BitConverter.GetBytes(file.Length);
await ns.WriteAsync(fileLength, 0, fileLength.Length);
await ns.WriteAsync(fileNameLength, 0, fileNameLength.Length);
await ns.WriteAsync(fileName, 0, fileName.Length);
}
// 获取允许
lbMsg.Text = "获取允许 ...";
{
byte[] permission = new byte[1];
await ns.ReadAsync(permission, 0, 1);
if (permission[0] != 1)
{
MessageBox.Show("服务器拒绝.");
return;
}
}
// 发送
lbMsg.Text = "发送中 ...";
progressBar1.Value = 0;
int read;
int totalWritten = 0;
byte[] buffer = new byte[32 * 1024]; // 32k chunks
while ((read = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await ns.WriteAsync(buffer, 0, read);
totalWritten = read;
progressBar1.Value = (int)((100d * totalWritten) / file.Length);
}
fileStream.Close(); // .Dispose();
client.Close();
MessageBox.Show("发送完成!");
}
}
C#TCP文件传输简例
接收端:
发送端:
public partial class Form1 : Form
{
//IPEndPoint ipEnd;
NetworkStream ns;
TcpClient client;
public Form1()
{
InitializeComponent();
lbMsg.Text = "空闲";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.CheckPathExists = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
tbFile.Text = openFileDialog.FileName;
}
private async void btnConnect_Click(object sender, EventArgs e)
{
// 转换
lbMsg.Text = "准备 ...";
IPAddress ipAddress;
if (!IPAddress.TryParse(tbServerIP.Text, out ipAddress))
{
MessageBox.Show("请输入正确的 IP 地址.");
return;
}
// 连接
lbMsg.Text = "连接中 ...";
client = new TcpClient();
try
{
await client.ConnectAsync(ipAddress, Convert.ToInt32(tbServerPort.Text));
}
catch
{
MessageBox.Show("与服务器连接失败.");
return;
}
lbMsg.Text = "已连接 ...";
ns = client.GetStream();
}
private async void btnSend_Click(object sender, EventArgs e)
{
FileInfo file;
FileStream fileStream;
try
{
file = new FileInfo(tbFile.Text);
fileStream = file.OpenRead();
}
catch
{
MessageBox.Show("无法打开指定的文件");
return;
}
// 发送文件信息
lbMsg.Text = "发送文件信息 ...";
{
byte[] fileName = Encoding.Default.GetBytes(file.Name);
byte[] fileNameLength = BitConverter.GetBytes(fileName.Length);
byte[] fileLength = BitConverter.GetBytes(file.Length);
await ns.WriteAsync(fileLength, 0, fileLength.Length);
await ns.WriteAsync(fileNameLength, 0, fileNameLength.Length);
await ns.WriteAsync(fileName, 0, fileName.Length);
}
// 获取允许
lbMsg.Text = "获取允许 ...";
{
byte[] permission = new byte[1];
await ns.ReadAsync(permission, 0, 1);
if (permission[0] != 1)
{
MessageBox.Show("服务器拒绝.");
return;
}
}
// 发送
lbMsg.Text = "发送中 ...";
progressBar1.Value = 0;
int read;
int totalWritten = 0;
byte[] buffer = new byte[32 * 1024]; // 32k chunks
while ((read = await fileStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await ns.WriteAsync(buffer, 0, read);
totalWritten = read;
progressBar1.Value = (int)((100d * totalWritten) / file.Length);
}
fileStream.Close(); // .Dispose();
client.Close();
MessageBox.Show("发送完成!");
}
}