知识大全 C#异步Socket代码示例
Posted 知
篇首语:天赋如同自然花木,要用学习来修剪本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C#异步Socket代码示例相关的知识,希望对你有一定的参考价值。
C#异步Socket代码示例 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
下面的代码示例演示如何使用 SocketAsyncEventArgs 类在服务器和客户端之间发送数据和接收响应
服务器端代码
Serverusing System Collections Generic;using System Text;using System Net Sockets;using System Net;namespace SocketTest class Program static void Main(string[] args) Socket serverSk = new Socket(AddressFamily InterNeork SocketType Stream ProtocolType Tcp); serverSk Bind(new IPEndPoint(IPAddress Parse( ) )); serverSk Listen( ); SocketAsyncEventArgs AcceptSAE = new SocketAsyncEventArgs(); AcceptSAE Completed += new EventHandler<SocketAsyncEventArgs>(AcceptSAE_Completed); serverSk AcceptAsync(AcceptSAE); Console ReadLine(); static void AcceptSAE_Completed(object sender SocketAsyncEventArgs e) Socket serverSk = sender as Socket; if (e SocketError == SocketError Success) serverSk = e AcceptSocket; SocketAsyncEventArgs SendSAE = new SocketAsyncEventArgs(); byte[] data = System Text Encoding UTF GetBytes( OK Just Put IT! ); SendSAE SetBuffer(data data Length); SendSAE Completed += new EventHandler<SocketAsyncEventArgs>(SendSAE_Completed); SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs(); byte[] buffer = new byte[ ]; RecieveSAE SetBuffer(buffer buffer Length); RecieveSAE Completed += new EventHandler<SocketAsyncEventArgs>(RecieveSAE_Completed); serverSk ReceiveAsync(RecieveSAE); serverSk SendAsync(SendSAE); else Console WriteLine( 接受网络套接字连接请求失败!具体原因请自己调试! ); static void RecieveSAE_Completed(object sender SocketAsyncEventArgs e) Socket sk = sender as Socket; byte[] data = e Buffer; //注意这里 如何取关联到套接字的发送接受的缓冲区中的值 string msg = System Text Encoding UTF GetString(data); Console WriteLine( Message received: + msg); // sk DisconnectAsync();//你看看 该怎么做呢? static void SendSAE_Completed(object sender SocketAsyncEventArgs e) Socket sk = sender as Socket; if (e SocketError == SocketError Success) Console WriteLine( Send plete! ); byte[] data = e Buffer; //注意这里 如何取关联到套接字的发送接受的缓冲区中的值 string msg = System Text Encoding UTF GetString(data); Console WriteLine( What you sent: + msg);
客户端代码
Cilentusing System Collections Generic;using System Text;using System Net Sockets;using System Net;namespace SocketTestTwo class Program static void Main(string[] args) Socket clientSk = new Socket(AddressFamily InterNeork SocketType Stream ProtocolType Tcp); SocketAsyncEventArgs ConnectSAE = new SocketAsyncEventArgs(); ConnectSAE RemoteEndPoint = new IPEndPoint(IPAddress Parse( ) ); ConnectSAE Completed += new EventHandler<SocketAsyncEventArgs>(ConnectSAE_Completed); clientSk ConnectAsync(ConnectSAE); Console ReadLine(); static void ConnectSAE_Completed(object sender SocketAsyncEventArgs e) Socket clientSk = sender as Socket; if (e SocketError == SocketError Success && clientSk Connected) SocketAsyncEventArgs SendSAE = new SocketAsyncEventArgs(); byte[] data = System Text Encoding UTF GetBytes( I want put all funy things together! ); SendSAE SetBuffer(data data Length); SendSAE Completed += new EventHandler<SocketAsyncEventArgs>(SendSAE_Completed); SocketAsyncEventArgs RecieveSAE = new SocketAsyncEventArgs(); byte[] buffer = new byte[ ]; RecieveSAE SetBuffer(buffer buffer Length); RecieveSAE Completed += new EventHandler<SocketAsyncEventArgs>(RecieveSAE_Completed); //先调用异步接收 再调用异步发送 让你体验到异步明显非一般的感觉 clientSk ReceiveAsync(RecieveSAE); clientSk SendAsync(SendSAE); static void RecieveSAE_Completed(object sender SocketAsyncEventArgs e) Socket sk = sender as Socket; byte[] data = e Buffer; //注意这里 如何取关联到套接字的发送接受的缓冲区中的值 string msg = System Text Encoding UTF GetString(data); Console WriteLine( Message received: +msg); //sk DisconnectAsync();//你看看 该怎么做呢? static void SendSAE_Completed(object sender SocketAsyncEventArgs e) Socket sk = sender as Socket; if (e SocketError == SocketError Success) Console WriteLine( Send plete! );
调试运行
先调试运行服务器项目
再调试运行客户端项目
查看运行结果
cha138/Article/program/net/201311/12699相关参考
C#实现的多线程异步Socket数据包接收器 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 几天
C#实现的多线程异步Socket数据包接收器框架 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
知识大全 C# 实现的多线程异步Socket数据包接收器框架
C#实现的多线程异步Socket数据包接收器框架 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!
前台设计复制代码代码如下:<%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="pagingaspxcs"Inherits="五二一练习
运用异步输入输出流编写Socket进程通信 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 杨健(
JavaNIO(异步IO)Socket通信例子 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! /
C#和C++结构体Socket通信 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 最近在用C#做
C#调用WebService示例 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! C#调用WebS
JavaSocket通信读取相关信息代码 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! Java
C#委托的同步调用和异步调用 以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧! 对于C#委托我们谈的