首页 > 编程语言 >C# Socket异步通信

C# Socket异步通信

时间:2023-05-27 23:55:51浏览次数:61  
标签:异步 Socket C# listener state handler new socket

转载

https://www.cnblogs.com/llllll/archive/2009/05/13/1455703.html

 

服务器端

TCPServer 

1、使用的通讯通道:socket

2、用到的基本功能:

Bind,

Listen,

BeginAccept

EndAccept

BeginReceive  

EndReceive

3、函数参数说明

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

 新建socket所使用的参数均为系统预定义的量,直接选取使用。

listener.Bind(localEndPoint);

localEndPoint 表示一个定义完整的终端,包括IP和端口信息。

//new IPEndPoint(IPAddress,port)

//IPAdress.Parse("192.168.1.3")

listener.Listen(100);

监听

    listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

  AsyncCallback(AcceptCallback),一旦连接上后的回调函数为AcceptCallback。当系统调用这个函数时,自动赋予的输入参数为IAsyncResoult类型变量ar。
   listener,连接行为的容器。

Socket handler = listener.EndAccept(ar);

完成连接,返回此时的socket通道。

handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);

接收的字节,0,字节长度,0,接收时调用的回调函数,接收行为的容器。

========

容器的结构类型为:


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();
}

容器至少为一个socket类型。

===============

  // Read data from the client socket. 
        int bytesRead = handler.EndReceive(ar);

完成一次连接。数据存储在state.buffer里,bytesRead为读取的长度。

handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);

发送数据byteData,回调函数SendCallback。容器handler

int bytesSent = handler.EndSend(ar);

发送完毕,bytesSent发送字节数。

4 程序结构

主程序:



        byte[] bytes = new Byte[1024];

        IPAddress ipAddress = IPAddress.Parse("192.168.1.104");
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);

        // 生成一个TCP的socket
        Socket listener = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream, ProtocolType.Tcp);

        listener.Bind(localEndPoint);
        listener.Listen(100);

            while (true)
            {
                // Set the event to nonsignaled state.
                allDone.Reset();

                //开启异步监听socket
                Console.WriteLine("Waiting for a connection");
                listener.BeginAccept(
                    new AsyncCallback(AcceptCallback),
                    listener);

                // 让程序等待,直到连接任务完成。在AcceptCallback里的适当位置放置allDone.Set()语句.
                allDone.WaitOne();
           }
    Console.WriteLine("\nPress ENTER to continue");
    Console.Read();

连接行为回调函数AcceptCallback:


    public static void AcceptCallback(IAsyncResult ar)
    {
        //添加此命令,让主线程继续.
        allDone.Set();

        // 获取客户请求的socket
        Socket listener = (Socket)ar.AsyncState;
        Socket handler = listener.EndAccept(ar);

        // 造一个容器,并用于接收命令.
        StateObject state = new StateObject();
        state.workSocket = handler;
        handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
            new AsyncCallback(ReadCallback), state);
    }

读取行为的回调函数ReadCallback:



    public static void ReadCallback(IAsyncResult ar)
    {
        String content = String.Empty;

        // 从异步state对象中获取state和socket对象.
        StateObject state = (StateObject)ar.AsyncState;
        Socket handler = state.workSocket;

        // 从客户socket读取数据. 
        int bytesRead = handler.EndReceive(ar);

        if (bytesRead > 0)
        {
            // 如果接收到数据,则存起来
            state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

            // 检查是否有结束标记,如果没有则继续读取
            content = state.sb.ToString();
            if (content.IndexOf("<EOF>") > -1)
            {
                //所有数据读取完毕.
                Console.WriteLine("Read {0} bytes from socket. \n Data : {1}",
                    content.Length, content);
                // 给客户端响应.
                Send(handler, content);
            }
            else
            {
                // 接收未完成,继续接收.
                handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                new AsyncCallback(ReadCallback), state);
            }
        }
    }

发送消息给客户端:


private static void Send(Socket handler, String data)
    {
        // 消息格式转换.
        byte[] byteData = Encoding.ASCII.GetBytes(data);

        // 开始发送数据给远程目标.
        handler.BeginSend(byteData, 0, byteData.Length, 0,
            new AsyncCallback(SendCallback), handler);
    }

private static void SendCallback(IAsyncResult ar)
    {
     
            // 从state对象获取socket.
            Socket handler = (Socket)ar.AsyncState;

            //完成数据发送
            int bytesSent = handler.EndSend(ar);
            Console.WriteLine("Sent {0} bytes to client.", bytesSent);

            handler.Shutdown(SocketShutdown.Both);
            handler.Close();

    }

 

在各种行为的回调函数中,所对应的socket都从输入参数的AsyncState属性获得。使用(Socket)或者(StateObject)进行强制转换。BeginReceive函数使用的容器为state,因为它需要存放传送的数据。

而其余接收或发送函数的容器为socket也可。

完整代码

 


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 = IPAddress.Parse("192.168.1.104");
        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;
    }
}

标签:异步,Socket,C#,listener,state,handler,new,socket
From: https://www.cnblogs.com/cxxtreasure/p/17437608.html

相关文章

  • Python协程:从yield/send到yield from再到async/await
    Python中的协程大概经历了如下三个阶段:最初的生成器变形yield/send引入@asyncio.coroutine和yieldfrom在最近的Python3.5版本中引入async/await关键字一、生成器变形yield/send普通函数如果出现了yield关键字,那么该函数就不再是普通函数,而是一个生成器。importrandom......
  • coreutils test 源码分析
    Test的代码中主要解析如下语法,当然使用的时候也可以参考如下语法进行语句的编写/*test(1)acceptsthefollowinggrammar:oexpr::=aexpr|aexpr"-o"oexpr;aexpr::=nexpr|nexpr"-a"aexpr;nexpr::=primary|"!"primaryprimary::=un......
  • Chatbase:用文档或网站创建机器人
    面对铺天盖地的OpenAIChatGPT,有人走马观花,有人却利用它赚了不少。有这样一个大学生,前脚还在Meta和Tesla等大厂实习,半年后,其创办的聊天机器人公司就已经稳定月收6.4万美元(约合45万人民币),而且自首次上线以来,业务流量从未下滑缩水。为了满足巨大需求,他还雇用了两......
  • Webpack and Babel — What are they, and how to use them with React
    摘抄自:https://medium.com/@agzuniverse/webpack-and-babel-what-are-they-and-how-to-use-them-with-react-5807afc82ca8WebpackandBabel—Toolswecan’tcodewithoutWe’llbeconfiguringbothoftheseforourReactproject,sofirsthere’saquickexplanation......
  • CMake构建指南:如何提高C/C++项目的可维护性
    如果您是一位C/C++开发人员,那么您一定知道在编写和维护大型项目时所面临的挑战。这些项目通常包含大量的源代码、库和依赖项,需要耗费大量的时间和精力才能构建和维护。在这种情况下,使用自动化工具可以大大减轻您的负担,提高项目的可维护性。CMake就是这样一款工具,它可以自动化C/C+......
  • OpenCASCADE曲面求交之迭代法
    OpenCASCADE曲面求交之迭代法[email protected]朱心雄等著《自由曲线曲面造型技术》书中对曲面求交的迭代法有详细介绍,其中关于曲面迭代求交的原理介绍如下:为求得两个曲面精确的交点,Newton-Raphson迭代法得到广泛应用,该法的优点为计算精度高,速度快,在初值选择比较合......
  • Android 服务Service详解
    Android服务(Service)是一种在后台运行的组件,它可以在不与用户交互的情况下执行长时间运行的操作。服务通常用于在后台播放音乐、下载数据、执行网络操作等。服务的特点如下:1.服务是一种后台运行的组件,可以在不与用户交互的情况下执行长时间运行的操作。2.服务可以在应用程序的......
  • MyCat19——搭建MyCat高可用集群
    1HAProxy单点故障在上一篇文章里,我们在一台机器上安装了HAProxy,实现了MyCat服务的集群。但是这样的架构中,只有一个HAProxy服务,一旦这个服务发生了宕机,集群将不可用,这就是所谓的单点故障。那么怎么进一步提高HAProxy的高可用,从而解决单点故障的问题呢?通过Keepalived可以实现。2解......
  • 允许Traceroute探测漏洞修复
    vim/etc/sysconfig/iptables-AINPUT-picmp-micmp--icmp-typetime-exceeded-jDROP-AOUTPUT-picmp-micmp--icmp-typetime-exceeded-jDROP重启iptables服务:systemctlrestartiptables检查新添加的规则是否有效,检查命令:iptables-L-n......
  • Anaconda正确安装pytorch正确步骤
    前提:Anaconda安装的10个坑1没有系统环境变量(有的安装包没有系统环境变量,勾选安装,需要自己配置环境变量,否则会后面会让你重新安装)2安装pytorch前,要condaactivatemyenv//激活环境,不然安装默认路径,用不了,白安装了 第一步一劳永逸,设置镜像源pipconfigsetglobal.index-......