燃烧至死 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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题