



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 情节有波澜的课件
- 2026届浙江省杭师大附中化学高一上期中学业质量监测试题含解析
- 患者约束的安全管理
- 恶劣天气应急处置课件
- 少儿教育感恩节的策划方案
- 学校读书日活动策划方案
- 无废医院知识试题及答案
- 方剂试题及答案
- 驾照考试题及答案
- 证券基础知识试题及答案
- 浙江省建设工程检测技术人员(建筑材料及构配件)认证考试题库(含答案)
- 2025农村土地墓地买卖合同
- 建筑施工现场消防安全培训课件
- 中小企业数字化转型路径分析
- 新入职医生培训
- 劳务派遣员工离职协议书 (2024年版)
- 升学宴会模板
- 2024至2030年中国聚脲涂料行业市场发展调研及投资前景分析报告
- DB11∕T 420-2019 电梯安装、改造、重大修理和维护保养自检规则
- 十年(2015-2024)高考真题英语分项汇编(全国)专题 22 完形填空(新高考15空)(学生卷)
- JTJ073.1-2001 公路水泥混凝土路面 养护技术规范
评论
0/150
提交评论