Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#仅在代码末尾发送字节。。。[插座]_C#_Sockets_Send_Exit - Fatal编程技术网

C#仅在代码末尾发送字节。。。[插座]

C#仅在代码末尾发送字节。。。[插座],c#,sockets,send,exit,C#,Sockets,Send,Exit,我是C#新手,这是我的第一个代码:D我过去用Java编写代码,现在我在发送套接字方面遇到了很多麻烦。 当我尝试发送套接字时,它仅在代码的末尾发送: 比如说 using System; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.IO; using System.Text; namespace HelloWorld { class Pr

我是C#新手,这是我的第一个代码:D我过去用Java编写代码,现在我在发送套接字方面遇到了很多麻烦。 当我尝试发送套接字时,它仅在代码的末尾发送: 比如说

    using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        public static TcpClient client;
        public static NetworkStream networkStream;

        public static void Main(string[] args)
        {
            client = new TcpClient("62.210.130.212", 35025);
            networkStream = client.GetStream();
            byte[] usernameBytes = Encoding.ASCII.GetBytes("This is just a test bro!");
            networkStream.Write(usernameBytes, 0, usernameBytes.Length);
            networkStream.Flush();

            while (true)
            {
                // Infinte loop
            }
            // THE SOCKER WILL ONLY BE SENT HERE, AT THE END OF THE CODE, WHY ?!
        }
    }
}
此处,套接字仅在关闭程序时发送(由于wile循环)

这里呢

using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;

namespace HelloWorld
{
    class Program
    {
        public static TcpClient client;
        public static NetworkStream networkStream;

        public static void Main(string[] args)
        {
            client = new TcpClient("62.210.130.212", 35025);
            networkStream = client.GetStream();
            byte[] usernameBytes = Encoding.ASCII.GetBytes("This is just a test bro!");
            networkStream.Write(usernameBytes, 0, usernameBytes.Length);
            networkStream.Flush();
        }
    }
}
在这种情况下,它将被直接发送,因为它是代码的结尾

有人知道为什么会发生这种情况以及如何解决吗?我希望我的套接字在运行时的任何时候都被发送,而不仅仅是在末尾:D

多谢各位!谢谢你一直读到这里。 朱利安

有人知道为什么会发生这种情况以及如何解决吗?我希望我的套接字可以在运行时的任何时候发送,而不仅仅是在最后

为什么会这样? 由于,传输将短暂延迟(以毫秒为单位)

这意味着您的代码在进入循环之前没有提供足够的时间

networkStream.Write(usernameBytes, 0, usernameBytes.Length);
networkStream.Flush();

// the time between here is not enough for nagle's algorithm to complete

while (true) { } // blocks the thread from doing any more work

// so, it will end up sending here instead.

如何修复它? 一种快速解决方案——禁用Nagle算法 如中所述,您可以将
TcpClient.NoDelay
设置为
true
以禁用Nagle的算法

通过将其添加到代码中,您可以轻松实现这一点:

 client = new TcpClient("62.210.130.212", 35025);
 client.NoDelay = true; // add this line of code to disable Nagle's algorithm
注意:此解决方案将立即发送。但是,由于您的问题不清楚具体要求,请注意,这可能不是“最佳”解决方案

更好的解决方案-通过处置资源强制执行 如果您处置/关闭
tcp客户端
,它将完成相同的任务。此外,它还提供了一个附加的(更好的实践),即在您使用完资源后立即处理它

public static void Main(string[] args)
{
    using (TcpClient client = new TcpClient("62.210.130.212", 35025))
    using (NetworkStream networkStream = client.GetStream())
    {
        byte[] usernameBytes = Encoding.ASCII.GetBytes("This is just a test bro!");
        networkStream.Write(usernameBytes, 0, usernameBytes.Length);
    }

    while (true) { } // loop infinitely
}
注意:这实际上并不像上面的解决方案那样立即发送数据包,而是强制数据包发送,因为我们正在关闭它。然而,在您的案例中,结果是相同的效果,这是一个更好的方法,您应该实践


纳格尔算法是什么? Nagle算法旨在通过使套接字缓冲小数据包,然后在特定情况下将它们合并并发送到一个数据包中,从而减少网络流量

TCP数据包由40字节的报头加上正在发送的数据组成。当使用TCP发送小数据包时,TCP报头产生的开销可能成为网络流量的重要组成部分。在负载较重的网络上,这种开销导致的拥塞可能导致数据报丢失和重新传输,以及拥塞导致的过度传播时间。当新的传出数据从用户处到达时,如果连接上先前传输的任何数据未被确认,Nagle算法将禁止发送新的TCP段

大多数网络应用程序都应该使用Nagle算法


在用户数据报协议(UDP)套接字上设置此属性将无效。

哦,非常感谢,它现在工作正常,我只是有另一个问题。现在如何接收和发送带有网络流的新行?我不能创建它的另一个实例…是的,我必须等待90分钟,直到发布一个新的问题,所以我会链接它,当它完成。非常感谢您对我的帮助:D@NoBravesStudio-您应该阅读此内容,以便快速进行以下链接: