文档详情

3.tcp文件传输.doc

发布:2017-07-02约1.89万字共19页下载文档
文本预览下载声明
ZHUHAI CAMPAUS OF BEIJING INSTITUTE OF TECHNOLOGY 班级 学号 姓名 指导教师 成绩 实验题目 基于TCP协议的文件传输程序设计 实验时间 实验题目 基于TCP协议的文件传输程序设计,要求TCP服务器端与客户端都可以给对方发送文件。 实验要求 协议具体交互过程如下: 服务器程序启动,点击侦听按钮,并开始侦听,如下: 客户端开始启动。输入服务器IP和端口号,点击连接按钮开始连接。比如: 客户端点击发送文件按钮,便弹出打开文件对话框,用户选择要发送的文件,点击打开按钮,便可以开始发送件了,比如: 接收端也会显示接收文件的进度,比如: 三、 实验代码 客户端 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.Net; using System.Net.Sockets; using System.Diagnostics; using System.IO; namespace TestClient { public partial class Form1 : Form { Thread receiveThread = null; Socket clientSocket = null; bool isExit = false; int block = 1024; string fileName; int blockCount = 0; FileStream fs = null; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); } delegate void SetListBoxCallback(string str);//定义操作ListBox控件的委托类型 /// summary /// 操作ListBox控件的方法 /// /summary /// param name=str/param private void SetListBox(string str) { if (listBoxInfo.InvokeRequired) { SetListBoxCallback setListBoxCallback = new SetListBoxCallback(SetListBox); listBoxInfo.Invoke(setListBoxCallback, str);//委托主线程操作listbox控件 } else { listBoxInfo.Items.Add(str);//主线程操作listbox控件 } } delegate void SetProcessBarCallback();//定义操作ListBox控件的委托类型 /// summary /// 操作ListBox控件的方法 /// /summary /// param name=str/param private void SetProcessBar() { if (progressBar1.InvokeRequired)
显示全部
相似文档