本文转载自:http://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("") > -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("") > -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;
    }
}

(转)C# Socket异步通信的更多相关文章

  1. socket异步通信-如何设置成非阻塞模式、非阻塞模式下判断connect成功(失败)、判断recv/recvfrom成功(失败)、判断send/sendto

    socket异步通信-如何设置成非阻塞模式.非阻塞模式下判断connect成功(失败).判断recv/recvfrom成功(失败).判断send/sendto 博客分类: Linux Socket s ...

  2. Socket异步通信学习三

    接下来是客户端部分,采用同步接收模式,在SocketClient项目中新建了一个SynServer类,用于存放socket服务器代码,和AsynServer类似,主要有4个方法: 有一个全局socke ...

  3. Socket异步通信学习二

    接下来是服务器部分,采用异步模式,新建了一个AsynServer类,用于存放socket服务器代码,主要有4个方法: 有一个全局socket,下面四个方法中都用到. Socket socket = n ...

  4. Socket异步通信学习一

    最近在做一个频谱管理项目,负责通信模块,自己也是小白,重头学起,直至今天通信基本框架已经完成,把自己在学习中的心得与大家分享一下,做一个socket系列的博文,顺便加固一下自己对socket通信的认识 ...

  5. Socket异步通信及心跳包同时响应逻辑分析。

    有段时间没有更博了,刚好最近在做Socket通信的项目,原理大致内容:[二维码-(加logo)]-->提供主机地址和端口号信息(直接使用[ThoughtWorks.QRCode.dll]比较简单 ...

  6. socket 异步通信的一些问题

    socket通信在使用时被封装很简单,像操作文件一样简单,正是因为简单里面好多细节需要深入研究一下. windows下通信有select和iocp方式,select是传统方式,在socket里使用re ...

  7. Socket 异步通信

    最近在写数据通信的时候用到的东西!希望对大家有帮助 /// <summary> /// 获取或设置服务器IP地址 /// </summary> public string se ...

  8. Socket异步通信及心跳包同时响应逻辑分析(最后附Demo)。

    有段时间没有更博了,刚好最近在做Socket通信的项目,原理大致内容:[二维码-(加logo)]-->提供主机地址和端口号信息(直接使用[ThoughtWorks.QRCode.dll]比较简单 ...

  9. Socket 异步通信示例

    这个项目是一个控制台应用程序: 服务器端: using System; using System.Net; using System.Net.Sockets; using System.Text; u ...

随机推荐

  1. C# 实现汉字转拼音

    /// <summary> /// 生成拼音简码 /// </summary> /// <param name="unicodeString"> ...

  2. Spring + Spring MVC + MyBatis框架整合

    ---恢复内容开始--- 一.Maven Web项目创建 如有需要,请参考:使用maven创建web项目 二.Spring + Spring MVC + MyBatis整合 1.Maven引入需要的J ...

  3. jQuery EasyUI Datagrid VirtualScrollView视图简单分析

    大家都知道EasyUI的Datagrid组件在加载大数据量时的优势并不是很明显,相对于其他一些框架,如果数据量达到几千,便会比较慢,特别是在IE下面.针对这种情况,我们首要做的是要相办法优化datag ...

  4. Windows下下载及安装numpy、pandas及简单应用

    下载numpy 下载地址 https://pypi.python.org/pypi/numpy 进入网站,下载和自己电脑及电脑中安装的python匹配的numpy版本.我的电脑是Win 10 x64位 ...

  5. Xposed框架

    Xposed框架,很好的一款软件,早起百团大战.外卖大战时候,对拉新用户有很大的帮助,一直没时间整理,今天有看到一个公众账号介绍这款,准备大概整理下,做个记录. 整理下思路 新用户,无非就是1.手机号 ...

  6. mysql数据库(一):建表与新增数据

    一. 学习目标 理解什么是数据库,什么是表 怎样创建数据库和表(create) 怎样往表里插入数据(insert) 怎样修改表里的数据(update) 怎样删除数据库,表以及数据(delete) 二. ...

  7. 解决:TypeError: object() takes no parameters

    运行测试用例时发现以下报错 Ran 1 test in 22.505s FAILED (errors=1) Error Traceback (most recent call last): File ...

  8. DGA短域名(360样本) mark下 下次分析可以参考

    一共100万,按照长度排序后的前2000个: aagst.cnacyke.wsaefrd.ccaiqxg.ukakplh.pwalurx.pwamsmz.ccaogtp.inawwgf.inayveg ...

  9. .SourceInsight添加.S文件

    在Option->Document Option添加配置.S然后再去添加文件

  10. 第七次scrum meeting记录

    文章负责:张华杰 日期:2017年10月31日 会议地点:主楼主南201 各组员工作情况 团队成员 昨日完成任务 明日要完成任务 赵晓宇 课程列表页面搭建 issue20 课程列表页面搭建(part ...