同步TCP编程传送门:https://www.yqyzf.love/?post=5
异步TCP编程基本知识:
1.什么是异步TCP编程?
就是在发送或接受消息时,不需要等待,可以先做其他事情,等发送或接受信息完毕之后通过
回调函数执行后面需要进行的操作。
2.异步TCP编程需要掌握的4个方法,一个委托和一个接口。
四个方法:
public IAsyncResult BeginAcceptTcpClient(AsyncCallBack callback,object state);
开启一个异步操作,开始接入连接请求。
public TcpClient EndAcceptTcpClient(IAsyncResult ar);
得到通知,完成接入连接请求,并返回一个TcpClient对象,用于与远程客户端对话。(必须在回调函数中使用)
public IAsyncResult BeginConnect(IPAddress ip,int port ,AsyncCallBack callback,object state);
开启一个异步操作,开始请求与服务器连接
public TcpClient EndConnect(IAsyncResult ar);
得到通知,完成与服务器的连接。(必须在回调函数中使用)
一个委托:AsyncCallBack
AsyncCallBack是一个委托,用于实现回调方法的委托(因为方法不可作为参数),当异步操作完成后
会自动调用回调函数。
一个接口:
我们注意到,开启异步操作的俩个方法的返回值是IAsyncResult.这是一个接口,保存异步操作信息的接
口,有以下属性:
AsyncState:包含异步操作状态信息。
AsyncWaitHandle:可以在异步操作完成前阻止程序运行。
CompletedSynchronously:可以设置在哪个线程上完成。
IsCompleted:指示异步操作是否完成。
3.如何进行异步编程?
服务端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Diagnostics;
namespace MyAsyncServer
{
class MyServer
{
//声明TCP异步通讯服务器类需要的一些变量
private IPAddress localAddress;
private int port;
private TcpListener listener;
private AsyncCallback acceptCallBack;
//记录连入的客户端数目
private int clientNum;
//服务器构造函数
public MyServer()
{
localAddress = IPAddress.Parse("192.168.0.200");
port = 50000;
listener = new TcpListener(localAddress,port);
acceptCallBack = new AsyncCallback(AcceptCallBack);
clientNum = 0;
}
//开启异步接入客户端的回调函数
private void AcceptCallBack(IAsyncResult ar)
{
TcpListener listener = (TcpListener)ar.AsyncState;
TcpClient client = listener.EndAcceptTcpClient(ar);
clientNum++;
Console.WriteLine("当前连入客户端数目:" + clientNum.ToString());
RecMsg(client);
Console.WriteLine("服务器回应消息:抱歉,我没有苹果");
SendMsg(client, "抱歉,我没有苹果");
}
//用于启动服务器
public void Start()
{
listener.Start();
Console.WriteLine("服务器启动\n等待客户端接入");
while (true)
{
try
{
//开始异步操作,接受远程客户端连入
IAsyncResult ir = listener.BeginAcceptTcpClient(AcceptCallBack, listener);
while (ir.IsCompleted != true)
{
//通过轮询,得知当前异步操作是否结束,没有结束的话,会一直陷入该循环,可以在该循环处理一些其他操作。
// Console.WriteLine("这是异步操作,因此我可以边等边玩手机了");
Thread.Sleep(10);
}
}
catch (Exception e)
{
DebugException(e.Message[0].ToString());
}
}
}
//处理异常的方法
public void DebugException(string log)
{
Console.WriteLine("出现异常情况");
Console.WriteLine("原因:" + log);
}
/// <summary>
/// 用于服务器发送消息
/// </summary>
/// <param name="client"></param>
/// <param name="data"></param>
public void SendMsg(TcpClient client,string data )
{
byte[] msgLength = new byte[2];
byte[] buff = Encoding.UTF8.GetBytes(data);
int length = buff.Length;
msgLength = BitConverter.GetBytes(length);
NetworkStream ns = client.GetStream();
//发送俩次消息,第一次发送消息长度,第二次发送消息
ns.BeginWrite(msgLength, 0, 2, new AsyncCallback(AysncSendMsg), ns);
ns.BeginWrite(buff, 0, length, new AsyncCallback(AysncSendMsg), ns);
}
/// <summary>
/// 用于异步发送消息的回调函数
/// </summary>
/// <param name="ar"></param>
private void AysncSendMsg(IAsyncResult ar)
{
NetworkStream ns = (NetworkStream)ar.AsyncState;
ns.EndWrite(ar);
}
/// <summary>
/// 接收信息的方法
/// </summary>
/// <param name="client"></param>
public void RecMsg(TcpClient client)
{
byte[] msgLength = new byte[2];
byte[] buff;
NetworkStream ns = client.GetStream();
//接受消息长度
IAsyncResult ir1 = ns.BeginRead(msgLength, 0, msgLength.Length, new AsyncCallback(AysncRecMsg), ns);
while (ir1.IsCompleted == false)
{
Thread.Sleep(10);
}
int length=BitConverter.ToInt16(msgLength,0);
buff = new byte[length];
//接受消息
IAsyncResult ir3 = ns.BeginRead(buff, 0, length, new AsyncCallback(AysncRecMsg), ns);
while (ir3.IsCompleted == false)
{
Thread.Sleep(10);
}
string message = Encoding.UTF8.GetString(buff).Trim();
Console.WriteLine("收到来自客户端{0}的消息:{1}", client.Client.RemoteEndPoint, message);
}
/// <summary>
/// 用于异步接受信息的回调函数
/// </summary>
/// <param name="ar"></param>
private void AysncRecMsg(IAsyncResult ar)
{
((NetworkStream)ar.AsyncState).EndRead(ar);
}
}
class Program
{
static void Main(string[] args)
{
//初始化一个服务器对象,启动服务器。
MyServer server = new MyServer();
server.Start();
}
}
}
客户端代码:客户端的代码基本和服务端一致。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace Client
{
class Program
{
static void Main(string[] args)
{
IPAddress ip = IPAddress.Parse("192.168.0.200");
int point = 50000;
IPEndPoint ipEndPoint = new IPEndPoint(ip, point);
TcpClient client = new TcpClient();
Console.WriteLine("尝试连接服务器");
IAsyncResult ir = client.BeginConnect(ip, point, new AsyncCallback(ConnectedCallBack), client);
Console.ReadLine();
}
private static void ConnectedCallBack(IAsyncResult ar)
{
TcpClient client = (TcpClient)ar.AsyncState;
client.EndConnect(ar);
Console.WriteLine("连接服务器成功");
Console.WriteLine("给服务器发送信息:服务器,我想要一个苹果。");
SendMsg(client, "服务器,我想要一个苹果。");
RecMsg(client);
}
public static void SendMsg(TcpClient client, string data)
{
byte[] msgLength = new byte[2];
byte[] buff = Encoding.UTF8.GetBytes(data);
int length = buff.Length;
msgLength = BitConverter.GetBytes(length);
NetworkStream ns = client.GetStream();
ns.BeginWrite(msgLength, 0, 2, new AsyncCallback(AysncSendMsg), ns);
ns.BeginWrite(buff, 0, length, new AsyncCallback(AysncSendMsg), ns);
}
private static void AysncSendMsg(IAsyncResult ar)
{
NetworkStream ns = (NetworkStream)ar.AsyncState;
ns.EndWrite(ar);
}
public static void RecMsg(TcpClient client)
{
byte[] msgLength = new byte[2];
byte[] buff;
NetworkStream ns = client.GetStream();
IAsyncResult ir1 = ns.BeginRead(msgLength, 0, msgLength.Length, new AsyncCallback(AysncRecMsg), ns);
while (ir1.IsCompleted == false)
{
Thread.Sleep(10);
}
int length = BitConverter.ToInt16(msgLength, 0);
buff = new byte[length];
IAsyncResult ir3 = ns.BeginRead(buff, 0, length, new AsyncCallback(AysncRecMsg), ns);
while (ir3.IsCompleted == false)
{
Thread.Sleep(10);
}
string message = Encoding.UTF8.GetString(buff).Trim();
Console.WriteLine("收到来自服务器{0}的消息:{1}", client.Client.RemoteEndPoint, message);
}
private static void AysncRecMsg(IAsyncResult ar)
{
((NetworkStream)ar.AsyncState).EndRead(ar);
}
}
}
运行结果:
更多内容访问那位先生