0

我正在按照网站http://www.codeproject.com/Articles/463947/Working-with-Sockets-in-Csharp上的示例来构建套接字程序。

我只是想知道在下面的代码下acceptcallback 的替代品是什么。

Socket sListener;

SocketPermission sPermission = new SocketPermission(NetworkAccess.Accept,
       TransportType.Tcp, "", SocketPermission.AllPorts);

sListener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

IPHostEntry ipHost = Dns.GetHostEntry("");
IPAddress ipAddr = ipHost.AddressList[0];
IPEndPoint ipEndP = new IPEndPoint(ipAddr, 4510);

sListener.Bind(ipEndP);

sListener.Listen(10);

//error below this
AsyncCallback aCallback = new AsyncCallback(AcceptCallback);           
sListener.BeginAccept(aCallback, sListener);
....

我在网上搜索并尝试了以下方法,但似乎没有任何效果:

AsyncCallback aCallback = new AsyncCallback(AcceptCallback);           

AsyncCallback aCallback = new AsyncCallback(sListener.AcceptCallback);

AsyncCallback aCallback = new AsyncCallback(this.AcceptCallback);

AsyncCallback aCallback = new AsyncCallback(this.IAsyncResult);   

AsyncCallback aCallback = new AsyncCallback(aCallback);      

AsyncCallback aCallback = new AsyncCallback(sListener.aCallback);

AsyncCallback aCallback = new AsyncCallback(this.aCallback);

我正在查看的另一个参考是https://stackoverflow.com/questions/3722270/c-sharp-server-does-not-trigger-acceptcallback

这是我第一次在 C# 中做套接字。

4

1 回答 1

0

AcceptCallback 是您创建的一种方法,当异步操作完成时会调用该方法。

看看在以下位置找到的示例:

http://msdn.microsoft.com/en-us/library/system.asynccallback.aspx _

// Create the delegate that will process the results of the  
            // asynchronous request.
            AsyncCallback callBack = new AsyncCallback(ProcessDnsInformation);

// The following method is called when each asynchronous operation completes. 
        static void ProcessDnsInformation(IAsyncResult result)
        { ...
于 2013-07-09T00:30:08.607 回答