燃烧至死 2016-05-06 08:41 采纳率: 0%
浏览 1585

自己用java写的简单的http服务器,该怎么测试最大连接的socket数量?

服务器监听端口代码:
while (true) {
try {
Socket connection = server.accept();
FileUtils.writeLocal(TimeUtils.getCurrentTime());
FileOutputStream fos = new FileOutputStream("serverlog.txt", true);
threadcount++;
fos.write((connection.getInetAddress() + ":" + connection.getPort() + " threadcount: " + threadcount)
.getBytes());
fos.write("\r\n".getBytes());
fos.flush();
fos.close();
pool.submit(new HTTPHandler(connection, header));
} catch (IOException e) {
logger.log(Level.WARNING, "Exception accepting connection", e);
} catch (RuntimeException e) {
logger.log(Level.SEVERE, "Unexpected error", e);
}
}

客户端链接服务器代码:
for (; j < 105000; j++) {
new Thread() {
public void run() {
try {
sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
startSocket(j);
};
}.start();
}

发现启动服务器后,再启动客户端,总会出现connetion refused:connect的异常。服务器 fos.write((connection.getInetAddress() + ":" + connection.getPort() + " threadcount: " + threadcount)将连接数写入文件,但是这个连接的数量不稳定,怎么办?

  • 写回答

1条回答 默认 最新

  • devmiao 2016-05-06 22:48
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型