这个C#文件传送程序是借鉴TcpFileTransferDemo项目的来的。C#文件传送源码很少,还有改进的地方就是可以用多线程进行大文件传送。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace CSharpData
{
/// /// SaveFileForm 的摘要说明。 ///
public class SaveFileForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.StatusBar statusBar1;
/// /// 必需的设计器变量。 ///
private System.ComponentModel.Container components = null;
public SaveFileForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
setStatusDelegate = new SetStatusDelegate(SetStatus);
getSaveFileDelegate = new GetSaveFileDelegate(GetSaveFile);
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// /// 清理所有正在使用的资源。 ///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 ///
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.button4 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.Controls.Add(this.textBox2);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.button4);
this.panel1.Controls.Add(this.button3);
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(8, 16);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(272, 152);
this.panel1.TabIndex = 0;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(96, 64);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 15;
this.textBox2.Text = "800";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 16);
this.label2.TabIndex = 14;
this.label2.Text = "通信端口";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 13;
this.textBox1.Text = "192.168.1.250";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(80, 16);
this.label1.TabIndex = 12;
this.label1.Text = "IP地址";
//
// button4
//
this.button4.Location = new System.Drawing.Point(160, 104);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 23);
this.button4.TabIndex = 11;
this.button4.Text = "接收文件";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(56, 104);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(64, 23);
this.button3.TabIndex = 10;
this.button3.Text = "发送文件";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(200, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 9;
this.button2.Text = "停止监听";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(200, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(64, 23);
this.button1.TabIndex = 8;
this.button1.Text = "开始监听";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 191);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Size = new System.Drawing.Size(292, 22);
this.statusBar1.TabIndex = 1;
this.statusBar1.Text = "statusBar1";
//
// SaveFileForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 213);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.panel1);
this.Name = "SaveFileForm";
this.Text = "局域网内接收文件";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
port = 0;
try
{
port = int.Parse(textBox2.Text);
if (port < 1 || port > 65535) throw new ArgumentOutOfRangeException();
}
catch
{
MessageBox.Show("请输入有效的端口号。");
textBox2.Focus();
textBox2.SelectAll();
return;
}
button1.Enabled = false;
button2.Enabled = true;
worker = new Thread(new ThreadStart(Start));
worker.IsBackground = true;
worker.Start();
}
private void button2_Click(object sender, System.EventArgs e)
{
try { worker.Abort(); }
catch {}
}
private void button3_Click(object sender, System.EventArgs e)
{
}
private void button4_Click(object sender, System.EventArgs e)
{
}
#region 程序逻辑
private Thread worker;
private int port;
private void Start()
{
try // 捕获线程中止异常
{
SetStatus("开始侦听端口");
TcpListener tcpListener = new TcpListener(IPAddress.Any, port);
try // 捕获开始侦听时出现的错误,例如端口已被占用
{
tcpListener.Start();
}
catch (Exception e)
{
SetStatus("侦听端口出错:" + e.Message);
InvokeCancel();
return;
}
try // 确保停止侦听
{
SetStatus("等待传入连接中");
while (true) // 一直循环,直到线程被中止。但每次只处理一个连接
{
// 没有等待传入的连接就一直等待。不使用异步模型以提高程序可读性
while (!tcpListener.Pending())
Thread.Sleep(100);
TcpClient tcpClient = tcpListener.AcceptTcpClient();
tcpClient.NoDelay = true;
tcpClient.ReceiveTimeout = 30000;
BinaryReader reader = new BinaryReader(tcpClient.GetStream());
try // 捕获文件传输中途出现的错误,并确保关闭连接
{
string filename = reader.ReadString();
long total = reader.ReadInt64();
SetStatus("收到文件传输请求,文件名 " + filename +
",大小 " + total.ToString() + " 字节");
string saveAs = GetSaveFile(filename);
if (saveAs == null)
{
SetStatus("操作已取消。等待下一连接中");
// finally 段会帮忙关闭连接
}
else
{
SetStatus("接收文件中");
FileStream fs = File.Create(saveAs);
try
{
byte[] buffer = new byte[8192];
int len;
while (total > 0)
{
len = reader.Read(buffer, 0, 8192);
if (len == 0)
throw new IOException("发送方中止了连接");
fs.Write(buffer, 0, len);
SetStatus("接收文件中 - 剩余 " +
(total -= len).ToString() + " 字节");
}
SetStatus("文件已保存至 " + saveAs +
"。等待下一连接中");
}
finally
{
fs.Close();
}
}
}
catch (Exception e)
{
SetStatus("文件传输中途出错:" + e.Message);
InvokeCancel();
}
finally
{
try { reader.Close(); }
catch {}
try { tcpClient.Close(); }
catch {}
}
}
}
finally
{
tcpListener.Stop();
}
}
catch (ThreadAbortException)
{
SetStatus("用户中断");
InvokeCancel();
}
finally
{
worker = null;
}
}
#endregion
#region 线程安全UI操作代码
private delegate void SetStatusDelegate(string status);
private SetStatusDelegate setStatusDelegate; // 赋值在构造函数里
private void SetStatus(string status)
{
if (InvokeRequired)
Invoke(setStatusDelegate, new object[] { status });
else
statusBar1.Text = status;
}
private void InvokeCancel()
{
if (InvokeRequired)
Invoke(new MethodInvoker(InvokeCancel));
else
{
button2.Enabled = false;
button1.Enabled = true;
}
}
private delegate string GetSaveFileDelegate(string filename);
private GetSaveFileDelegate getSaveFileDelegate; //
private string GetSaveFile(string filename)
{
if (InvokeRequired)
return (string)Invoke(getSaveFileDelegate, new object[] { filename });
else
{
//打开要发送的文件
SaveFileDialog sfd = new SaveFileDialog();
sfd.FileName = filename;
if (sfd.ShowDialog(this) == DialogResult.OK)
return sfd.FileName;
else
return null;
}
}
#endregion
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
- 204.
- 205.
- 206.
- 207.
- 208.
- 209.
- 210.
- 211.
- 212.
- 213.
- 214.
- 215.
- 216.
- 217.
- 218.
- 219.
- 220.
- 221.
- 222.
- 223.
- 224.
- 225.
- 226.
- 227.
- 228.
- 229.
- 230.
- 231.
- 232.
- 233.
- 234.
- 235.
- 236.
- 237.
- 238.
- 239.
- 240.
- 241.
- 242.
- 243.
- 244.
- 245.
- 246.
- 247.
- 248.
- 249.
- 250.
- 251.
- 252.
- 253.
- 254.
- 255.
- 256.
- 257.
- 258.
- 259.
- 260.
- 261.
- 262.
- 263.
- 264.
- 265.
- 266.
- 267.
- 268.
- 269.
- 270.
- 271.
- 272.
- 273.
- 274.
- 275.
- 276.
- 277.
- 278.
- 279.
- 280.
- 281.
- 282.
- 283.
- 284.
- 285.
- 286.
- 287.
- 288.
- 289.
- 290.
- 291.
- 292.
- 293.
- 294.
- 295.
- 296.
- 297.
- 298.
- 299.
- 300.
- 301.
- 302.
- 303.
- 304.
- 305.
- 306.
- 307.
- 308.
- 309.
- 310.
- 311.
- 312.
- 313.
- 314.
- 315.
- 316.
- 317.
- 318.
- 319.
- 320.
- 321.
- 322.
- 323.
- 324.
- 325.
- 326.
- 327.
- 328.
- 329.
- 330.
- 331.
- 332.
- 333.
- 334.
- 335.
- 336.
- 337.
- 338.
- 339.
- 340.
- 341.
- 342.
- 343.
- 344.
- 345.
- 346.
- 347.
- 348.
- 349.
- 350.
- 351.
- 352.
- 353.
- 354.
- 355.
- 356.
- 357.
- 358.
- 359.
- 360.
- 361.
- 362.
- 363.
- 364.
- 365.
- 366.
- 367.
- 368.
- 369.
- 370.
- 371.
- 372.
- 373.
- 374.
- 375.
- 376.
- 377.
- 378.
- 379.
- 380.
- 381.
- 382.
- 383.
【编辑推荐】