java tcp程序太慢(在同一台计算机上)

时间:2013-04-12 16:01:36

标签: java performance sockets tcp

所以基本上我在我的计算机广告上运行2个java应用程序然后在我传输数据后我计算传输速度超过java tcp并且速度总是在7兆比特/秒左右 无论如何,我期待至少56mbits /秒的客户应用程序:

 import java.io.*;
import java.net.*;

class TCPClient
{
 public static void main(String argv[]) throws Exception
 {


 System.out.println("Hello, World");


           String modifiedSentence;
           BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
           Socket clientSocket = new Socket("127.0.0.1", 7);

           BufferedInputStream inFromServer  = new BufferedInputStream(clientSocket.getInputStream());

           double count=0;
           OutputStream socketOutputStream = clientSocket.getOutputStream();

        String s = "012345678901234567\n";    

        byte[] bytes = s.getBytes(); 

        final int BUFFER_SIZE = 65536;
        byte[] buffer = new byte[BUFFER_SIZE];

        // socketOutputStream.write(bytes);
        // inFromServer.read(buffer);
        // modifiedSentence = new String(buffer);
        // System.out.println("FROM SERVER:" + modifiedSentence);
         socketOutputStream.write(bytes);
         long times=System.nanoTime() ;
        long temp=times;long temp2=times;
           while (true) {
               socketOutputStream.write(bytes);
           count++;

           socketOutputStream.flush();
               if(count>=100000){
                   break;

               }

           }

           times=System.nanoTime() ;
           System.out.println("time(ns) : " + (times-temp2));
           double speed=(72*2*count)/(double)((times-temp2)*Math.pow(10,-9));
           System.out.println("speed : " + speed/(1024*1024));
           clientSocket.close();


 }
}

这是服务器代码:

import java.io.*;
import java.net.*;

class TCPClient extends Thread
{
    Socket clientSocket;
      public TCPClient(Socket socket) {
            super("KKMultiServerThread");
            this.clientSocket = socket;
            }

 public  void run() //throws Exception
 {


 System.out.println("Hello, World");
    try {

           String modifiedSentence;


           BufferedOutputStream outToServer = new BufferedOutputStream(clientSocket.getOutputStream());
           BufferedInputStream inFromServer  = new BufferedInputStream(clientSocket.getInputStream());

           double count=0;


        String s = "012345678901234567\n";    

        byte[] bytes = s.getBytes(); 
        long times=System.nanoTime() ;
        long temp=times;long temp2=times;
        final int BUFFER_SIZE = 19;
        byte[] buffer = new byte[BUFFER_SIZE];

         modifiedSentence = "";
           while ((inFromServer.read(buffer))!=-1) {

          // outToServer.write(bytes);
         //  outToServer.flush();


           }

           System.out.println("FROM SERVER:" + modifiedSentence);

           clientSocket.close();

     } catch (UnknownHostException e) {
         System.err.println("Don't know about host: taranis.");
         System.exit(1);
     } catch (IOException e) {
         System.err.println("Couldn't get I/O for "
                            + "the connection to: taranis.");
         System.exit(1);
     }


 }


}

服务器代码调整后不写,但只接收和客户端只发送通过put增加到27Mbit / sec,而不是50Mbit / sec哪个是以太网的半速,有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

我同意LJ2。

要提高吞吐量,请将缓冲区大小调整为1024字节,然后发送相同数量的数据;你目前只发送18个字节;这不是很有效 另外,考虑只在一边阅读...你正在进行乒乓球比赛(客户端之后读取和写入,服务器也是如此) - 如果涉及到吞吐量,则无济于事。