基本信息
源码名称:C# udp通讯入门示例代码
源码大小:0.04M
文件格式:.rar
开发语言:C#
更新时间:2016-02-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

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

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


客户端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            UdpClient udpClient = new UdpClient();
            udpClient.Connect("172.16.0.73", 11000);
            IPEndPoint ep = null;
            try
            {
                byte[] rddata = new byte[100];
                udpClient.Send(new byte[] { 0x31, 0x32 }, 2);
                while (true)
                {
                    rddata = udpClient.Receive(ref ep);
                    string aa = Encoding.ASCII.GetString(rddata);
                    Console.WriteLine(aa);
                }

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

服务端:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            UdpClient receivingUdpClient = new UdpClient(11000);
            IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
            try
            {
                byte[] sdata = Encoding.ASCII.GetBytes("dkasfjkdsjf");
                IPAddress ipa = IPAddress.Parse("172.16.0.73");
                receivingUdpClient.Receive(ref RemoteIpEndPoint);
                Console.WriteLine("远程:{0}", RemoteIpEndPoint.ToString());
                while (true)
                {
                    receivingUdpClient.Send(sdata, sdata.Length, new IPEndPoint(RemoteIpEndPoint.Address, RemoteIpEndPoint.Port));
                    System.Threading.Thread.Sleep(3000);
                    Console.Write('.');
                    break;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}