简体   繁体   English

linux拒绝从本地主机打开监听端口

[英]linux refuse to open listening port from localhost

I have problem to open a listening port from localhost in a heavy loaded production system. 我在重载生产系统中从本地主机打开侦听端口时遇到问题。

Sometimes some request to my port 44000 failed. 有时对我的端口44000的某些请求失败。 During that time , I checked the telnet to the port with no response, I'm wonder to know the underneath operations takes there. 在这段时间里,我没有任何响应地检查telnet到端口,我很想知道下面的操作在那儿进行。 Is the application that is listening to the port is failing to response to the request or it is some problem in kernel side or number of open files. 是正在侦听端口的应用程序无法响应请求,还是内核方面或打开文件的数量出现问题。

I would be thankful if someone could explain the underneath operation to opening a socket. 如果有人可以解释打开套接字的基本操作,我将不胜感激。

Let me clarify more. 让我澄清更多。 I have a java process which accept state full connection from 12 different server.requests are statefull SOAP message . 我有一个Java进程,它接受来自12个不同服务器的状态完全连接。请求是statefull SOAP消息。 this service is running for one year without this problem. 该服务运行一年没有出现此问题。 Recently we are facing a problem that sometimes connection from source is not possible to my server in port 44000. As I checked During that time telnet to the service is not possible even from local server. 最近,我们面临一个问题,即有时无法从源连接到端口44000上的服务器。正如我所检查的,在此期间,即使从本地服务器也无法从telnet到该服务。 But all other ports are responding good. 但是所有其他港口都反应良好。 they all are running with same user and number of allowed open files are much more bigger than this all (lsof | wc -l ) 它们都以相同的用户身份运行,并且允许打开的文件数量远大于这一切(lsof | wc -l)

As I understood there is a mechanism in application that limits the number of connection from source to 450 concurrent session, And the problem will likely takes when I'm facing with maximum number of connection (but not all the time) 据我了解,应用程序中存在一种机制,可将从源连接的数量限制为450个并发会话,当我面临最大数量的连接时(但并非始终),该问题很可能会发生。

My application vendor doesn't accept that this problem is from his side and points to os / network / hardware configuration. 我的应用程序供应商不接受这个问题是他的观点,而是指向os / network /硬件配置。 To be honest I restarted the network service and the problem solved immediately for this special port. 老实说,我重新启动了网络服务,此特殊端口的问题立即得到解决。 Any idea please??? 有什么想法吗???

Here's a quick overview of the steps needed to set up a server-side TCP socket in Linux: 以下是在Linux中设置服务器端TCP套接字所需步骤的快速概述:

  1. socket() creates a new socket and allocates system resources to it (*) socket()创建一个新套接字,并为其分配系统资源(*)
  2. bind() associates a socket with an address bind()将套接字与地址关联
  3. listen() causes a bound socket to enter a listening state listen()使绑定的套接字进入侦听状态
  4. accept() accepts a received incoming attempt, and creates a new socket for this connection. accept()接受收到的传入尝试,并为此连接创建一个新的套接字。 (*) (*)

(It's explained quite clearly and in more detail on wikipedia ). (在Wikipedia对此进行了非常清楚的解释和更详细的说明)。

(*) : These operations allocate an entry in the file descriptor table and will fail if it's full. (*) :这些操作在文件描述符表中分配一个条目,如果已​​满则将失败。 However, most applications fork and there shouldn't be issues unless the number of concurrent connections you are handling is in the thousands (see, the C10K problem ). 但是,大多数应用程序会分叉,除非您正在处理的并发连接数达到数千(请参阅C10K问题 ),否则不会出现问题。

If a call fails for this or any other reason, errno will be set to report the error condition (eg, to EMFILE if the descriptor table is full). 如果由于某种原因或任何其他原因调用失败, errno以报告错误情况(例如,如果描述符表已满,则报告给EMFILE )。 Most applications will report the error somewhere. 大多数应用程序将在某处报告错误。


Back to your application, there are multiple reasons that could explain why it isn't responding. 回到您的应用程序,有多种原因可以解释为什么它没有响应。 Without providing more information about what kind of service you are trying to set up, we can only guess. 在没有提供有关您要设置哪种服务的更多信息的情况下,我们只能猜测。 Try testing if you can telnet consistently, and see if the server is overburdened. 尝试测试是否可以一致地进行telnet ,并查看服务器是否负担过多。

Cheers! 干杯!

Your description leaves room for interpretation, but as we talked above, maybe your problem is that your terminated application is trying to re-use the same socket port, but it is still in TIME_WAIT state. 您的描述有解释的余地​​,但是,正如我们在上面所说的,也许您的问题是终止的应用程序正试图重新使用相同的套接字端口,但它仍处于TIME_WAIT状态。

You can set your socket options to reuse the same address (and port) by this way: 您可以通过以下方式设置套接字选项以重复使用相同的地址(和端口):

int srv_sock;
int i = 1;

srv_sock = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(srv_sock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));

Basically, you are telling the OS that the same socket address & port combination can be re-used, without waiting the MSL (Maximum Segment Life) timeout. 基本上,您是在告诉OS,可以重复使用相同的套接字地址和端口组合,而无需等待MSL (最大段寿命)超时。 This timeout can be several minutes. 超时时间可能是几分钟。

This does not permit to re-use the socket when it is still in use, it only applies to the TIME_WAIT state. 这不允许在套接字仍在使用时重新使用它,它仅适用于TIME_WAIT状态。 Apparently there is some minor possibility of data coming from previous transactions, though. 显然,来自先前交易的数据可能性很小。 But, you can (and should anyway) program your application protocol to take care of unintelligible data. 但是,您可以(而且应该)对您的应用程序协议进行编程,以处理难以理解的数据。

More information for example here: http://www.unixguide.net/network/socketfaq/4.5.shtml 有关更多信息,例如: http : //www.unixguide.net/network/socketfaq/4.5.shtml

Start TCP server with sudo will solve or, in case, edit firewalls rules (if you are connecting in LAN). 使用sudo启动TCP服务器将解决或编辑防火墙规则(如果您在LAN中连接)。 Try to scan ports with nmap (like with TCP Sync Handshake), or similar, to see if the port is opened to any protocol (maybe network security trunkates pings ecc.. to don't show hosts up). 尝试使用nmap(例如,使用TCP Sync Handshake)或类似方法扫描端口,以查看该端口是否对任何协议打开(也许网络安全中继ping ecc ..以便不显示主机)。 If the port isn't responsive, check privileges used by the program, check firewalls rules maybe the port is on but you can't get to it. 如果端口没有响应,请检查程序使用的特权,检查防火墙规则,也许端口已打开,但您无法访问它。

Mh I mean.. you are talking about enterprise network so I'm supposing you are on a LAN environment so you are just trying to localhost but you need it to work on LAN. 嗯,我的意思是..您在谈论企业网络,所以我假设您在LAN环境中,因此您只是尝试使用localhost,但需要它在LAN上工作。 Anyway if you just need to open localhost port check privileges and routing, try to "tracert" and see what happens and so on... 无论如何,如果您只需要打开localhost端口检查特权和路由,请尝试“ tracert”并查看会发生什么,依此类推...

Oh and check if port is used by a higher privilege service or deamon. 哦,检查端口是否被更高特权的服务或守护进程使用。

Anyway I see now that this is a 2014 post, np gg nice coding byebye 无论如何,我现在看到的是这是2014年的帖子,NP gg nice编码byebye

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

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