简体   繁体   English

Java NIO中的文件打开错误过多

[英]Too Many Files Open error in java NIO

Hi I have created a socket and client program using java NIO. 嗨,我已经使用Java NIO创建了一个套接字和客户端程序。 My server and client are on different computers ,Server have LINUX OS and CLIENT have WINDOWS OS. 我的服务器和客户端在不同的计算机上,服务器具有LINUX OS,客户端具有WINDOWS OS。 Whenever I have created 1024 sockets on client my client machines supports but in server I got too many files open error. 每当我在客户端上创建1024个套接字时,我的客户端计算机就支持,但是在服务器中,我得到了太多的文件打开错误。 So How to open 15000 sockets without any error in server. 因此,如何在服务器中打开15000个套接字而没有任何错误。 Or is there any other way to connect with 15000 clients at the same time? 还是有其他方法可以同时连接15000个客户端?

Thanks Bapi 谢谢Bapi

Ok, questioning why he needs 15K sockets is a separate discussion. 好的,质疑他为什么需要15K插槽是一个单独的讨论。

The answer is that you are hitting the user's file descriptor limit. 答案是您达到了用户的文件描述符限制。

Log with the user you will use in the listener and do $ulimit -n to see the current limit. 使用您将在侦听器中使用的用户登录,然后执行$ ulimit -n以查看当前限制。

Most likely 1024. 最有可能是1024。

Using root edit the file /etc/security/limits.conf 使用root编辑文件/etc/security/limits.conf

and set -> 并设置->

{username} soft nofile 65536
{username} hard nofile 65536

65536 is just a suggestion, you will need to figure that out from your app. 65536只是一个建议,您需要从您的应用中弄清楚。

Log off, log in again and recheck with ulimit -n, to see it worked. 注销,再次登录并使用ulimit -n重新检查,以查看它是否有效。

Your are probably going to need more than 15 fds for all that. 所有这些可能需要超过15 fds。 Monitor your app with lsof. 使用lsof监视您的应用程序。

Like this: 像这样:

$lsof -p {pid}   <- lists all file descriptors
$lsof -p {pid}  | wc -l    <- count them

By the way, you might also hit the system wide fd limit, so you need to check it: 顺便说一句,您可能还会达到系统范围的fd限制,因此需要检查一下:

$cat /proc/sys/fs/file-max

To increase that one, add this line to the /etc/sysctl.conf 要增加该行,请将此行添加到/etc/sysctl.conf中

#Maximum number of open FDs
fs.file-max = 65535

Why do you need to have 15000 sockets on one machine? 为什么在一台机器上需要15000个插座? Anyway, look at ulimit -n 无论如何,看看ulimit -n

If you're going to have 15,000 clients talking to your server (and possibly 200,000 in the future according to your comments) then I suspect you're going to have scalability problems servicing those clients once they're connected (if they connect). 如果您将有15,000个客户端与您的服务器通信(根据您的评论,将来可能有200,000个),那么我怀疑一旦这些客户端连接(如果它们已连接),将无法为他们提供服务。

I think you may need to step back and look at how you can architect your application and/or deployment to successfully achieve these sort of numbers. 我认为您可能需要退后一步,看看如何构建应用程序和/或部署才能成功实现这些数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM