c# - 网络流总是空的!

标签 c# networking

嘿,我正在写一个服务器-客户端程序 但是当我的客户端发送一些东西时,它永远不会到达我的服务器!

我是这样发送的:

    public void Send(string s)  
    {  
        char[] chars = s.ToCharArray();  
        byte[] bytes = chars.CharToByte();  
        nstream.Write(bytes, 0, bytes.Length);  
        nstream.Flush();  
    }

并像这样在后台线程中接收

    void CheckIncoming(object dd)
    {
        RecievedDelegate d = (RecievedDelegate)dd;
        try
        {

            while (true)
            {
                List<byte> bytelist = new List<byte>();
                System.Threading.Thread.Sleep(1000);
                int ssss;
                ssss = nstream.ReadByte();
                if (ssss > 1)
                {
                    System.Diagnostics.Debugger.Break();
                }
                if (bytelist.Count != 0)
                {
                    d.Invoke(bytelist.ToArray());
                }
            }
        }
        catch (Exception exp)
        {
            MSGBOX("ERROR:\n" + exp.Message);
        }
    }

ssss int 永远不会 > 1 这里发生了什么???

最佳答案

NetworkStream.Flush()其实没有效果:

The Flush method implements the Stream..::.Flush method; however, because NetworkStream is not buffered, it has no affect [sic] on network streams. Calling the Flush method does not throw an exception

正在发送多少数据?
如果您没有发送足够的数据,它可能会在网络级别保持缓冲,直到您关闭流或写入更多数据。

参见 TcpClient.NoDelay如果您只发送小块数据并需要低延迟,请使用属性来禁用它。

关于c# - 网络流总是空的!,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2507929/

相关文章:

c# - ParameterInfo.ParameterType 是 "SqlBytes&",如何与 typeof() 比较

c# - protected 字段的符合 CLS 的正确命名约定是什么?

networking - Erlang:以编程方式启动远程节点

linux - Docker Compose 在容器的/etc/hosts 中设置 Linux Docker 主机 IP 地址

c# - 表达式树嵌套 wheres

c# - winform 中的线程问题

c# - 枚举扩展方法未显示

docker - Docker-无法从容器内部ping主机子网上的任何内容

c# - 发送给客户端后获取无效的 Zip 文件

url - DNS服务器如何工作知道网站的IP地址?