最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
WPF实现Socket异步通讯示例
时间:2022-06-25 08:42:49 编辑:袖梨 来源:一聚教程网
示例一
代码如下 | 复制代码 |
public class SocketHelper
|
三种方式实现异步socket
代码如下 | 复制代码 |
using System; namespace ExtensionTest public class NetWork private IPEndPoint endPoint; public NetWork() server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP); public void StartListening() public void StartListeningWithIterator() private void AcceptCallback(IAsyncResult asyncResult) byte[] buffer = new byte[256]; receiveSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveCallback, new State(receiveSocket, buffer)); // private void ReceiveCallback(IAsyncResult asyncResult) if (state != null) private void SendCallback(IAsyncResult asyncResult) private IEnumerator yield return 1; Socket receiveSocket = server.EndAccept(enumerator.DequeueAsyncResult()); byte[] buffer = new byte[256]; receiveSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, enumerator.End(), null); yield return 1; int receLength = receiveSocket.EndReceive(enumerator.DequeueAsyncResult()); if (receLength > 0) yield return 1; receiveSocket.EndSend(enumerator.DequeueAsyncResult()); private void StartAsync(Socket socket) byte[] buffer = new byte[256]; if (receLength > 0) };
private class State public State(Socket socket, byte[] bytes) |
相关文章
- 《原神》5.2卡池抽取建议 11-14
- 《原神》5.2版本新怪物介绍 11-14
- 《原神》希诺宁增伤触发方法 11-14
- 《原神》循音觅奇活动入口 11-14
- 《原神》循音觅奇兑换码获取方法 11-14
- 《原神》花羽会活动飞行技巧介绍 11-14