美文网首页
TCP异步编程接收数据问题

TCP异步编程接收数据问题

作者: 在河之简 | 来源:发表于2015-09-18 08:45 被阅读302次

参考博客

在回调函数中继续调用BeginReceive函数,便可解决不能一直接收数据的问题。

private static void Receive(Socket client)
        {
            try
            {
                // Create the state object.     
                StateObject state = new StateObject();
                state.workSocket = client;
                // Begin receiving the data from the remote device.     
                client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);
            }
            catch (Exception e)
            {
                MessageBox.Show("Receive出错了" + e.ToString());
            }
        }
        private static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                // Retrieve the state object and the client socket     
                // from the asynchronous state object.     
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;
                // Read data from the remote device.     
                int bytesRead = client.EndReceive(ar);
                if (bytesRead > 0)
                {
                    // There might be more data, so store the data received so far.     
                    string getMsg = Encoding.ASCII.GetString(state.buffer, 0, bytesRead);

                    //Console.WriteLine("新得到的数据是" + getMsg + "呵呵");
                    MessageBox.Show("新得到的数据是" + getMsg);
                    state.sb.Append(Encoding.ASCII.GetString(state.buffer, 0, bytesRead));

                    // Get the rest of the data.     
                    client.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, new AsyncCallback(ReceiveCallback), state);

                }
                else
                {
                    // All the data has arrived; put it in response.     
                    if (state.sb.Length > 1)
                    {
                        response = state.sb.ToString();
                    }
                    // Signal that all bytes have been received.     
                    receiveDone.Set();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Receive回调函数出错了" + e.ToString());
            }
        }

相关文章

  • TCP异步编程接收数据问题

    参考博客 在回调函数中继续调用BeginReceive函数,便可解决不能一直接收数据的问题。

  • Python总结之 recv与recv_from

    在udp编程中,会发现,在利用socke接收数据时用的时recv_from,在tcp编程中用的是recv。但是,细...

  • Python || 网络编程

    Python3的TCP编程:客户端功能是创建socket对象并建立TCP连接、发送请求、接收服务器返回的数据并进行...

  • ES6学习--异步编程--Generator

    异步编程 : (异步编程的语法目的就是让异步编程更像同步编程) 回调函数利用回调函数实现异步编程本身没有问题, 问...

  • Node.js中的网络编程

    实验简介 此实验主要讲解TCP和UDP的网络编程,net模块提供了一个异步网络包装器,用于TCP网络编程,它包含了...

  • day32-粘包问题和报头的定制

    粘包问题 TCP协议作为流式协议,只有TCP协议存在粘包问题。 发送端可以是一K一K地发送数据,而接收端的应用程序...

  • RxJS

    一、简介(异步编程) 源自微软的JS脚本,针对异步数据流的编程。它将所有的数据(http,DOM,或普通数据等)包...

  • Stream_recvmsg

    stream recvmsg要检测sock状态为TCP_ESTABLISHED,接收数据时,可跨越多个数据包,接收...

  • Dart简介5--Stream

    说明 Stream 也是用于接收异步事件数据,和 Future 不同的是,它可以接收多个异步操作的结果(成功或失败...

  • RxJava在Android移动端开发中的实战应用之二

    响应式编程是以异步和数据流来构建事务关系的编程模型,异步和数据流是以构建事务关系而存在,异步是为了区分无关的事务,...

网友评论

      本文标题:TCP异步编程接收数据问题

      本文链接:https://www.haomeiwen.com/subject/eupbcttx.html