如何在C#套接字侦听器中“刷新”TCP客户端缓冲区 [英] How do I “flush” a TCP Client Buffer in C# socket listener

查看:87
本文介绍了如何在C#套接字侦听器中“刷新”TCP客户端缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已从几个示例中提取设置连接到服务器的全双工C#TCP客户端。基本概念是客户端和服务器都发送/接收消息(命令和事件)。因此,我开发了一个FullDuplexSocket类,它公开了一个Send方法,用于向服务器发送消息,以及一个事件处理程序,用于从服务器接收消息。一切正常,除了我似乎无法刷新从服务器收到的消息的缓冲区。每次服务器发送新消息时,我的套接字中的缓冲区都包含所有旧消息(已读取)和新消息



我收到的消息数据



I've pulled from several examples to setup a Full Duplex C# TCP Client connected to a server. The basic concept is that the client and server both send/receive messages (commands and events). Therefore, I've developed a FullDuplexSocket class that exposes a Send method to send messages to the server and a event handler to receive messages from the server. Everything works except I can't seem to flush the buffer of messages received from the server. Each time the server sends a new message, the buffer in my socket contains all the old messages (already read) and the new message(s)

I am getting receive data in

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.
                
                    // Echo the data back to the client.

                    handler.NoDelay = true;
                  //  Send(handler, content);
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                   new AsyncCallback(ReadCallback), state);
                }
                else
                {
                    // Not all data received. Get more.
                    handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0,
                    new AsyncCallback(ReadCallback), state);
                }
            }
        }

推荐答案

这项工作对我来说

带插座没有齐平





this work with me
with socket without flush


void start_listen()
   {
       String    result1="";
       char[] incoming = new char[1024];
       while (!s.isClosed())
       {
           try {

           int lenght  =  input.read(incoming);
               result1 = String.copyValueOf(incoming,0,lenght);


           }
           catch (IOException e)
           {
               e.printStackTrace();
           }
           Log.d("ddddddddddd",result1);

       }


这篇关于如何在C#套接字侦听器中“刷新”TCP客户端缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆