WEB开发网
开发学院软件开发C语言 Udp打洞简单描述 阅读

Udp打洞简单描述

 2009-03-18 08:21:12 来源:WEB开发网   
核心提示: 服务器端,Serverusing System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;us

服务器端,

Server

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;
using System.Net;
using System.Net.Sockets;
using lib;
using System.Threading;

namespace Server
{
    public partial class FrmServer : Form
    {

        private UdpClient udpServer;
        private List<UserInfo> userInfoList;
        private IPEndPoint remotePoint;//目标IP地址
        private Thread listenThread;//监听线程

        public FrmServer()
        {
            InitializeComponent();
            userInfoList = new List<UserInfo>();
            listenThread = new Thread(new ThreadStart(Run));
        }

        /**//// <summary>
        /// 连接按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnConnect_Click(object sender, EventArgs e)
        {
            int port = int.Parse(this.txtPort.Text);
            udpServer = new UdpClient(port);
            this.btnConnect.Enabled = false;
            listenThread.Start();
        }

        /**//// <summary>
        /// 监听线程事件
        /// </summary>
        private void Run()
        {
            byte[] buffer;
            while (true)
            {
                buffer = udpServer.Receive(ref remotePoint);
                try
                {
                    object msgObj = SerializeHelper.Deserialize(buffer);
                    Type msgType = msgObj.GetType();
                    if (msgType == typeof(UserInfo))//用户登陆,返回当前所有用户登陆后的信息
                    {
                        UserInfo userInfo = (UserInfo)msgObj;
                        userInfo.Address = remotePoint;
                        this.userInfoList.Add(userInfo);
                        this.lstUserInfo.BeginInvoke(new System.EventHandler(UpdateUI), userInfo);
                        UserList userList = new UserList();
                        userList.userInfoList = this.userInfoList;
                        byte[] buff = SerializeHelper.Serialize(userList);
                        this.udpServer.Send(buff, buff.Length, remotePoint);
                    }
                    if (msgType == typeof(GetUserList))//获取所有登陆后的用户信息
                    {
                        UserList userList = new UserList();
                        userList.userInfoList = this.userInfoList;
                        byte[] buff = SerializeHelper.Serialize(userList);
                        udpServer.Send(buff, buff.Length, remotePoint);
                    }
                    Thread.Sleep(50);
                }
                catch
                {

                }
            }
        }
       
        /**//// <summary>
        /// 跨线程访问控件
        /// </summary>
        /// <param name="o"></param>
        /// <param name="e"></param>
        private void UpdateUI(object o, System.EventArgs e)
        {
            this.lstUserInfo.Items.Add(((UserInfo)o).UserName + ":" + ((UserInfo)o).Address.ToString());
        }

        /**//// <summary>
        /// 关闭按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmServer_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                listenThread.Abort();
                udpServer.Close();
            }
            catch
            { }
        }

    }
}

上一页  1 2 3 4 

Tags:Udp 简单 描述

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接