提高简单 TCP 客户端/套接字上的数据传输速度

发布于 2024-12-04 08:34:50 字数 1826 浏览 0 评论 0原文

我有一个家庭作业,通过重定向标准 I/O 来创建一个带有客户端/服务器 TCP 套接字对的简单数据传输机制。我实际上可以正常工作,但是当我尝试传输大文件(比如~5g)时,速度会急剧下降。我正在使用 BufferedInputStream 和 BufferedOutputStream,我认为也许我可以在那里进行一些优化。我的服务器的代码是:

private static final int BUF_SIZE = 2047;

public static void main(String[] args) throws IOException{
   /*
    * Attempt to parse command line arguments.
    * @require args[0] is an int
    */
   int port = 0;
   try {
       port = Integer.parseInt(args[0]);
   } catch(NumberFormatException e) {
       System.err.println("Port must be an integer in range 0 - 65535.");
       System.exit(-1);
   }

   /*
    * Bind server socket to specified port number and wait for request.
    * @require port >= 0 && port <= 65535
    */
   ServerSocket welcomeSocket = null;
   welcomeSocket = new ServerSocket(port);
   System.out.println("Now listening on port: " + port);

    /*
     * Accept connection from client socket.
     */
    Socket connectionSocket = null;
    connectionSocket = welcomeSocket.accept();
    System.out.println("Client made connection");

    BufferedInputStream input;
    BufferedOutputStream output;
    if(System.in.available() > 0) {
        input = new BufferedInputStream(System.in, BUF_SIZE);
        output = new BufferedOutputStream(
                connectionSocket.getOutputStream(), BUF_SIZE);
    } else {
        input = new BufferedInputStream(
                connectionSocket.getInputStream(), BUF_SIZE);
        output = new BufferedOutputStream(System.out, BUF_SIZE);
    }

    int place;
    while((place = input.read()) != -1)
        output.write(place);

    input.close();
    output.close();
    welcomeSocket.close();
    connectionSocket.close();
}

客户端代码本质上是相同的。我尝试过使用不同的缓冲区大小,包括默认值(不指定缓冲区大小),但它们都以大致相同的速度运行。关于如何提高我的表现有什么建议吗?

谢谢您的宝贵时间!

I have a homework assignment to create a simple data transfer mechanism with a client/server TCP socket pair by redirecting standard I/O. I actually have it working, but when I try to transfer large files (say ~5g) the speed slows down dramatically. I am using BufferedInputStream and BufferedOutputStream, and I think that perhaps there is some optimization I can make there. The code for my server is:

private static final int BUF_SIZE = 2047;

public static void main(String[] args) throws IOException{
   /*
    * Attempt to parse command line arguments.
    * @require args[0] is an int
    */
   int port = 0;
   try {
       port = Integer.parseInt(args[0]);
   } catch(NumberFormatException e) {
       System.err.println("Port must be an integer in range 0 - 65535.");
       System.exit(-1);
   }

   /*
    * Bind server socket to specified port number and wait for request.
    * @require port >= 0 && port <= 65535
    */
   ServerSocket welcomeSocket = null;
   welcomeSocket = new ServerSocket(port);
   System.out.println("Now listening on port: " + port);

    /*
     * Accept connection from client socket.
     */
    Socket connectionSocket = null;
    connectionSocket = welcomeSocket.accept();
    System.out.println("Client made connection");

    BufferedInputStream input;
    BufferedOutputStream output;
    if(System.in.available() > 0) {
        input = new BufferedInputStream(System.in, BUF_SIZE);
        output = new BufferedOutputStream(
                connectionSocket.getOutputStream(), BUF_SIZE);
    } else {
        input = new BufferedInputStream(
                connectionSocket.getInputStream(), BUF_SIZE);
        output = new BufferedOutputStream(System.out, BUF_SIZE);
    }

    int place;
    while((place = input.read()) != -1)
        output.write(place);

    input.close();
    output.close();
    welcomeSocket.close();
    connectionSocket.close();
}

The client code is essentially the same. I have tried using different buffer sizes, including the default (by not specifying a buffer size), but they are all running at approximately the same speed. Any pointers on how I can increase my performance?

Thank you for your time!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

想你的星星会说话 2024-12-11 08:34:50
while((place = input.read()) != -1)

您一次从缓冲区读取一个字节。调用该方法数百万次的开销是相当大的。

我建议使用其他版本将多个字节读取到缓冲区中(并以相同的方式写入):

public int read(byte[] b,
            int off,
            int len)

示例:

byte[] myBuffer = new byte[BUF_SIZE];
while((place = input.read(myBuffer, 0, BUF_SIZE)) != 1)
    output.write(myBuffer, 0, place);
while((place = input.read()) != -1)

You're reading one byte at a time from the buffer. The overhead of calling this method millions of times is rather large.

I would suggest reading more than one byte into a buffer with the other version (and writing the same way):

public int read(byte[] b,
            int off,
            int len)

Example:

byte[] myBuffer = new byte[BUF_SIZE];
while((place = input.read(myBuffer, 0, BUF_SIZE)) != 1)
    output.write(myBuffer, 0, place);
夏の忆 2024-12-11 08:34:50

您一次读取和发送一个字节,效率不高,您应该读取数据块(空闲大小将是磁盘硬件缓冲区大小)。
当然,磁盘应该是你的瓶颈,读取5G格式的磁盘需要时间。

you are reading and sending a byte at a time which is not efficient, you should read blocks of data (idle size would be the disk hardware buffer size).
of course, the disk should be your bottle neck here it takes time to read 5G form disk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文