//代码声明:此代码是从网络上下载过来的.项目名称TcpFileTransferDemo,已经不记得是从哪个网站哪个好友那里找来的。程序被我修改了很简单的一小部分.希望大家多交流交流.用到了委托(线程修改空间statusBar1.Text).还有几个功能一直是这个星期从网络上查询"C#传(送)(二进制)文件"学习中整理的来的.
//下面是发送程序端代码
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
//添加引用
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Text;
using System.IO;
using System.Data;
namespace CSharpData
{
/// <summary>
/// SendFileForm 的摘要说明。
/// </summary>
public class SendFileForm : System.Windows.Forms.Form
{
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 button3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.Panel panel1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public SendFileForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
setStatusDelegate = new SetStatusDelegate(SetStatus);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
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.button3 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.panel1 = new System.Windows.Forms.Panel();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(80, 64);
this.textBox2.Name = "textBox2";
this.textBox2.TabIndex = 15;
this.textBox2.Text = "800";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 72);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(56, 16);
this.label2.TabIndex = 14;
this.label2.Text = "通信端口";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(80, 24);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 13;
this.textBox1.Text = "192.168.1.250";
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(56, 16);
this.label1.TabIndex = 12;
this.label1.Text = "IP地址";
//
// button3
//
this.button3.Location = new System.Drawing.Point(48, 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(192, 64);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(64, 23);
this.button2.TabIndex = 9;
this.button2.Text = "停止监听";
//
// button1
//
this.button1.Location = new System.Drawing.Point(192, 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);
//
// button4
//
this.button4.Location = new System.Drawing.Point(152, 104);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(64, 23);
this.button4.TabIndex = 16;
this.button4.Text = "取消发送";
this.button4.Click += new System.EventHandler(this.button4_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 = 17;
this.statusBar1.Text = "statusBar1";
//
// panel1
//
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.textBox2);
this.panel1.Controls.Add(this.button3);
this.panel1.Controls.Add(this.button4);
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 = 18;
//
// SendFileForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 213);
this.Controls.Add(this.panel1);
this.Controls.Add(this.statusBar1);
this.Name = "SendFileForm";
this.Text = "局域网内发送文件";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
#region 程序逻辑
private Thread worker;
private string hostname;
private int port;
private string filename;
private void Start()
{
try // 捕获线程中止异常
{
SetStatus("连接中");
TcpClient tcpClient = new TcpClient();
tcpClient.SendTimeout = 30000;
tcpClient.NoDelay = true;
try // 捕获连接建立时出现的错误
{
tcpClient.Connect(hostname, port);
}
catch (Exception e)
{
SetStatus("连接出错:" + e.Message);
InvokeCancel();
worker = null;
return;
}
BinaryWriter writer = new BinaryWriter(tcpClient.GetStream());
try // 捕获文件传输中途出现的错误,并确保关闭连接
{
SetStatus("发送文件信息中");
FileInfo fi = new FileInfo(filename);
writer.Write(fi.Name);
writer.Write(fi.Length);
SetStatus("发送文件内容中");
FileStream fs = fi.OpenRead();
try // 保证关闭文件
{
long total = fi.Length;
byte[] buffer = new byte[8192];
int len;
while ((len = fs.Read(buffer, 0, 8192)) != 0)
{
writer.Write(buffer, 0, len);
SetStatus("发送文件内容中 - 剩余 " +
(total -= len).ToString() + " 字节");
}
SetStatus("文件发送完毕");
InvokeCancel();
}
finally
{
fs.Close();
}
}
catch (Exception e)
{
SetStatus("文件传输中途出错:" + e.Message);
InvokeCancel();
}
finally
{
try { writer.Close(); }
catch {}
try { tcpClient.Close(); }
catch {}
}
}
catch (ThreadAbortException)
{
SetStatus("用户中断");
InvokeCancel();
}
finally
{
worker = null;
}
}
#endregion
private void button1_Click(object sender, System.EventArgs e)
{
}
private void button3_Click(object sender, System.EventArgs e)
{
hostname = textBox1.Text.Trim();
if (hostname.Length == 0)
{
MessageBox.Show("请输入目标IP/域名。");
textBox1.Focus();
return;
}
port = 0;
try
{
port = int.Parse(textBox2.Text);
if (port < 1 || port > 65535) throw new ArgumentOutOfRangeException();
}
catch
{
MessageBox.Show("请输入有效的端口号。");
textBox2.Focus();
textBox2.SelectAll();
return;
}
//打开要发送的文件
OpenFileDialog Ofd = new OpenFileDialog();
Ofd.InitialDirectory = "C:/";
Ofd.Filter = "所有文件|*.*";
Ofd.FilterIndex = 1;
if (Ofd.ShowDialog(this) == DialogResult.OK)
filename = Ofd.FileName;
if (!File.Exists(filename))
{
MessageBox.Show("请输入存在的文件名。");
return;
}
button3.Enabled = false;
button4.Enabled = true;
worker = new Thread(new ThreadStart(Start));
worker.IsBackground = true;
worker.Start();
}
private void button4_Click(object sender, System.EventArgs e)
{
try { worker.Abort(); }
catch {}
button3.Enabled = true;
button4.Enabled = false;
}
#region 线程安全UI操作代码
private delegate void SetStatusDelegate(string status);
private SetStatusDelegate setStatusDelegate; // 赋值在构造函数里
private void SetStatusSynchronized(string status)
{
statusBar1.Text = status;
}
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
{
button4.Enabled = false;
button3.Enabled = true;
}
}
#endregion
}
}
分享到:
相关推荐
用C# 写的文件发送源代码。socket,多线程,基本上都用到了。
C#开发的局域网聊天软件,可以进行发送消息,传输文件等常见操作。点开即可运行,不需要做任何配置。业务代码逻辑规范,注释完全
C#超大文件发送与接收代码。用于开发超大文件的发送与接收
udp文件传输发送端源代码,每次数据报的大小可调,不丢包,ip地址,端口号,文件名需要自己设置
用C#接收和发送文件,支持大文件传输。这是我在国外网站下载的。
C#利用开源控件InTheHand.Net.Personal.dll制作的完整配对,发送及接收文件,可以连接手机也可以连接电脑。
本人是根据他人网页的代码在C#桌面程序中实现了UDP的接收发送,实现的方式是P2P方式,因此寻求广播方式、一对多方式就不用下载了,代码简单易懂,有点基础就可以看懂并应用,里面是接收发送一起完成。亲测可用,VS...
可以实现的功能1.根据默认路径在treeview中遍历文件夹和文件,若选择图片文件,则可以显示出图像信息。...3.连接COM口后可以向stm32中发送图片的RGB像素信息。以实现在STM32上面的刷图。...5.包含安装卸载项目代码。
C# 代码编写的FTP发送实例,程序运用了多线程技术,保证了在发送大量文件时,软件不会出现停顿无响应现象
本代码包括:服务器和客户端两个部分,启动服务器后,客户端与服务器建立连接,然后,就可以互相传送文件。
发送文件代码如下: 代码如下: /// /// Http上传文件 /// public static string HttpUploadFile(string url, string path) { // 设置参数 HttpWebRequest request = WebRequest.Create(url) as ...
C#仿飞鸽传书的部分功能,可以实现在局域网多人聊天、快速发送文件,运行后点击系统托盘处的图标可以进行简单设置,相比真正的飞鸽传书,本实例需要做的还有很多。
C#利用SharpPcap发送以太网报文(以太网原始帧),软件基本流程:从文件读取要发送的帧内容,然后找到电脑上的以太网卡,接下来利用SharpPcap来循环发送。 vs2008工程,全部源代码,可编译和测试。 以太网帧内容...
C# socket局域网内文件传输代码 使用 C#socket在局域网发送、接收文件等基本功能,下载 "android socket局域网内文件传输"可以在电脑和手机之间传输文件
c# DICOMSCU发送上传实例源码 AE,IP及端口需要配置正确 没有数据库,上传路径需要在config里面配置一下。
C# Socket通信(winform) 异步发送消息,服务端客户端通信,可以发送消息和文件并进行接收,代码注释详细 C# Socket通信(winform) 异步发送消息,服务端客户端通信,可以发送消息和文件并进行接收,代码注释详细 ...
C#SMS短信发送代码的FORM.CS文件,写的比较乱,经过测试可以发送信息
网站编写代码,asp架构下用c#实现文件的发送和接收功能
这个文件中有核心的图片传送代码,即将图片存入MemoryStream中转换成byte[]传送,之后可以接收转换。
[c#源码]通过TCP http 通信发送大文件.rar