简单说想实现一个本地网络 p2p 通信程序,用户在控制台输入命令,程序开始检测本地网络 9990 这个端口,如果 9990 为空闲端口,则在此建立监听。如果 9990 位置已经有其他用户使用本程序建立好的监听,则 connect 加入。如果端口被其他程序占用,报错。 那么该如何得到端口状态呢?谢谢解答!!!!
![]() | 1 mrcn 2020-01-13 09:23:42 +08:00 via Android 记得是如果占用了,listen 还是 bind 会出错,返回值-1。 |
![]() | 2 chu1337 2020-01-13 10:41:53 +08:00 bind: ``` RETURN VALUES Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and the global integer variable errno is set to indicate the error. ``` |
![]() | 3 paoqi2048 2020-01-13 11:00:46 +08:00 bind error: EADDRINUSE: The given address is already in use. |