【发布时间】:2011-12-09 03:07:48
【问题描述】:
我正在尝试测试一个场景,其中一台服务器接受来自一个客户端的连接(每次一个),始终使用相同的端口(在服务器端和客户端)。
目的是让 1 个客户端应用程序以大于 100/min 的速率发送少量数据。显而易见的解决方案是在客户端和服务器之间建立一个始终连接的链接,但这是生产的东西,需要对已经实现的代码进行更大的更改。使用我们今天实施的解决方案,TIME_WAIT 中的连接数始终为 +-1K,我想摆脱它们。
我已经实现了一个简单的测试器,代码是:
public class Server {
public static void main(String[] args) {
ServerSocket ssock = null;
try {
ssock = new ServerSocket();
ssock.bind(new InetSocketAddress(Common.SERVER_PORT));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
while(true){
try{
Socket cSock = ssock.accept();
BufferedReader reader = new BufferedReader(new InputStreamReader(cSock.getInputStream()));
reader.readLine();
PrintWriter writer = new PrintWriter(cSock.getOutputStream());
writer.println(Common.SERVER_SEND);
writer.flush();
reader.close();
writer.close();
cSock.close();
}catch (Exception e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
}
}
}
}
public class Client {
public static void main(String[] args) throws Exception {
InetSocketAddress cliAddr = new InetSocketAddress(
InetAddress.getByName(args[0]),
Common.CLIENT_PORT);
InetSocketAddress srvAddr = new InetSocketAddress(
InetAddress.getByName(args[1]),
Common.SERVER_PORT);
for(int j=1;j<=50;j++){
Socket sock = null;
try{
sock = new Socket();
sock.setReuseAddress(true);
sock.bind(cliAddr);
sock.connect(srvAddr);
PrintWriter writer =
new PrintWriter(
sock.getOutputStream());
writer.println(Common.CLIENT_SEND);
writer.flush();
BufferedReader reader =
new BufferedReader(
new InputStreamReader(
sock.getInputStream()));
reader.readLine();
}catch (Exception e) {
System.out.println(e.getClass().getName() + ": " + e.getMessage());
System.exit(-1);
}finally{
if(sock!=null) sock.close();
System.out.println("Done " + j);
}
}
}
}
public class Common {
public static final int SERVER_PORT = 9009;
public static final int CLIENT_PORT = 9010;
public static final String CLIENT_SEND = "Message";
public static final String SERVER_SEND = "OK";
}
在 windows 主机上执行客户端和服务器时,在一个客户端执行中我总是得到
java.net.ConnectException: Connection timed out
在 linux 主机中执行客户端和服务器时,在某些客户端执行中我得到一个
java.net.NoRouteToHostException: Cannot assign requested address
我一直对这种行为感到头疼。有人可以告诉我是否可以做我想做的事,以及我做错了什么?
【问题讨论】:
-
所以你有很多连接,你想用一大块代码加速它,但它不起作用,帮我弄清楚?或者您的问题是关于您的客户端代码似乎无法找到服务器的事实?如果是后者,您应该编辑您的问题,使其显而易见。
-
我只想改进已经实现的大块代码......上面的代码只是一个测试应用程序!
-
客户端确实找到、连接并与服务器交换了一些消息,但它不像时钟那样工作。我猜这是因为这两个应用程序无法控制两端连接的关闭,一切都搞混了。
-
为什么总是使用同一个客户端端口?这是不好的做法。
-
我不相信:
java.net.NoRouteToHostException: Cannot assign requested address。我相信这是BindException。
标签: java sockets reusability connection