菜鸟学C#

淡泊以明志,宁静以致远

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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

posted on 2009-09-25 09:53  东东会会  阅读(19386)  评论(0编辑  收藏  举报