我试图在一个简单的客户端服务器程序中演示Nagle算法。但我不太清楚,或者让它清晰地印在我面前。
在我的示例中,客户机只生成从1到1024的int,并将它们发送到服务器。服务器只是将这些int转换为十六进制字符串并将它们发送回客户端。
我改变的每一件事都有同样的结果。int是以256 int的块发送和憎恨的。我在双方都尝试了setTcpNoDelay(true)来查看更改,但这在我的控制台中得到了相同的结果。(但没有在wireshark中,我看到服务器和客户端之间发送的数据包数量有很大的不同),但我的目标是能够在控制台中看到它,我猜有一些ObjectOutputStream缓冲区或类似的缓冲区将所有东西都搁置起来?
当我将output = new PrintWriter(client.getOutputStream(), true)更改为false ( true或false__:autoFlush;如果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();
}
}
}发布于 2011-04-22 22:18:01
您将无法看到两者之间的差异,因为readLine()将等待读取eol。若要查看差异,请使用二进制数据。使输出流写入由10 by分隔的64个字节的块。使传入流读取1024字节的块。当tcpNoDelay为真时,传入流将在每次读取操作中读取约64个字节。当tcpNoDelay为false时,传入流将读取更多字节。您可以记录每次读取操作中读取的平均字节数,因此差异很明显。并且总是使用两台机器进行测试,因为操作系统可以优化回送流。
https://stackoverflow.com/questions/2536768
复制相似问题