c#|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 = https://www.it610.com/article/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 = https://www.it610.com/article/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 = https://www.it610.com/article/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 = https://www.it610.com/article/local.EndReceive(iar, ref epServer);
Console.WriteLine("Server: {0}", Encoding.ASCII.GetString(receiveData));
}
}
}
【c#|c# 使用UDPClient实现异步通信】
推荐阅读
- 由浅入深理解AOP
- 【译】20个更有效地使用谷歌搜索的技巧
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- mybatisplus如何在xml的连表查询中使用queryWrapper
- MybatisPlus|MybatisPlus LambdaQueryWrapper使用int默认值的坑及解决
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入