当前位置:编程学习 > C#/ASP.NET >>

UDP多播问题

刚学套接字,大家帮我看看一下代码有什么问题...(UDP多播)

===服务器端================================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;


using System.Net.Sockets;
using System.Threading;


namespace UDPServer
{
    public partial class FrmServer : Form
    {
        //组播组IP地址
        private IPAddress groupIP;
        //组播组端口
        private IPEndPoint groupPort;

        //客户端
        private UdpClient udpClient;

        public FrmServer()
        {
            InitializeComponent();

            groupIP = new IPAddress(new byte[] { 225, 0, 0, 1 });
            groupPort = new IPEndPoint(groupIP, 8080);
            udpClient = new UdpClient(8081);

            udpClient.JoinMulticastGroup(groupIP);
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            Byte[] bs = Encoding.ASCII.GetBytes(txtMessage.Text);
            udpClient.Send(bs, bs.Length);
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            udpClient.Close();
            Application.Exit();
        }
    }
}
=============================================





=======客户端================================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Net;
using System.Net.Sockets;

using System.Threading;

public delegate void Notify(string text);

namespace UDPClient
{
    public partial class FrmClient : Form
    {
        private IPAddress groupIP;
        private IPEndPoint groupPort;
        private UdpClient udpClient;
        private Thread thread;

        public FrmClient()
        {
            InitializeComponent();

            groupIP = new IPAddress(new byte[] { 225, 0, 0, 1 });
            groupPort = new IPEndPoint(groupIP, 8080);

            udpClient = new UdpClient(8082);
            udpClient.JoinMulticastGroup(groupIP);

            thread = new Thread(new ThreadStart(Receive));
            thread.Start();
        }

        private void Receive()
        {
            IPEndPoint data = groupPort;
            Byte[] bs = udpClient.Receive(ref data);

            txtMessage.Text = Encoding.ASCII.GetString(bs);

            Thread.Sleep(2000);
        }
    }
}
=========================================================================
补充:.NET技术 ,  .NET Framework
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,