java 获取网络带宽_Java开发网 - 一个测网络延迟和带宽的程序,发10M的数据有问题...
我的想法是客戶端發10M的string給服務器,服務器把收到的數據返回給客戶端,
客戶端記下發送的時刻和收完數據的時刻,兩者相減再除2就是傳輸用的時間.
發1M沒有問題,現在要發10M,就循環10次,結果服務器和客戶端都陷入了循環,
不知問題出在哪,請大家指點,謝謝!
客戶端程序如下
import java.io.*;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLSocketFactory;
public
class EchoClient
{
public static void main(String [] args)
{
String str="0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",sendstr=null;
for(int i=0;i<10;i++)
sendstr+=str;
sendstr+="12345678123456781234";
String realsendstr=null;
for(int k=0;k<1000;k++)
realsendstr+=sendstr;
try
{
System.setProperty("javax.net.ssl.trustStore","clienttrust");
SSLSocketFactory sslsocketfactory = (SSLSocketFactory)SSLSocketFactory.getDefault();
SSLSocket sslsocket = (SSLSocket)sslsocketfactory.createSocket("localhost", 9999);
InputStream ins= sslsocket.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(ins);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
OutputStream outs=sslsocket.getOutputStream();
PrintStream ps=new PrintStream(outs);
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outs);
BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
String recvstr=null;
int len=0;
System.out.println("the size of sendstr is "+realsendstr.length());
long sendtime = System. currentTimeMillis();
for(int m=0;m<10;m++)
ps.println(realsendstr);
ps.println("");
while((recvstr=bufferedreader.readLine())!=null) {
len+=recvstr.length();
if(recvstr.equals("")) break;
}
long recvtime = System. currentTimeMillis();
long costtime= recvtime-sendtime;
System.out.println("the delay is "+costtime+" ms");
System.out.println("recv len:"+len);//+" data:["+new String(buff,0,len)+"]");
double f=len*2*1000/(costtime*1024);
System.out.print("the speed is ");
System.out.print(f);
System.out.println(" K/s");
ps.close();
bufferedwriter.close();
bufferedreader.close();
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}
服務器端如下:
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.*;
import javax.net.ssl.SSLSocket;
import javax.net.ssl.SSLServerSocket;
import javax.net.ssl.SSLServerSocketFactory;
public
class EchoServer
{
public static void main(String [] arstring)
{
try
{
SSLServerSocketFactory sslserversocketfactory =
(SSLServerSocketFactory)SSLServerSocketFactory.getDefault();
SSLServerSocket sslserversocket =
(SSLServerSocket)sslserversocketfactory.createServerSocket(9999);
while(true) {
SSLSocket sslsocket = (SSLSocket) sslserversocket.accept();
InputStream inputstream = sslsocket.getInputStream();
InputStreamReader inputstreamreader = new InputStreamReader(inputstream);
BufferedReader bufferedreader = new BufferedReader(inputstreamreader);
OutputStream outs=sslsocket.getOutputStream();
PrintStream ps=new PrintStream(outs);
OutputStreamWriter outputstreamwriter = new OutputStreamWriter(outs);
BufferedWriter bufferedwriter = new BufferedWriter(outputstreamwriter);
String str=null;
while((str=bufferedreader.readLine())!=null) {
ps.println(str);
if(str.equals("")) break;
}
System.out.println("recv and send over");
bufferedwriter.close();
bufferedreader.close();
sslsocket.close();
}
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
}
總結
以上是生活随笔為你收集整理的java 获取网络带宽_Java开发网 - 一个测网络延迟和带宽的程序,发10M的数据有问题...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中循环结构关键字_Pytho
- 下一篇: java 反转 控制 注入_控制反转和依