socket bind 随机端口
https://www.cprogramming.com/code_blocks/
这个地址可以下载c, c++的编译器,在windows下可以用的 IDE.
bind到端口0上,系统就会自动分配,但是可能不是随机的,而是根据系统的算法。
也可以用rand算个随机数出来,然后bind,如果bind不成功就取下一个随机数。
At this point, you can reach for the port 0 trick: on both Windows and Linux, if you bind a socket to port 0, the kernel will assign it a free port number somewhere above 1024.
原文:https://blog.csdn.net/Borntodieee/article/details/78939923
https://cboard.cprogramming.com/linux-programming/89244-bind-chosing-port.html
I set the server.sin_port=0 and then I call the bind function that doesn't return any error:
Where can I get the port that was asigned by the bind function?
int sd; //server descriptor
struct sockaddr_in server;
bzero(&server,sizeof(server));
server.sin_family=AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=htons(0); if(bind(sd,(struct sockaddr*)&server,sizeof(struct sockaddr))==-1); // error printf("%d\n",ntohs(server.sin_port)); //is still 0
怎么获取系统真正分配的随机端口呢?

All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Don't use bzero -- it's been obsolete for decades. It makes you look like a stupid hacker (I'm not saying you are one -- but let me guess, you've been learning socket programming by reading hacking sites, right?) Use memset() instead.
Also, why do you need to know the port number? Why are you binding without specifying the port, anyway?
------------------------------------------------------------------------------------------------------------------
述
TCP/IP 协议中的端口在报头中占2个字节即16位,范围是从0-65535。端口号用来表示和区别网络中的不同应用程序
端口分类
(1)公认端口(Well Known Ports):0-1023之间的端口号,也叫Well Known ports。这些端口由 IANA 分配管理。IANA 把这些端口分配给最重要的一些应用程序,让所有的用户都知道,当一种新的应用程序出现后,IANA必须为它指派一个公认端口。
常用的公认端口有:
FTP : 21
TELNET : 23
SMTP : 25
DNS : 53
TFTP : 69
HTTP : 80
SNMP : 161
(2)注册端口(Registered Ports):从1024-49151。是公司和其他用户向互联网名称与数字地址分配机构(ICANN)登记的端口号,利用因特网的传输控制协议(TCP)和用户数据报协议(UDP)进行通信的应用软件需要使用这些端口。在大多数情况下,这些应用软件和普通程序一样可以被非特权用户打开。
(3)客户端使用的端口号:49152~65535.这类端口号仅在客户进程运行时才动态选择,因此又叫做短暂端口号。被保留给客户端进程选择暂时使用的。也可以理解为,客户端启动的时候操作系统随机分配一个端口用来和服务器通信,客户端进程关闭下次打开时,又重新分配一个新的端口。
总结
端口就像一道门,外部可以通过不同的端口和本机上不同服务的进程进行交流。而IP 地址和端口标识了接入互联网主机的唯一 一个进程。
---------------------
socket bind 随机端口的更多相关文章
- Linux下Socket编程的端口问题( Bind error: Address already in use )
Linux下Socket编程的端口问题( Bind error: Address already in use ) 在进行linux网络编程时,每次修改了源代码并再次编译运行时,常遇到下面的地使用错误 ...
- java.lang.Exception: Socket bind failed: [730048] 端口被占用
错误提示如下: org.apache.coyote.http11.Http11AprProtocol init 严重: Error initializing endpoint java.lang.Ex ...
- java.lang.Exception: Socket bind failed 服务器端口冲突-->修改端口
需要修改三个端口号:%apache_tomcat6%/conf/server.xml 四月 11, 2014 11:39:25 上午 org.apache.catalina.core.AprLifec ...
- java.lang.Exception: Socket bind failed: [730013] An attempt was made to acc
在CMD命令行中启动运行startup.bat,启运程序总是闪退,查看日志发现如下错误: 26-Jan-2016 18:12:34.463 SEVERE [main] org.apache.coyot ...
- java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í??
严重: Error starting endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???? ...
- socket bind详解
http://www.cnblogs.com/nightwatcher/archive/2011/07/03/2096717.html 在最开始接触bind的时候,只是在写基于tcp的server端的 ...
- java.lang.Exception: Socket bind failed
1.错误描述 严重: Failed to initialize end point associated with ProtocolHandler ["http-apr-8080" ...
- 【tomcat】启动报错:Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"] java.lang.Exception: Socket bind failed 和java.net.BindException: Address already in use: JVM_Bind错误解决
背景:[新手] 将开发机子上的Tomcat连同其中的项目,一起拷贝到服务器上,启动tomcat的start.bat,然后报错如下: 问题1: Failed to initialize end poin ...
- java.lang.Exception: Socket bind failed: [730048]
严重: Error initializing endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/ ...
随机推荐
- Revit二次开发示例:CancelSave
在Revit程序中注册文件操作事件,保存新建或打开文件的信息.当保存时,如果当前文件内容和之前的一致时,则弹出对话框提示并取消保存.对话框中有一个功能链接,点击可打开插件所在目录. #region N ...
- Centos7下shell脚本添加开机自启动
添加开机自启脚本,注意都需要用绝对路径 psubscribe.sh脚本中的内容: nohup /usr/bin/php -f /data/aliyun51015cn/redisChannel/psub ...
- luoguP4492 [HAOI2018]苹果树 组合计数 + dp
首先,每个二叉树对应着唯一的中序遍历,并且每个二叉树的概率是相同的 这十分的有用 考虑\(dp\)求解 令\(f_i\)表示\(i\)个节点的子树,根的深度为\(1\)时,所有点的期望深度之和(乘\( ...
- 20162303石亚鑫 第十二周hash补充博客
要求 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,65,(你的8位学号相加 ...
- python开发_tkinter_图形随鼠标移动
做这个东西的时候,灵感源自于一个js效果: 两个眼睛随鼠标移动而移动 运行效果: =============================================== 代码部分: ===== ...
- CentOS7.0安装Nginx-1.12.0
一.安装准备 首先由于nginx的一些模块依赖一些lib库,所以在安装nginx之前,必须先安装这些lib库,这些依赖库主要有g++.gcc.openssl-devel.pcre-devel和zlib ...
- Python学习笔记(四):字符串的学习
总结的内容: 1.字符串常用的方法 2.Python字符串格式化 3.Python字符串转义字 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...
- visio2013 激活工具,仅供交流学习
visio2013激活软件 环境是 win7, 64 bit 装了 visio 2013 , 可以却不能用它来画图,在网上找了一些破解工具,大都不能解决问题.网上不靠谱的广告型文章太多了 所幸,终于找 ...
- Notepad++源代码阅读——窗口元素组织与布局
1.1 前言 这两天在看notepad++ 1.0版本的源代码.看了许久终于把程序的窗口之间的关系搞清楚了现在把其组织的要点写于此,希望对大家有所帮助. 1.2 窗口元素之间的关系 Notepad++ ...
- C++使用autoreconf -vi出现error: possibly undefined macro: AC_LIBTOOL_WIN32_DLL If this token and others are legitimate, please use m4_pattern_allow. See the Autoconf documentation.
安装这个:libtool libsysfs yum install -y libtool libsysfs 参考: https://blog.csdn.net/yusiguyuan/article/ ...