Topic: 一个测网络延迟和带宽的程序,发10M的数据有问题

  Print this page

1.一个测网络延迟和带宽的程序,发10M的数据有问题 Copy to clipboard
Posted by: wordby
Posted on: 2006-06-23 12:01

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

2.Re:一个测网络延迟和带宽的程序,发10M的数据有问题 [Re: wordby] Copy to clipboard
Posted by: cxp108
Posted on: 2006-06-23 17:06

What are you talking about?What are you talking about?What are you talking about?What are you talking about?What are you talking about?What are you talking about?汗啊.........................

强烈建议楼主加强J2SE的基础知识,特别是虚拟机的一些特性!


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;



这段程序会把你JVM的内存飞速地榨干啊!

如果你需要频繁增长你的String 请使用StringBuffer

另外,String 并不一定是 :一个字符 = 1字节 , 你这样的计算方式有明显的不妥,而且JVM组装String
也需要不少时间,这在一定程度上都会影响程序的准确率。

楼主为什么不使用传输 byte[]的方式来计算,不是更加准确么?

3.Re:一个测网络延迟和带宽的程序,发10M的数据有问题 [Re: wordby] Copy to clipboard
Posted by: wordby
Posted on: 2006-06-25 19:36

楼上说得很有道理,现在改成
byte[] buff=new byte[1024];
for(int i=0;i<1024;i++)
buff[i]='1';
比如发10K的数据,
客户端代码如下:
for(int k=0;k<10;k++) {
    outs.write(buff);
    len=ins.read(buff);
  }
服务器端代码如下:
for(k=0;k<10;k++) {
len=ins.read(buff);
outs.write(buff);
}
运行后客户端报错:
java.net.SocketException: Software caused connection abort: recv failed
就是read的那一行报错,
怎么解决这个问题?用tcp还是udp呢?

4.Re:一个测网络延迟和带宽的程序,发10M的数据有问题 [Re: wordby] Copy to clipboard
Posted by: cxp108
Posted on: 2006-06-28 09:04

如果是测试网络实际速度的话当然用TCP/IP,因为大部分软件还是用TCP。若测试理论速度可以用UDP

至于那个Excpetion,我猜测可能是你的OutputStream没有执行flush()或你的发送和接收代码不对应所至。


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923