收藏 分销(赏)

文件传输接收端.docx

上传人:pc****0 文档编号:6660173 上传时间:2024-12-19 格式:DOCX 页数:4 大小:21.32KB 下载积分:10 金币
下载 相关 举报
文件传输接收端.docx_第1页
第1页 / 共4页
文件传输接收端.docx_第2页
第2页 / 共4页


点击查看更多>>
资源描述
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 文件传输之接收端 { ///<summary> /// Form1 的摘要说明。 ///</summary> publicclassForm1 : System.Windows.Forms.Form { private 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; ///<summary> /// 必需的设计器变量。 ///</summary> private System.ComponentModel.Container components = null; privateFileDetails fileDet; // private Int32 port; privateUdpClient udpClient; privateIPEndPoint ipEndPoint = null; privateFileStream fs = null; privatebyte[] receiveBytes = newbyte[0]; privateThread threadReceive; public Form1() { InitializeComponent(); } ///<summary> /// 清理所有正在使用的资源。 ///<summary> /// 应用程序的主入口点。 ///</summary> [STAThread] staticvoid 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) { try { UpdateControls(true); txtIP.Text = ""; threadReceive.Abort(); if(fs != null) fs.Close(); udpClient.Close(); } catch(Exception exc) { listBox1.Items.Add(exc.Message); } } Public class FileDetails { publicstring FILETYPE = ""; publicint FILESIZE = 0; } Private void GetFileDetails() { try { listBox1.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() { try { listBox1.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 Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { // 当服务器在运行时,不允许直接关闭,必须先停止服务,才可关闭 if(button1.Enabled == false) { e.Cancel = true; MessageBox.Show("请先停止!","提示"); } } } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服