熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#異步Socket代碼示例

2022-06-13   來源: .NET編程 

  下面的代碼示例演示如何使用 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!);
}
}


}
}

   調試運行

  先調試運行服務器項目

  調試運行服務器項目 

  再調試運行客戶端項目

  調試運行客戶端項目

   查看運行結果

  查看運行結果


From:http://tw.wingwit.com/Article/program/net/201311/12699.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.