视频图像处理系列索引 || Arcgis/Engine/Server开发索引 || Web Map Gis开发索引 || jquery表格组件 JQGrid索引
WPF MVVM模式开发实现简明教程索引 || ArcGIS Runtime WPF(.net C#)开发简明教程索引

C# 的TCP Socket (异步方式)

 

简单的c# TCP通讯(TcpListener)

C# 的TCP Socket (同步方式)

C# 的TCP Socket (异步方式)

C# 的tcp Socket设置自定义超时时间

C# TCP socket发送大数据包时,接收端和发送端数据不一致 服务端接收Receive不完全

 

 

服务器端:

 

public static ManualResetEvent allDone = new ManualResetEvent(false);

IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port); 
            Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

listener.Bind(localEndPoint);
                listener.Listen(30);
                while (true)
                {
                        allDone.Reset();
                        listener.BeginAccept(new AsyncCallback(AcceptCallback), listener);
                        allDone.WaitOne();
                }
public static void AcceptCallback(IAsyncResult ar)
        {
            try
            {
                if (IsListening)
                {
                    allDone.Set();
                    Socket listener = (Socket)ar.AsyncState;
                    Socket handler = listener.EndAccept(ar);
                    StateObject state = new StateObject();
                    state.workSocket = handler;
                    handler.BeginReceive(state.headbuffer, 0, StateObject.HeadBufferSize, 0,
                        new AsyncCallback(ReceiveHead), state);
                }
            }
            catch (System.Exception ex)
            {
                log.ErrorFormat("AcceptCallback异常,原因:{0}", ex.Message);
            }
        }

        private static void ReceiveHead(IAsyncResult ar)
        {
            try
            {
                StateObject state = (StateObject)ar.AsyncState;
                Socket handler = state.workSocket;
                int bytesRead = handler.EndReceive(ar);

                uint length = ByteTouint(state.headbuffer, 3, 4);
                state.length = (int)length - state.headbuffer.Length;
                state.bodybuffer = new byte[length - state.headbuffer.Length];
                handler.BeginReceive(state.bodybuffer, 0, state.bodybuffer.Length, 0,
                        new AsyncCallback(ReadCallback), state);
            }
            catch (System.Exception ex)
            {
                log.ErrorFormat("ReceiveHead异常,原因:{0}", ex.Message + ex.StackTrace);
            }
        }

 

public static void ReadCallback(IAsyncResult ar)
{
            StateObject state = (StateObject)ar.AsyncState;
            Socket handler = state.workSocket;

              int bytesRead = handler.EndReceive(ar);
    //接收的字节在state.bodybuffer里,下面开始处理

}

 

已知接收长度的话可以把ReceiveHead函数去掉,直接进入ReadCallback里。

加入ReceiveHead是因为协议一般在开头几个固定的字节里有后面数据包的长度,所以先接收头,再根据解析出的数据包长度接收后面的。

 

StateObject定义如下,可以不要headbuffer  HeadBufferSize
public class StateObject
    {
        public Socket workSocket = null;
        public const int HeadBufferSize = 7;

        public byte[] headbuffer = new byte[HeadBufferSize];
        public byte[] bodybuffer;
        public int length;
    }

 

客户端发送数据  C# 的TCP Socket (同步方式)

 

posted @ 2016-08-23 14:45  jhlong  阅读(15649)  评论(5编辑  收藏  举报
海龙的博客 jhlong@cnblogs 版权所有© 转载请注明链接.有用请推荐一下
代码全部经过本人测试,但不保证复制粘贴就正常运行,更不保证能解决你的问题,请结合前后代码及描述理解后修改和使用