如何使用c检查一个端口是否在linux中是免费的?

时间:2022-09-11 16:54:40

i have never written anything like it, how do i check things like if a port is empty using c program in Linux environment thanks a lot.

我从来没有写过类似的东西,我如何在Linux环境中使用c程序检查端口是否为空,非常感谢。

ps looking for a way, by not using bind or connect and checking if it failed.

ps寻找一种方法,不使用bind或connect并检查它是否失败。

edit i cant use bind or connect, looking for faster way to find 3k ports that are free in a row

编辑,我不能使用绑定或连接,寻找更快的方式找到3k端口,在一个行是免费的

4 个解决方案

#1


2  

Better way is to use next free port,You can also use 0 port bind will use the next available port.

更好的方法是使用下一个空闲端口,您也可以使用0端口绑定将使用下一个可用端口。

You can get port selected by bind() by following code

可以通过以下代码获得bind()选择的端口

struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &len) != -1)
  printf("port number %d\n", ntohs(sin.sin_port)); 

Also refer How to bind to any available port? for more inforamtion

还请参考如何绑定到任何可用端口?更多inforamtion

#2


0  

Run following command using system() or popen()

使用system()或popen()运行以下命令

netstat -antu

It will give list of all used port of your machine. You need to parse output of that command and then you will have list of all busy port.

它将给出您的机器的所有使用端口的列表。您需要解析该命令的输出,然后您将拥有所有繁忙端口的列表。

#3


0  

How about you using bind() directly, and if it doesn't succeed you can try another port.

直接使用bind()如何,如果不能成功,您可以尝试另一个端口。

You just checked, that a port was free, but someone already used it would be a race condition,so checking if a port is free and then binding it is not possible

您刚刚检查过,一个端口是空闲的,但是有人已经使用了它,所以检查一个端口是否空闲,然后绑定它是不可能的

You can also read /proc/net/tcp for help but the race condition can still occur.

您还可以读取/proc/net/tcp以获得帮助,但是仍然可以出现竞争条件。

#4


-1  

i had same problem the question is do you need to check just one port or many ports

我也有同样的问题,问题是你需要只检查一个端口还是多个端口

If you need to check just one or few use bind, if it works then its free (and dont forget to free the socket)

如果你只需要检查一个或几个使用bind,如果它有效,那么它就是free(不要忘记释放socket)

if like me you need to check many ports, then what worked for me is run system('netstat -tulpn') and redirect output to a file/variable and then on this info search for ":{yourport}"

如果像我一样,您需要检查许多端口,那么对我有效的是运行系统('netstat -tulpn'),并将输出重定向到文件/变量,然后在此信息搜索":{yourport}"

worked for me

为我工作

ps if like me you need to keep them free, tell your computer not to randomlly allocate ports in that area

ps:如果你像我一样需要免费,告诉你的电脑不要在那个区域随机分配端口

#1


2  

Better way is to use next free port,You can also use 0 port bind will use the next available port.

更好的方法是使用下一个空闲端口,您也可以使用0端口绑定将使用下一个可用端口。

You can get port selected by bind() by following code

可以通过以下代码获得bind()选择的端口

struct sockaddr_in sin;
socklen_t len = sizeof(sin);
if (getsockname(sock, (struct sockaddr *)&sin, &len) != -1)
  printf("port number %d\n", ntohs(sin.sin_port)); 

Also refer How to bind to any available port? for more inforamtion

还请参考如何绑定到任何可用端口?更多inforamtion

#2


0  

Run following command using system() or popen()

使用system()或popen()运行以下命令

netstat -antu

It will give list of all used port of your machine. You need to parse output of that command and then you will have list of all busy port.

它将给出您的机器的所有使用端口的列表。您需要解析该命令的输出,然后您将拥有所有繁忙端口的列表。

#3


0  

How about you using bind() directly, and if it doesn't succeed you can try another port.

直接使用bind()如何,如果不能成功,您可以尝试另一个端口。

You just checked, that a port was free, but someone already used it would be a race condition,so checking if a port is free and then binding it is not possible

您刚刚检查过,一个端口是空闲的,但是有人已经使用了它,所以检查一个端口是否空闲,然后绑定它是不可能的

You can also read /proc/net/tcp for help but the race condition can still occur.

您还可以读取/proc/net/tcp以获得帮助,但是仍然可以出现竞争条件。

#4


-1  

i had same problem the question is do you need to check just one port or many ports

我也有同样的问题,问题是你需要只检查一个端口还是多个端口

If you need to check just one or few use bind, if it works then its free (and dont forget to free the socket)

如果你只需要检查一个或几个使用bind,如果它有效,那么它就是free(不要忘记释放socket)

if like me you need to check many ports, then what worked for me is run system('netstat -tulpn') and redirect output to a file/variable and then on this info search for ":{yourport}"

如果像我一样,您需要检查许多端口,那么对我有效的是运行系统('netstat -tulpn'),并将输出重定向到文件/变量,然后在此信息搜索":{yourport}"

worked for me

为我工作

ps if like me you need to keep them free, tell your computer not to randomlly allocate ports in that area

ps:如果你像我一样需要免费,告诉你的电脑不要在那个区域随机分配端口