下面的代码示例演示如何使用 SocketAsyncEventArgs 类在服务器和客户端之间发送数据和接收响应
服务器端代码
Server
using SystemCollectionsGeneric;
using SystemText;
using SystemNetSockets;
using SystemNet;
namespace SocketTest
{
class Program
{
static void Main(string[] args)
{
Socket serverSk = new Socket(AddressFamilyInterNetwork SocketTypeStream ProtocolTypeTcp);
serverSkBind(new IPEndPoint(IPAddressParse() ));
serverSkListen();
SocketAsyncEventArgs AcceptSAE = new SocketAsyncEventArgs();
AcceptSAECompleted += new EventHandler<SocketAsyncEventArgs>(AcceptSAE_Completed);
serverSkAcceptAsync(AcceptSAE);
ConsoleReadLine();
}
static void AcceptSAE_Completed(object sender SocketAsyncEventArgs e)
{
Socket serverSk = sender as Socket;
if (eSocketError == SocketErrorSuccess)
{
serverSk = eAcceptSocket;
SocketAsyncEventArgs SendSAE = new SocketAsyncEventArgs();
byte[] data = SystemTextEncodingUTFGetBytes(OKJust Put IT!);
SendSAESetBuffer(data dataLength);
SendSAECompleted += new EventHandler<SocketAsyncEventArgs>(SendSAE_Completed);
SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs();
byte[] buffer = new byte[];
RecieveSAESetBuffer(buffer bufferLength);
RecieveSAECompleted += new EventHandler<SocketAsyncEventArgs>(RecieveSAE_Completed);
serverSkReceiveAsync(RecieveSAE);
serverSkSendAsync(SendSAE);
}
else
ConsoleWriteLine(接受网络套接字连接请求失败!具体原因请自己调试!);
}
static void RecieveSAE_Completed(object sender SocketAsyncEventArgs e)
{
Socket sk = sender as Socket;
byte[] data = eBuffer; //注意这里如何取关联到套接字的发送接受的缓沖区中的值
string msg = SystemTextEncodingUTFGetString(data);
ConsoleWriteLine(Message received: + msg);
// skDisconnectAsync();//你看看 该怎么做呢?
}
static void SendSAE_Completed(object sender SocketAsyncEventArgs e)
{
Socket sk = sender as Socket;
if (eSocketError == SocketErrorSuccess)
{
ConsoleWriteLine(Send complete!);
byte[] data = eBuffer; //注意这里如何取关联到套接字的发送接受的缓沖区中的值
string msg = SystemTextEncodingUTFGetString(data);
ConsoleWriteLine(What you sent: + msg);
}
}
}
}
客户端代码
Cilent
using SystemCollectionsGeneric;
using SystemText;
using SystemNetSockets;
using SystemNet;
namespace SocketTestTwo
{
class Program
{
static void Main(string[] args)
{
Socket clientSk = new Socket(AddressFamilyInterNetwork SocketTypeStream ProtocolTypeTcp);
SocketAsyncEventArgs ConnectSAE = new SocketAsyncEventArgs();
ConnectSAERemoteEndPoint = new IPEndPoint(IPAddressParse() );
ConnectSAECompleted += new EventHandler<SocketAsyncEventArgs>(ConnectSAE_Completed);
clientSkConnectAsync(ConnectSAE);
ConsoleReadLine();
}
static void ConnectSAE_Completed(object sender SocketAsyncEventArgs e)
{
Socket clientSk = sender as Socket;
if (eSocketError == SocketErrorSuccess && clientSkConnected)
{
SocketAsyncEventArgs SendSAE = new SocketAsyncEventArgs();
byte[] data = SystemTextEncodingUTFGetBytes(I want put all funy things together!);
SendSAESetBuffer(data dataLength);
SendSAECompleted += new EventHandler<SocketAsyncEventArgs>(SendSAE_Completed);
SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs();
byte[] buffer = new byte[];
RecieveSAESetBuffer(buffer bufferLength);
RecieveSAECompleted += new EventHandler<SocketAsyncEventArgs>(RecieveSAE_Completed);
//先调用异步接收再调用异步发送让你体验到异步明显非一般的感觉
clientSkReceiveAsync(RecieveSAE);
clientSkSendAsync(SendSAE);
}
}
static void RecieveSAE_Completed(object sender SocketAsyncEventArgs e)
{
Socket sk = sender as Socket;
byte[] data = eBuffer; //注意这里如何取关联到套接字的发送接受的缓沖区中的值
string msg = SystemTextEncodingUTFGetString(data);
ConsoleWriteLine(Message received: +msg);
//skDisconnectAsync();//你看看 该怎么做呢?
}
static void SendSAE_Completed(object sender SocketAsyncEventArgs e)
{
Socket sk = sender as Socket;
if (eSocketError == SocketErrorSuccess)
{
ConsoleWriteLine(Send complete!);
}
}
}
}
调试运行
先调试运行服务器项目
再调试运行客户端项目
查看运行结果