Linux C Socket 编程 如何知道(检测)某个端口是否已建立监听?

2020-01-13 09:01:43 +08:00
 b00tyhunt3r

简单说想实现一个本地网络 p2p 通信程序,用户在控制台输入命令,程序开始检测本地网络 9990 这个端口,如果 9990 为空闲端口,则在此建立监听。如果 9990 位置已经有其他用户使用本程序建立好的监听,则 connect 加入。如果端口被其他程序占用,报错。 那么该如何得到端口状态呢?谢谢解答!!!!

1840 次点击
所在节点    程序员
3 条回复
mrcn
2020-01-13 09:23:42 +08:00
记得是如果占用了,listen 还是 bind 会出错,返回值-1。
chuhades
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.
```
paoqi2048
2020-01-13 11:00:46 +08:00
bind error: EADDRINUSE: The given address is already in use.

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/637374

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX