资源描述
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("请先停止!","提示");
}
}
}
}
展开阅读全文