我的想法是客户端发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();
}
}
}