- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我试图在一个简单的客户端-服务器程序中说明 Nagle 算法。但我不太明白,也无法将其清楚地打印给我。
在我的示例中,客户端只是生成从 1 到 1024 的整数并将它们发送到服务器。服务器只是将这些 int 转换为十六进制字符串并将它们发送回客户端。
我所做的几乎所有更改都以相同的结果结束。 int 以 256 个 int 的 block 的形式发送和重新发送..我在两侧尝试了 setTcpNoDelay(true) 以查看更改,但这在我的控制台中给出了相同的结果。 (但不是在 wireshark 中,我发现服务器和客户端之间发送的数据包数量有很大差异)但我的目标是能够在控制台中看到它,我猜有一些 ObjectOutputStream 缓冲区或类似的东西可以支撑一切?
当我将 output = new PrintWriter(client.getOutputStream(), true)
更改为 false
(true
或 false
:autoFlush
- boolean 值;如果 true
,则 println
、printf
或format
方法将刷新输出缓冲区)我的服务器不再将任何输出返回给客户端。
基本上,我的目标是在启动时将服务器和/或客户端的 true 或 false 作为参数来设置 TcpNoDelay,以便清楚地看到控制台中输入/输出的差异。我不确定所使用的所有内容,因此欢迎任何帮助来解决这个问题。
服务器:
package Networks.Nagle;
import java.io.*;
import java.net.*;
import java.util.*;
public class NagleDemoServer
{
private static ServerSocket serverSocket;
private static final int PORT = 1234;
public static void main(String[] args) throws IOException
{
int received = 0;
String returned;
ObjectInputStream input = null;
PrintWriter output = null;
Socket client;
try
{
serverSocket = new ServerSocket(PORT);
System.out.println("\nServer started...");
}
catch (IOException ioEx)
{
System.out.println("\nUnable to set up port!");
System.exit(1);
}
while(true)
{
client = serverSocket.accept();
client.setTcpNoDelay(true);
System.out.println("\nNew client accepted.\n");
try
{
input = new ObjectInputStream(client.getInputStream());
output = new PrintWriter(client.getOutputStream(), true);
while( true )
{
received = input.readInt();
returned = Integer.toHexString(received);
System.out.print(" " + received);
output.println(returned.toUpperCase());
}
}
catch(EOFException eofEx)
{
output.flush();
System.out.println("\nEnd of client data.\n");
}
catch(SocketException sEx)
{
System.out.println("\nAbnormal end of client data.\n");
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
input.close();
output.close();
client.close();
System.out.println("\nClient closed.\n");
}
}
}
客户端:
package Networks.Nagle;
import java.io.*;
import java.net.*;
import java.util.*;
public class NagleDemoClient
{
private static InetAddress host;
private static final int PORT = 1234;
public static void main(String[] args)
{
Socket socket = null;
try
{
host = InetAddress.getByName("localhost");
socket = new Socket(host, PORT);
socket.setTcpNoDelay(true);
socket.setSendBufferSize(64);
System.out.println("Send Buffer: " + socket.getSendBufferSize());
System.out.println("Timeout: " + socket.getSoTimeout());
System.out.println("Nagle deactivated: " + socket.getTcpNoDelay());
}
catch(UnknownHostException uhEx)
{
System.out.println("\nHost ID not found!\n");
System.exit(1);
}
catch(SocketException sEx)
{
sEx.printStackTrace();
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
NagleClientThread client = new NagleClientThread(socket);
NagleReceiverThread receiver = new NagleReceiverThread(socket);
client.start();
receiver.start();
try
{
client.join();
receiver.join();
socket.close();
}
catch(InterruptedException iEx)
{
iEx.printStackTrace();
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
System.out.println("\nClient finished.");
}
}
class NagleClientThread extends Thread
{
private Socket socket;
public NagleClientThread(Socket s)
{
socket = s;
}
public void run()
{
try
{
ObjectOutputStream output = new ObjectOutputStream(socket.getOutputStream());
for( int i = 1; i < 1025; i++)
{
output.writeInt(i);
sleep(10);
}
output.flush();
sleep(1000);
output.close();
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
catch(InterruptedException iEx)
{
iEx.printStackTrace();
}
}
}
class NagleReceiverThread extends Thread
{
private Socket socket;
public NagleReceiverThread(Socket s)
{
socket = s;
}
public void run()
{
String response = null;
BufferedReader input = null;
try
{
input = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
try
{
while( true )
{
response = input.readLine();
System.out.print(response + " ");
}
}
catch(Exception e)
{
System.out.println("\nEnd of server data.\n");
}
input.close();
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
}
}
最佳答案
您将无法看到差异,因为 readLine() 将等待直到读取 eol。要查看差异,请使用二进制数据。使传出流写入由 10ms sleep 分隔的 64 字节 block 。使传入流读取 1024 字节的 block 。当 tcpNoDelay 为真时,传入流将在每次读取操作时读取大约 64 个字节。当 tcpNoDelay 为 false 时,传入流将读取更多字节。您可以记录每次读取操作读取的平均字节数,因此差异很明显。并且始终使用两台机器进行测试,因为操作系统可以优化环回流。
关于Java 和 Nagle 插图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2536768/
我在 Linux (Ubuntu) 上工作。我理解这两个概念,但我想自己阅读逻辑。源码写在什么地方?有人说在 FreeBSD 中更容易理解。是真的吗? 最佳答案 我不知道 FreeBSD,但 Nagl
所以我有了这个实时游戏,C++ 服务器使用 SFML library 禁用了 nagle , 和客户端使用 asyncsocket , 也禁用nagle。我每 1 秒发送 30 个数据包。从客户端发送
出于无聊的好奇心,我一直在研究 Nagle 的算法。我理解它背后的基本概念(TCP 数据包包含大量开销,尤其是在处理小负载时),但我不确定我是否理解了实现。 我正在阅读 this article在维基
下面是对Nagle算法的两个简单描述。 Version 1: Wait for the peer to acknowledge the previously sent packets before s
Nagle 算法是否在 Windows 中的环回连接上处于事件状态? 最佳答案 在 Windows 上,根据 Microsoft documentation,出于性能原因,Nagle 算法未应用于环回
我试图在一个简单的客户端-服务器程序中说明 Nagle 算法。但我不太明白,也无法将其清楚地打印给我。 在我的示例中,客户端只是生成从 1 到 1024 的整数并将它们发送到服务器。服务器只是将这些
根据 this Socket FAQ article , Nagle 的算法是许多算法中的一种,它可以导致一堆数据位于 TCP 缓冲区中,而不是到达线路。 Nagle 算法的延迟最高可达 200 毫秒
来自 Node redis docs : socket_nodelay: defaults to true. Whether to call setNoDelay() on the TCP strea
是否也需要在客户端禁用 Nagle 算法?如果是这种情况,我还没有找到单独通过 JavaScript 禁用 Nagle 算法的方法。 我正在尝试从 Raspbian OS 上托管的 PHP CLI 服
因此,当我遇到 Nagle 的算法和针对小型数据包(1 字节数据)的延迟 ACK 时,我正在研究 TCP。原因是,避免在网络上发送大量小数据包 (Nagle) 和搭载数据 (Delayed ACK)。
我看过很多关于在 Azure 上工作时禁用 WCF 中的 Nagle 算法的帖子。我一直想知道这是否仅适用于 Azure 或者这是否应该是更通用的最佳实践。 如各种来源所述,Nagle 算法基本上将小
我正在阅读 this thread并在解决方案中注意到,如果您发送的数据长度与实际数据分开,Nagle 的算法可能会播放和取消同步发送的数据。 我想知道这一点,因为我看到很多代码示例,客户端首先发送数
我正在使用 HttpUrlConnection 与服务器通信。延迟非常重要,在使用 WireShark 分析 tcpdump 日志后,我得出的结论是 Nagle 的算法存在问题。 我知道可以通过设置
我正在使用 MPCF 创建多人游戏.您可以使用 iPhone 在 iPad 上控制 spaceship 。 我在随机时间和间隔遇到各种延迟和延迟以及缓冲/暂停,现在已经登陆苹果 Technical Q
我们正在使用 ServiceStack 3.9.71.0,目前我们在 WAN 连接上遇到无法解释的客户端延迟问题。 在 200 毫秒以上后收到负载非常小(); sp.UseNagleAlgorithm
我正在使用 webSockets 将 javascript webSocket 客户端连接到 java webSocketServer(来自 Android 应用程序),使用 Java-WebSock
我在网上找不到任何可以在 Linux 中从命令行禁用 Nagle 算法的东西。 Solaris 确实有一种机制可以从命令行启用/禁用它。 如果有任何方法可以在 Linux 上执行此操作,请告诉我。 最
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我需要在 python2.6 中禁用 nagle 算法。我发现以这种方式在 httplib.py 中修补 HTTPConnection def connect(self): "
有没有人知道如何在使用 socket.io 时配置 nagle 的算法(打开或关闭)? 这个选项甚至与socket.io一起提供吗? 我假设默认行为被配置为使用 nagle 的算法(如果我错了,请纠正
我是一名优秀的程序员,十分优秀!