基本信息
源码名称:C# UDP 文件传输实例源码
源码大小:0.12M
文件格式:.rar
开发语言:C#
更新时间:2014-06-19
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UDPMessager
{
    public partial class Form1 : Form
    {
        UDPSocket _udpSocket = null;  //数据接收
        UDPSender _udpSender = null;  //数据发送
        MyDataAnalyse _myDataAnalyse = null; //数据分析
        MyDataDeal _myDataDeal = null; //数据处理

        public Form1()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            //使各环节关联起来
            _udpSocket = new UDPSocket();
            _udpSender = new UDPSender(_udpSocket);
            _myDataAnalyse = new MyDataAnalyse(_udpSocket);
            _myDataDeal = new MyDataDeal(_myDataAnalyse);

            _myDataDeal.Login  = new LoginEventHandler(_myDataDeal_Login);
            _myDataDeal.LoginBack  = new LoginBackEventHandler(_myDataDeal_LoginBack);
            _myDataDeal.Logout  = new LogoutEventHandler(_myDataDeal_Logout);
            _myDataDeal.TxtMessageBack  = new TxtMessageBackEventHandler(_myDataDeal_TxtMessageBack);
            _myDataDeal.TxtMessageReceived  = new TxtMessageReceivedEventHandler(_myDataDeal_TxtMessageReceived);
            _myDataDeal.AskSendFile  = new AskSendFileEventHandler(_myDataDeal_AskSendFile);

            Application.ThreadExit  = new EventHandler(Application_ThreadExit);
        }

        void Application_ThreadExit(object sender, EventArgs e)
        {
            _udpSender.SendString(Msg.ZMsg5, "", "255.255.255.255", _udpSocket.Port); //退出
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            _myDataDeal.Start(); //开启数据处理环节
            _myDataAnalyse.Start(); //开启数据分析环节
            if (!_udpSocket.Start())
            {
                MessageBox.Show("端口绑定失败!");
                Environment.Exit(0);
            }//开启数据接收环节
            listView1.Items.Clear();
            //发送登录信息
            _udpSender.SendString(Msg.ZMsg1, Helper.GetUserName()   "^"   Helper.GetComputerName()   "^"   Helper.GetGroupName(), "255.255.255.255", _udpSocket.Port); //广播

        }

        #region udp event handlers
        /// <summary>
        /// 接收文本信件
        /// </summary>
        /// <param name="remoteIP"></param>
        /// <param name="remotePort"></param>
        /// <param name="txtMsg"></param>
        void _myDataDeal_TxtMessageReceived(string remoteIP, int remotePort, string txtMsg)
        {
            this.BeginInvoke((Action)delegate()
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    if (item.SubItems[3].Text == remoteIP)
                    {
                        frmMsg frmm = new frmMsg(remoteIP, remotePort, txtMsg, item.SubItems[1].Text, item.SubItems[2].Text, _udpSender);
                        frmm.Reply  = new ReplyEventHandler(frmm_Reply);
                        frmm.Show();
                    }
                }
            });
        }
        /// <summary>
        /// 信件反馈
        /// </summary>
        /// <param name="remoteIP"></param>
        /// <param name="remotePort"></param>
        /// <param name="txtMsgBack"></param>
        void _myDataDeal_TxtMessageBack(string remoteIP, int remotePort, string txtMsgBack)
        {
            this.BeginInvoke((Action)delegate()
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    if (item.SubItems[3].Text == remoteIP)
                    {
                        MessageBox.Show(item.SubItems[1].Text   "("   item.SubItems[2].Text   ") 打开信封\n信件内容:\n"   txtMsgBack);
                        break;
                    }
                }
                
            });
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="remoteIP"></param>
        /// <param name="remotePort"></param>
        /// <param name="remoteName"></param>
        void _myDataDeal_Logout(string remoteIP, int remotePort, string remoteName)
        {
            string[] infos = remoteName.Split('^');
            //if (infos.Length == 3)
            //{
                this.BeginInvoke((Action)delegate()  //这个可以确定不是在UI线程上执行 因此需要投递到UI线程上
                {
                    foreach (ListViewItem i in listView1.Items)
                    {
                        if (i.SubItems[3].Text.ToString() == remoteIP)
                        {
                            listView1.Items.Remove(i);
                            label1.Text = listView1.Items.Count.ToString()   "人在线";
                            break;
                        }
                    }
                });
            //}
        }
        /// <summary>
        /// 登录反馈
        /// </summary>
        /// <param name="remoteIP"></param>
        /// <param name="remotePort"></param>
        /// <param name="remoteName"></param>
        void _myDataDeal_LoginBack(string remoteIP, int remotePort, string remoteName)
        {
            string[] infos = remoteName.Split('^');
            if (infos.Length == 3)
            {
                this.BeginInvoke((Action)delegate()  //这个可以确定不是在UI线程上执行 因此需要投递到UI线程上
                {
                    bool flag = false;
                    foreach (ListViewItem i in listView1.Items)
                    {
                        if (i.SubItems[3].Text.ToString() == remoteIP)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        ListViewItem item = new ListViewItem();
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[0]));//登录名
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[1]));//主机名
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, remoteIP)); //远程IP
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[2])); //组名
                        listView1.Items.Add(item);
                        label1.Text = listView1.Items.Count.ToString()   "人在线";
                    }
                });
            }
        }
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="remoteIP"></param>
        /// <param name="remotePort"></param>
        /// <param name="remoteName"></param>
        void _myDataDeal_Login(string remoteIP, int remotePort, string remoteName)
        {
            string[] infos = remoteName.Split('^');
            if (infos.Length == 3)
            {
                this.BeginInvoke((Action)delegate()  //这个可以确定不是在UI线程上执行 因此需要投递到UI线程上
                {
                    bool flag = false;
                    foreach (ListViewItem i in listView1.Items)
                    {
                        if (i.SubItems[3].Text.ToString() == remoteIP)
                        {
                            flag = true;
                            break;
                        }
                    }
                    if (!flag)
                    {
                        ListViewItem item = new ListViewItem();
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[0]));//登录名
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[1]));//主机名
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, remoteIP)); //远程IP
                        item.SubItems.Add(new ListViewItem.ListViewSubItem(item, infos[2])); //组名
                        listView1.Items.Add(item);
                        label1.Text = listView1.Items.Count.ToString()   "人在线";
                    }
                });
                _udpSender.SendString(Msg.ZMsg2, Helper.GetUserName()   "^"   Helper.GetComputerName()   "^"   Helper.GetGroupName(), remoteIP, remotePort); //登录反馈
            }
        }
        /// <summary>
        /// 请求发送文件
        /// </summary>
        /// <param name="remoteIP"></param>
        /// <param name="remotePort"></param>
        /// <param name="filename"></param>
        /// <param name="length"></param>
        /// <param name="uid"></param>
        void _myDataDeal_AskSendFile(string remoteIP, int remotePort, string filename, long length, int uid)
        {
            this.BeginInvoke((Action)delegate()
            {
                foreach(ListViewItem item in listView1.Items)
                {
                    if(item.SubItems[3].Text == remoteIP)
                    {
                        string username = item.SubItems[1].Text;
                        string machinename = item.SubItems[2].Text;
                        frmReceiveFile frmr = new frmReceiveFile(uid, length, remoteIP, _udpSocket.Port, username, machinename, filename, _udpSender, _myDataDeal);

                        frmr.Show();
                        break;
                    }
                }
            });

        }
        #endregion

        /// <summary>
        /// 刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            //发送登录信息
            _udpSender.SendString(Msg.ZMsg1, Helper.GetUserName()   "^"   Helper.GetComputerName()   "^"   Helper.GetGroupName(), "255.255.255.255", _udpSocket.Port); //广播
        }
        /// <summary>
        /// 关于
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("UDPMessager通信demo,详细参见 http://www.cnblogs.com/xiaozhi_5638/");
        }
        /// <summary>
        /// 发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                if (textBox1.Text != "")
                {
                    string pass = "";
                    if (checkBox2.Checked)
                    {
                        using (frmLock frml = new frmLock())
                        {
                            if (frml.ShowDialog() == DialogResult.OK)
                            {
                                pass = frml.Pass;
                            }
                        }
                    }

                    string msg = pass   "^"   textBox1.Text; //为了方便简单相加 实际中不应该如此编程
                    string ip = "";
                    if (checkBox1.Checked)
                    {
                        ip = "255.255.255.255";
                    }
                    else
                    {
                        ip = listView1.SelectedItems[0].SubItems[3].Text;
                    }
                    _udpSender.SendString(Msg.ZMsg3, msg, ip, _udpSocket.Port); //发送文本消息
                    checkBox1.Checked = false;
                    checkBox2.Checked = false;
                    textBox1.Text = "";
                }
            }
        }

        /// <summary>
        /// 点击回复
        /// </summary>
        /// <param name="oldmsg"></param>
        /// <param name="remoteIP"></param>
        void frmm_Reply(string oldmsg, string remoteIP)
        {
            foreach (ListViewItem item in listView1.Items)
            {
                if (item.SubItems[3].Text == remoteIP)
                {
                    item.Selected = true;
                    break;
                }
            }
            textBox1.Text = oldmsg; //引用原文
            this.textBox1.Select(textBox1.Text.Length, 0);
            this.textBox1.Select();
        }
        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 发送文件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                using (OpenFileDialog ofd = new OpenFileDialog())
                {
                    ofd.Multiselect = false;
                    ofd.CheckFileExists = true;
                    if (ofd.ShowDialog() == DialogResult.OK)
                    {
                        string remoteip = listView1.SelectedItems[0].SubItems[3].Text;
                        string username = listView1.SelectedItems[0].SubItems[1].Text;
                        string machinename = listView1.SelectedItems[0].SubItems[2].Text;
                        frmSendFile frms = new frmSendFile(remoteip, _udpSocket.Port, username, machinename, ofd.FileName, _udpSender, _myDataDeal);
                        frms.Show();
                    }
                }
            }
        }
    }
}