所以基本上我在我的电脑广告上运行 2 个 java 应用程序,然后在我传输数据后,我计算通过 java tcp 的传输速度,速度总是在 7 兆比特/秒左右,我期待至少 56 兆比特/秒的东西,这是客户端应用程序:
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);
}
}
}
服务器代码被调整为不写,但只接收,客户端只发送,吞吐量增加到 27Mbit/sec,但不是 50Mbit/sec,这是以太网的一半速度,有什么帮助吗?