我怎么用C,一个端口是免费使用找? [英] how do I find in C that a port is free to use?

查看:121
本文介绍了我怎么用C,一个端口是免费使用找?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该操作系统是Linux操作系统。
我有一个服务器进程,可以改变它的端口实时。不过,我想提前知道,如果一个端口绑定之前是免费的。

方案:服务器绑定本地主机:5000和接收到本地主机绑定的请求:6000。服务器必须检查该端口是免费的。这个问题的目的为提供来检查,如果一个端口是免费的或者不是一个常规的答案。

有关记录,我编辑我用code片段来检查,如果一个端口是免费使用的问题。这并不意味着它会被使用。以下问题的答案的code,如果该端口可现在,它不使用它。打开一个插座,检查是否绑定返回EADDRINUSE和关闭套接字。

 的#include<&iostream的GT;
#包括LT&; SYS / socket.h中>
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; ARPA / inet.h>
#包括LT&;&errno.h中GT;INT主(INT ARGC,字符** argv的){    结构SOCKADDR_IN serv_addr;
    如果(的argc 2)
        返回0;
    INT端口=的atoi(ARGV [1]);    INT的sockfd =插座(AF_INET,SOCK_STREAM,0);
    如果(的sockfd℃,){
        的printf(套接字错误。\\ n);
        返回0;
    }其他{
        的printf(开FD%d个\\ N的sockfd);
    }     bzero((字符*)及serv_addr,sizeof的(serv_addr));
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(端口);
     如果(绑定(的sockfd,(结构sockaddr *)及serv_addr,sizeof的(serv_addr))小于0){        如果(错误== EADDRINUSE)
        {
            的printf(该端口不可用已给其他进程\\ n);
        }其他{
            的printf(无法绑定到过程(%D)%S \\ n,错误号,字符串错误(错误));
        }
    }    如果(接近(的sockfd)小于0){
        输出(没有关闭的fd:%S \\ n字符串错误(错误));
        返回错误号;
    }    返回0;
}

下面是一些样品运行(部分输出)

  [{庆典1051} {} 51]:[〜/ some_sources / checkbind] :: ./ a.out的41067
端口不可用。已经到其他进程
[bash的{1052} {} 52]:[〜/ some_sources / checkbind] :: ./ a.out的22
无法绑定到过程(13)权限被拒绝
[bash的{1053} {} 53]:[〜/ some_sources / checkbind] :: ./ a.out的22000
开业FD 3


解决方案

这是一个明显的竞争条件,因为其他您系统上的进程可能会并行绑定端口。所以,你找到任何解决方案将是不完美的,你会的还是的需要只写它根据尝试绑定(),如果它无法选择一个新的端口号,然后再试一次的做法。

The OS is Linux. I have a server process that can change its port realtime. However I would like to know in advance if a port is free before binding.

Scenario: Server binds localhost:5000 and receives a request to bind at localhost:6000. The server has to check if the port is free. This question seeks for answers that provide a routine that checks if a port is free or not.

For the record, I am editing my question with a code snippet that checks if a port is free to use. This does not mean that it will be used. The code below answer to the question "if the port is available right now", it does not use it. Opens a socket, check if bind returns EADDRINUSE and closes the socket.

#include <iostream>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <errno.h>

int main(int argc, char **argv) {

    struct sockaddr_in serv_addr;
    if( argc < 2 )
        return 0;
    int port = atoi(argv[1]);

    int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if( sockfd < 0 ) {
        printf("socket error\n");
        return 0;
    } else {
        printf("Opened fd %d\n", sockfd);
    }

     bzero((char *) &serv_addr, sizeof(serv_addr));
     serv_addr.sin_family = AF_INET;
     serv_addr.sin_addr.s_addr = INADDR_ANY;
     serv_addr.sin_port = htons(port);
     if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {

        if( errno == EADDRINUSE )
        {
            printf("the port is not available. already to other process\n");
        } else {
            printf("could not bind to process (%d) %s\n", errno, strerror(errno));
        }
    }

    if (close (sockfd) < 0 ) {
        printf("did not close fd: %s\n", strerror(errno));
        return errno;
    }

    return 0;


}

Here are some sample runs (partial outputs)

[bash{1051}{51}]:[~/some_sources/checkbind]::./a.out 41067
the port is not available. already to other process
[bash{1052}{52}]:[~/some_sources/checkbind]::./a.out 22
could not bind to process (13) Permission denied
[bash{1053}{53}]:[~/some_sources/checkbind]::./a.out 22000
Opened fd 3

解决方案

This is an obvious race condition, since other processes on your system might be binding to ports in parallel. So, any solution you find will be imperfect, and you will still need to just write it according to the "try to bind(), if it fails pick a new port number and try again" approach.

这篇关于我怎么用C,一个端口是免费使用找?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆