c# 使用UDPClient实现异步通信

下载:http://download.csdn.net/download/cdtaixf/7239105


server:


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

namespace AsyncServer
{
    class Program
    {
        private static IPEndPoint epServer;
        private static IPEndPoint epClient;
        private static UdpClient server;

        static void Main(string[] args)
        {
            epServer = new IPEndPoint(IPAddress.Any, 11000);    //设置服务器端口,IP是本程序所在PC的内网IP
            epClient = new IPEndPoint(IPAddress.Any, 0);    //设置客户端,任意IP,任意端口号
            server = new UdpClient(epServer);   //绑定设置的服务器端口和IP
            Console.WriteLine("listening...");
            while (true) {
                //下面三行代码经过修改,原代码如下
                //server.BeginReceive(new AsyncCallback(ReceiveCallback), null);
                //该代码会被挂起,接收到一个消息时启动一个线程,该线程启动函数是:ReceiveCallback,在该函数中
                //会修改epClient的值,但程序会继续向下执行,执行到server.BeginSend时,发现epClient是无效的,报异常
                //所以改为如下代码,可以正常运行
                IAsyncResult iar = server.BeginReceive(null, null);
                byte[] receiveData = server.EndReceive(iar, ref epClient);
                Console.WriteLine("Received a message from [{0}]: {1}", epClient, Encoding.ASCII.GetString(receiveData));
                //以下是发送消息回客户端
                string strSend = "hello " + epClient.ToString();
                byte[] sendData = Encoding.ASCII.GetBytes(strSend);
                //下面一行代码,将启动一个线程,该线程启动函数:SendCallback,该函数结束挂起的异步发送
                server.BeginSend(sendData, sendData.Length, epClient, new AsyncCallback(SendCallback), null);
            }
        }
        //此函数未被使用,如需使用,可以借鉴
        private static void ReceiveCallback(IAsyncResult iar)
        {
            byte[] receiveData = server.EndReceive(iar, ref epClient);
            Console.WriteLine("Received a message from [{0}]: {1}", epClient, Encoding.ASCII.GetString(receiveData));
        }

        private static void SendCallback(IAsyncResult iar)
        {
            int sendCount = server.EndSend(iar);
            if (sendCount == 0)
            { Console.WriteLine("Send a message failure..."); }
        }
    }
}

client


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

namespace AsyncClient
{
    class Program
    {
        private static IPEndPoint epServer;
        private static UdpClient local;

        static void Main(string[] args)
        {
            //设置服务器端IP和端口
            epServer = new IPEndPoint(IPAddress.Parse("192.168.1.140"), 11000);
            local = new UdpClient(9001);    //绑定本机IP和端口,9001
            while (true) {
                string strSend = Console.ReadLine();
                if (strSend == "exit") break;
                byte[] sendData = Encoding.ASCII.GetBytes(strSend);
                //开始异步发送,启动一个线程,该线程启动函数是:SendCallback,该函数中结束挂起的异步发送
                local.BeginSend(sendData, sendData.Length, epServer, new AsyncCallback(SendCallback), null);
                //开始异步接收启动一个线程,该线程启动函数是:ReceiveCallback,该函数中结束挂起的异步接收
                local.BeginReceive(new AsyncCallback(ReceiveCallback), null);
            }
        }

        private static void SendCallback(IAsyncResult iar)
        {
            int sendCount = local.EndSend(iar);
            if (sendCount == 0)
            { Console.WriteLine("Send a message failure..."); }
        }

        private static void ReceiveCallback(IAsyncResult iar)
        {
            byte[] receiveData = local.EndReceive(iar, ref epServer);
            Console.WriteLine("Server: {0}", Encoding.ASCII.GetString(receiveData));
        }
    }
}





  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值