全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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.Text;using System.Threading;using System.Xml.Serialization;namespace 文件传输之接收端/ Form1 的摘要说明。/publicclassForm1 : System.Windows.Forms.Formprivate System.Windows.Forms.Label label1;private System.Windows.Forms.Button button1;private System.Windows.Forms.Button button2;private System.Windows.Forms.Label label2;private System.Windows.Forms.TextBox txtPort;private System.Windows.Forms.TextBox txtIP;private System.Windows.Forms.ListBox listBox1;private System.Windows.Forms.SaveFileDialog saveFileDialog1;/ 必需的设计器变量。/private System.ComponentModel.Container components = null;privateFileDetails fileDet;/private Int32 port;privateUdpClient udpClient;privateIPEndPoint ipEndPoint = null;privateFileStream fs = null;privatebyte receiveBytes = newbyte0;privateThread threadReceive;public Form1()InitializeComponent();/ 清理所有正在使用的资源。/ 应用程序的主入口点。/STAThreadstaticvoid Main() Application.Run(newForm1();privatevoid button1_Click(object sender, System.EventArgs e)try/ 改变按钮控件的状态UpdateControls(false); udpClient = newUdpClient(Int32.Parse(txtPort.Text);threadReceive = newThread(newThreadStart(ReceiveMessages);threadReceive.Start();/GetFileDetails();/ReceiveFile();catch(Exception exc)listBox1.Items.Add(exc.Message);privatevoid ReceiveMessages()while(true)GetFileDetails();ReceiveFile();/ 停止服务Private void button2_Click(object sender, System.EventArgs e)tryUpdateControls(true);txtIP.Text = ;threadReceive.Abort();if(fs != null)fs.Close();udpClient.Close();catch(Exception exc)listBox1.Items.Add(exc.Message);Public class FileDetailspublicstring FILETYPE = ;publicint FILESIZE = 0;Private void GetFileDetails()trylistBox1.Items.Add(开始等待文件细节.);receiveBytes = udpClient.Receive(ref ipEndPoint);txtIP.Text = ipEndPoint.Address.ToString();listBox1.Items.Add(读取文件细节);XmlSerializer fileSerializer = newXmlSerializer(typeof(FileDetails);MemoryStream stream = newMemoryStream();stream.Write(receiveBytes, 0, receiveBytes.Length);stream.Position = 0;fileDet = (FileDetails)fileSerializer.Deserialize(stream);listBox1.Items.Add(接收到的文件其类型是: + fileDet.FILETYPE + 其大小是: + fileDet.FILESIZE.ToString() + 字节);catch(Exception exc)listBox1.Items.Add(exc.Message);privatevoid ReceiveFile()trylistBox1.Items.Add(等待文件.);receiveBytes = udpClient.Receive(ref ipEndPoint);listBox1.Items.Add(开始接收文件.);/ 文件名筛选器的说明string s = 文件(* + fileDet.FILETYPE + ) ;/listBox1.Items.Add(s = + s );saveFileDialog1.Filter = s.ToString() + |* + fileDet.FILETYPE ;if(saveFileDialog1.ShowDialog() = DialogResult.OK)fs = newFileStream(saveFileDialog1.FileName, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);fs.Write(receiveBytes, 0, receiveBytes.Length);listBox1.Items.Add(文件已成功保存!);fs.Flush(); / 真正将数据写入文件中catch(Exception exc)/listBox1.Items.Add(exc.Message);MessageBox.Show(exc.Message);privatevoid Form1_Load(object sender, System.EventArgs e)UpdateControls(true);Form.CheckForIllegalCrossThreadCalls = false;/ 更新控件的状态。开始按钮与停止按钮(txtPort)的状态相反Private void UpdateControls(bool state)button1.Enabled = state; button2.Enabled = !state;txtPort.ReadOnly = !state;Private void
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T∕HMSA 009-2022 农产品气候品质评价 高粱
- 施工损坏电缆协议书
- 验收政府工程合同范本
- 施工船舶安全协议书
- 教育机构收费协议书
- 树子占地赔偿合同范本
- 新进员工试用协议书
- 乡村振兴签约协议书
- 施工合同中劳保协议
- 数据收集协议书范本
- 义务教育信息科技课程标准(2022版)测试真题及答案
- 2025新疆中新建数字发展有限责任公司招聘31人笔试考试参考题库及答案解析
- 高校科研项目资金管理规范与操作流程
- 2025年中级消防监控题库及答案(可下载)
- 硫化氢安全培训课件
- 2025版心律失常症状解析及护理方法训练
- 《新能源汽车机械基础》课件 项目五 新能源汽车机械传动应用
- 生殖科知识培训课件
- 2025年国家开放大学(电大)《公司治理与内部控制》期末考试备考题库及答案解析
- DB15∕T 3413-2024 住宅小区和商业用房供配电设施规范
- ICU专科护士学习汇报
评论
0/150
提交评论