亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > C# > 正文

c#(Socket)異步套接字代碼示例

2019-10-29 21:48:20
字體:
來源:轉載
供稿:網友
異步客戶端套接字示例   


下面的示例程序創建一個連接到服務器的客戶端。該客戶端是用異步套接字生成的,因此在等待服務器返回響應時不掛起客戶端應用程序的執行。該應用程序將字符串發送到服務器,然后在控制臺顯示該服務器返回的字符串。 

C# 

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Threading; 
using System.Text; 
// State object for receiving data from remote device. 
public class StateObject { 
// Client socket. 
public Socket workSocket = null; 
// Size of receive buffer. 
public const int BufferSize = 256; 
// Receive buffer. 
public byte[] buffer = new byte[BufferSize]; 
// Received data string. 
public StringBuilder sb = new StringBuilder(); 

public class AsynchronousClient { 
// The port number for the remote device. 
private const int port = 11000; 
// ManualResetEvent instances signal completion. 
private static ManualResetEvent connectDone = 
new ManualResetEvent(false); 
private static ManualResetEvent sendDone = 
new ManualResetEvent(false); 
private static ManualResetEvent receiveDone = 
new ManualResetEvent(false); 
// The response from the remote device. 
private static String response = String.Empty; 
private static void StartClient() { 
// Connect to a remote device. 
try { 
// Establish the remote endpoint for the socket. 
// The name of the  
// remote device is "host.contoso.com". 
IPHostEntry ipHostInfo = Dns.Resolve("host.contoso.com"); 
IPAddress ipAddress = ipHostInfo.AddressList[0]; 
IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); 
// Create a TCP/IP socket. 
Socket client = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream, ProtocolType.Tcp); 
// Connect to the remote endpoint. 
client.BeginConnect( remoteEP, 
new AsyncCallback(ConnectCallback), client); 
connectDone.WaitOne(); 
// Send test data to the remote device. 
Send(client,"This is a test<EOF>"); 
sendDone.WaitOne(); 
// Receive the response from the remote device. 
Receive(client); 
receiveDone.WaitOne(); 
// Write the response to the console. 
Console.WriteLine("Response received : {0}", response); 
// Release the socket. 
client.Shutdown(SocketShutdown.Both); 
client.Close(); 
} catch (Exception e) { 
Console.WriteLine(e.ToString()); 


private static void ConnectCallback(IAsyncResult ar) { 
try { 
// Retrieve the socket from the state object. 
Socket client = (Socket) ar.AsyncState; 
// Complete the connection. 
client.EndConnect(ar); 
Console.WriteLine("Socket connected to {0}", 
client.RemoteEndPoint.ToString()); 
// Signal that the connection has been made. 
connectDone.Set(); 
} catch (Exception e) { 
Console.WriteLine(e.ToString()); 


private static void Receive(Socket client) { 
try { 
// Create the state object. 
StateObject state = new StateObject(); 
state.workSocket = client; 
// Begin receiving the data from the remote device. 
client.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, 
new AsyncCallback(ReceiveCallback), state); 
} catch (Exception e) { 
Console.WriteLine(e.ToString()); 


private static void ReceiveCallback( IAsyncResult ar ) { 
try { 
// Retrieve the state object and the client socket  
// from the asynchronous state object. 
StateObject state = (StateObject) ar.AsyncState; 
Socket client = state.workSocket; 
// Read data from the remote device. 
int bytesRead = client.EndReceive(ar); 
if (bytesRead > 0) { 
// There might be more data, so store the data received so far. 
state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,bytesRead)); 
// Get the rest of the data. 
client.BeginReceive(state.buffer,0,StateObject.BufferSize,0, 
new AsyncCallback(ReceiveCallback), state); 
} else { 
// All the data has arrived; put it in response. 
if (state.sb.Length > 1) { 
response = state.sb.ToString(); 

// Signal that all bytes have been received. 
receiveDone.Set(); 

} catch (Exception e) { 
Console.WriteLine(e.ToString()); 


private static void Send(Socket client, String data) { 
// Convert the string data to byte data using ASCII encoding. 
byte[] byteData = Encoding.ASCII.GetBytes(data); 
// Begin sending the data to the remote device. 
client.BeginSend(byteData, 0, byteData.Length, 0, 
new AsyncCallback(SendCallback), client); 

private static void SendCallback(IAsyncResult ar) { 
try { 
// Retrieve the socket from the state object. 
Socket client = (Socket) ar.AsyncState; 
// Complete sending the data to the remote device. 
int bytesSent = client.EndSend(ar); 
Console.WriteLine("Sent {0} bytes to server.", bytesSent); 
// Signal that all bytes have been sent. 
sendDone.Set(); 
} catch (Exception e) { 
Console.WriteLine(e.ToString()); 


public static int Main(String[] args) { 
StartClient(); 
return 0; 


異步服務器套接字示例 下面的示例程序創建一個接收來自客戶端的連接請求的服務器。該服務器是用異步套接字生成的, 
因此在等待來自客戶端的連接時不掛起服務器應用程序的執行。該應用程序接收來自客戶端的字符串, 
在控制臺顯示該字符串,然后將該字符串回顯到客戶端。來自客戶端的字符串必須包含字符串“<EOF>”, 
以發出表示消息結尾的信號。 



  




C# 
 復制代碼 

using System; 
using System.Net; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 
// State object for reading client data asynchronously 
public class StateObject { 
// Client  socket. 
public Socket workSocket = null; 
// Size of receive buffer. 
public const int BufferSize = 1024; 
// Receive buffer. 
public byte[] buffer = new byte[BufferSize]; 
// Received data string. 
public StringBuilder sb = new StringBuilder(); 

public class AsynchronousSocketListener { 
// Thread signal. 
public static ManualResetEvent allDone = new ManualResetEvent(false); 
public AsynchronousSocketListener() { 

public static void StartListening() { 
// Data buffer for incoming data. 
byte[] bytes = new Byte[1024]; 
// Establish the local endpoint for the socket. 
// The DNS name of the computer 
// running the listener is "host.contoso.com". 
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName()); 
IPAddress ipAddress = ipHostInfo.AddressList[0]; 
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000); 
// Create a TCP/IP socket. 
Socket listener = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream, ProtocolType.Tcp ); 
// Bind the socket to the local endpoint and listen for incoming connections. 
try { 
listener.Bind(localEndPoint); 
listener.Listen(100); 
while (true) { 
// Set the event to nonsignaled state. 
allDone.Reset(); 
// Start an asynchronous socket to listen for connections. 
Console.WriteLine("Waiting for a connection..."); 
listener.BeginAccept( 
new AsyncCallback(AcceptCallback), 
listener ); 
// Wait until a connection is made before continuing. 
allDone.WaitOne(); 

} catch (Exception e) { 
Console.WriteLine(e.ToString()); 

Console.WriteLine("/nPress ENTER to continue..."); 
Console.Read(); 

public static void AcceptCallback(IAsyncResult ar) { 
// Signal the main thread to continue. 
allDone.Set(); 
// Get the socket that handles the client request. 
Socket listener = (Socket) ar.AsyncState; 
Socket handler = listener.EndAccept(ar); 
// Create the state object. 
StateObject state = new StateObject(); 
state.workSocket = handler; 
handler.BeginReceive( state.buffer, 0, StateObject.BufferSize, 0, 
new AsyncCallback(ReadCallback), state); 

public static void ReadCallback(IAsyncResult ar) { 
String content = String.Empty; 
// Retrieve the state object and the handler socket 
// from the asynchronous state object. 
StateObject state = (StateObject) ar.AsyncState; 
Socket handler = state.workSocket; 
// Read data from the client socket.  
int bytesRead = handler.EndReceive(ar); 
if (bytesRead > 0) { 
// There  might be more data, so store the data received so far. 
state.sb.Append(Encoding.ASCII.GetString( 
state.buffer,0,bytesRead)); 
// Check for end-of-file tag. If it is not there, read  
// more data. 
content = state.sb.ToString(); 
if (content.IndexOf("<EOF>") > -1) { 
// All the data has been read from the  
// client. Display it on the console. 
Console.WriteLine("Read {0} bytes from socket. /n Data : {1}", 
content.Length, content ); 
// Echo the data back to the client. 
Send(handler, content); 
} else { 
// Not all data received. Get more. 
handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, 
new AsyncCallback(ReadCallback), state); 



private static void Send(Socket handler, String data) { 
// Convert the string data to byte data using ASCII encoding. 
byte[] byteData = Encoding.ASCII.GetBytes(data); 
// Begin sending the data to the remote device. 
handler.BeginSend(byteData, 0, byteData.Length, 0, 
new AsyncCallback(SendCallback), handler); 

private static void SendCallback(IAsyncResult ar) { 
try { 
// Retrieve the socket from the state object. 
Socket handler = (Socket) ar.AsyncState; 
// Complete sending the data to the remote device. 
int bytesSent = handler.EndSend(ar); 
Console.WriteLine("Sent {0} bytes to client.", bytesSent); 
handler.Shutdown(SocketShutdown.Both); 
handler.Close(); 
} catch (Exception e) { 
Console.WriteLine(e.ToString()); 


public static int Main(String[] args) { 
StartListening(); 
return 0; 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
夜夜嗨av色一区二区不卡| 欧美色道久久88综合亚洲精品| 国产成人精品一区二区| 美乳少妇欧美精品| 97在线视频免费观看| 国产精品久久久久免费a∨大胸| 欧美日韩国产精品一区二区不卡中文| 国产99久久久欧美黑人| 久久亚洲欧美日韩精品专区| 日韩中文视频免费在线观看| 国产97在线观看| www.日韩视频| 欧美日韩ab片| 夜夜嗨av色一区二区不卡| 精品一区二区三区四区在线| 久久香蕉国产线看观看网| 亚洲精品久久久久久久久| zzijzzij亚洲日本成熟少妇| 亚洲美女福利视频网站| 国产精品久久久久久久久男| 亚洲国产精品久久久久久| 成人在线国产精品| 91日本在线视频| 国产精品黄视频| 色噜噜国产精品视频一区二区| 欧美激情一级二级| 国产精品精品久久久久久| 2020久久国产精品| 91欧美精品成人综合在线观看| 日本一区二区不卡| 97涩涩爰在线观看亚洲| 欧美另类交人妖| 日韩精品在线私人| 亚洲国产欧美一区二区三区久久| 久久精品99久久久香蕉| 久久久久久亚洲精品中文字幕| 97视频在线观看免费| 日本中文字幕成人| 国内偷自视频区视频综合| 日韩欧美国产成人| 欧美激情一级欧美精品| 久久99精品久久久久久噜噜| 91久久精品国产| 欧美巨大黑人极品精男| 国产91热爆ts人妖在线| 亚洲综合精品伊人久久| 欧美在线观看网站| 欧美激情二区三区| 欧美激情一区二区三级高清视频| 国产成人精品一区二区三区| 日韩av免费在线观看| 亚洲国产成人在线视频| 2019中文字幕在线观看| 九九热精品在线| 亚洲国产精品嫩草影院久久| 日韩欧中文字幕| 久久久久久网址| 欧美性视频网站| 国产精品稀缺呦系列在线| 欧美激情精品久久久久久久变态| 欧美怡春院一区二区三区| 亚洲国产99精品国自产| 91tv亚洲精品香蕉国产一区7ujn| 欧美疯狂性受xxxxx另类| 精品视频久久久久久久| 日韩高清免费在线| 国产午夜精品麻豆| 亚洲黄页视频免费观看| 国产欧美日韩免费看aⅴ视频| 成人免费福利视频| www.欧美免费| 亚洲成色777777在线观看影院| 亚洲免费福利视频| 九九热这里只有精品免费看| 国产精品成人av性教育| 北条麻妃99精品青青久久| 欧美自拍视频在线观看| 成人免费午夜电影| 日韩精品免费综合视频在线播放| 久久av.com| 亚洲欧美激情在线视频| 久久国产精品影视| 日韩免费电影在线观看| 中文字幕视频一区二区在线有码| 爽爽爽爽爽爽爽成人免费观看| 黄色一区二区在线| 国产精品自产拍在线观| 欧美性视频在线| 精品久久在线播放| 久久免费视频这里只有精品| 久久精品国产亚洲7777| 国产精品老女人精品视频| 高清亚洲成在人网站天堂| 亚州欧美日韩中文视频| 亚洲精品视频免费| 欧美成人高清视频| 最近中文字幕日韩精品| 91亚洲国产成人精品性色| 亚洲成人a**站| 亚洲福利视频二区| 国产一区二区久久精品| 欧美性jizz18性欧美| 国产精品av在线| 久久天天躁狠狠躁夜夜av| 国产精品偷伦免费视频观看的| 热99精品只有里视频精品| 一本一本久久a久久精品综合小说| 国产精品久久久久久久久久小说| 亚洲欧美激情精品一区二区| 一区二区三区亚洲| 国产在线播放91| 久久久综合av| 91免费国产网站| 一本色道久久88亚洲综合88| 日韩av大片在线| 亚洲网站视频福利| 亚洲香蕉成视频在线观看| 日韩在线观看网址| 欧美中文字幕视频| 日韩av免费在线| 亚洲色图15p| 亚洲欧美一区二区三区久久| 亚洲色图18p| 国内精品久久久久伊人av| 亚洲天堂av在线免费观看| 亚洲美女精品久久| 性欧美激情精品| 久久久久久美女| 欧美激情在线狂野欧美精品| 亚洲精品欧美日韩| 久久久久国产精品免费网站| 国产精品一区二区三区久久久| 欧美日韩亚洲网| 国产成人精品视| 日韩毛片在线看| 亚洲精品720p| 欧美午夜丰满在线18影院| 国产日韩中文字幕在线| 一区二区av在线| 国产精品美女免费视频| 日韩在线高清视频| 久久久999国产精品| 在线播放日韩精品| 国产精品三级美女白浆呻吟| 国产日韩欧美成人| 午夜精品久久久久久久99黑人| 久久全球大尺度高清视频| 色综合久久中文字幕综合网小说| 国产日产欧美a一级在线| 亚洲免费视频网站| 久久久久久久久久久91| 91精品在线影院| 久久视频在线视频| 欧美日韩一区二区在线| 欧美精品电影免费在线观看| 国产91对白在线播放| 国产精品av电影| 亚洲国产精品久久久| 91中文字幕在线| 国外成人免费在线播放| 热久久这里只有| 中文字幕亚洲欧美日韩在线不卡| 91tv亚洲精品香蕉国产一区7ujn| 在线观看视频亚洲|