免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Socket異步編程-之服務(wù)器端. - 業(yè)精于勤荒于嬉,形成于思------->毀于隨 - 博客園
 1 using System;
 2 using System.Text;
 3 using System.Threading;
 4 using System.Net.Sockets;
 5 using System.Net;
 6 namespace asyncSocketServer {
 7 
 8     class SocketListener {
 9         System.Threading.ManualResetEvent allDone=new ManualResetEvent(false);
10         [STAThread]
11         static void Main(string[] args) {
12             SocketListener server=new SocketListener();
13             server.StartListening();
14         }
15 
16         public void StartListening() {
17             IPHostEntry ipHostInfo=Dns.Resolve(Dns.GetHostName());
18             IPEndPoint localEP=new IPEndPoint(ipHostInfo.AddressList[0],11000);
19             Console.WriteLine("Local address and pot:{0}",localEP.ToString());
20             Socket listener=new Socket(AddressFamily.InterNetwork,
21                 SocketType.Stream,ProtocolType.Tcp);
22             try {
23                 listener.Bind(localEP);
24                 listener.Listen(10);
25                 while (true) {
26                     allDone.Reset();
27                     Console.WriteLine("Waiting for a connection
");
28                     listener.BeginAccept(new AsyncCallback(acceptCallback),listener);
29                     allDone.WaitOne();            //阻塞主線程
30                 }
31             }
32             catch(Exception ex) {
33                 Console.WriteLine(ex.ToString());
34             }
35             Console.WriteLine("Closing the Listener
.
");
36         }
37 
38         public void acceptCallback(IAsyncResult ar) {
39             Socket listener=(Socket)ar.AsyncState;
40             Socket handler=listener.EndAccept(ar);
41             //設(shè)置主線程繼續(xù)
42             allDone.Set();
43             StateObject state=new StateObject();
44             state.workSocket=handler;
45             handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
46                 new AsyncCallback(readCallback),state);
47         }
48 
49         public void readCallback(IAsyncResult ar) {
50             StateObject state=(StateObject)ar.AsyncState;
51             Socket handler=state.workSocket;
52             int read=handler.EndReceive(ar);
53             if (read>0) {
54                 state.sb.Append(Encoding.ASCII.GetString(state.buffer,0,read));
55                 handler.BeginReceive(state.buffer,0,StateObject.BufferSize,0,
56                     new AsyncCallback(readCallback),state);
57             }
58             else {
59                 if (state.sb.Length>1) {
60                     string content=state.sb.ToString();
61                     Console.WriteLine("Read {0} bytes from socket.\n Data:{1}",
62                         content.Length,content);
63                 }
64                 handler.Close();
65             }
66         }
67     }
68
69     public class StateObject {
70         public Socket workSocket=null;
71         public const int BufferSize=1024;
72         public byte[] buffer=new byte[BufferSize];
73         public StringBuilder sb=new StringBuilder();
74     }
75 }
76 
本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
c#(Socket)異步套接字代碼示例
VB.NET環(huán)境下的Socket編程舉例
C# Socket 網(wǎng)絡(luò)編程
Socket 同步和異步模式
基于C#的內(nèi)網(wǎng)穿透學(xué)習(xí)筆記(附源碼)
C# Socket 入門3 UPD
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服