C#简单的UDP通信例子

http://www.cnblogs.com/dongdonghuihui/archive/2009/09/25/1573782.html

1,UDP客户端

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

namespace WindowsFormsApplication14
{
    public partial class Form1 : Form
    {
        UdpClient udpClient;
        IPEndPoint ipEndPoint;

        public Form1()
        {
            InitializeComponent();
            udpClient = new UdpClient(12345);
            ipEndPoint = new IPEndPoint(IPAddress.Parse("192.168.1.241"), 60000);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //byte[] mybyte = Encoding.Default.GetBytes("nihao");
            
            byte[] mybyte = new byte[4];
            mybyte[0] = 0x12;
            mybyte[1] = 0x00;
            mybyte[2] = 0x34;
            mybyte[3] = 0x00;

            
            udpClient.Send(mybyte, mybyte.Length,ipEndPoint);
        }
    }
}

2,UDP服务器

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

namespace WindowsFormsApplication15
{
    public partial class Form1 : Form
    {
        UdpClient udpServer;
        IPEndPoint ipEndPoint;

        public Form1()
        {
            InitializeComponent();
            udpServer = new UdpClient(23456);
            ipEndPoint = new IPEndPoint(new IPAddress(0), 0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            byte[] data = udpServer.Receive(ref ipEndPoint);
            MessageBox.Show ( Encoding.Default.GetString(data));
        }
    }
}

3,读接收缓冲区当前数据个数

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

namespace WindowsFormsApplication15
{
    public partial class Form1 : Form
    {
        UdpClient udpServer;
        IPEndPoint ipEndPoint;

        public Form1()
        {
            InitializeComponent();
            udpServer = new UdpClient(23456);
            ipEndPoint = new IPEndPoint(new IPAddress(0), 0);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] outValue =  BitConverter.GetBytes(0);
            udpServer.Client.IOControl(IOControlCode.DataToRead, null, outValue);


            MessageBox.Show((BitConverter.ToInt32(outValue,0)).ToString());//发送数据发现此时数据增长,但不会超过8K
            //MessageBox.Show((BitConverter.ToString(outValue)).ToString());

        }
    }
}

 

4,上例有点复杂,看下面的例子,这个例子还可以说明如何设置非阻塞Socket通讯

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

namespace WindowsFormsApplication15
{
    public partial class Form1 : Form
    {
        UdpClient udpServer;
        IPEndPoint ipEndPoint;

        public Form1()
        {
            InitializeComponent();
            udpServer = new UdpClient(23456);
            ipEndPoint = new IPEndPoint(new IPAddress(0), 0);
            udpServer.Client.Blocking = false; //设置为非阻塞模式
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int buffSizeCurrent;
            buffSizeCurrent = udpServer.Client.Available;//取得缓冲区当前的数据的个数            
             MessageBox.Show(buffSizeCurrent.ToString());
           if (buffSizeCurrent > 0)     //有数据时候才读,不然会出异常哦
            {
                byte[] data = udpServer.Receive(ref ipEndPoint);
                MessageBox.Show(Encoding.Default.GetString(data));
           }
        }
    }
}

5,上面的例子是同步非阻塞的例子,但默认是同步阻塞的。除此之外还有异步的通讯方式(事件通知的方式触发),稍微复杂,以后补充。

分类:  网络通讯
0
0
(请您对文章做出评价)
« 上一篇: 中国电信DNS劫持导致的现象及解决方法
» 下一篇: 上海火车站订票信息(全)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值