Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java SE 综合讨论区  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 一个测网络延迟和带宽的程序,发10M的数据有问题
wordby





发贴: 8
积分: 0
于 2006-06-23 12:01 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
我的想法是客户端发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();
}
}
}


why edited on 2006-06-23 17:11


Hibernate开发指南

话题树型展开
人气 标题 作者 字数 发贴时间
6226 一个测网络延迟和带宽的程序,发10M的数据有问题 wordby 3828 2006-06-23 12:01
5224 Re:一个测网络延迟和带宽的程序,发10M的数据有问题 cxp108 565 2006-06-23 17:06
5077 Re:一个测网络延迟和带宽的程序,发10M的数据有问题 wordby 427 2006-06-25 19:36
5294 Re:一个测网络延迟和带宽的程序,发10M的数据有问题 cxp108 115 2006-06-28 09:04

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   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