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] ?????????×???(Э?é/ ...
随机推荐
- 设计模式 结构型模式 外观模式(Facade Pattern)
在软件开发过程中,客户端程序经常会与复杂系统的内部子系统进行耦合,从而导致客户端程序随着子系统的变化而变化. 这时为了将复杂系统的内部子系统与客户端之间的依赖解耦,从而就有了外观模式,也称作 ”门面“ ...
- 01-学前入门VS各个组成部分
1)快捷打开运行窗口(Windows+R) 里面输入devenv命令快捷打开VS 2)解决方案,项目及类之间的关系(解决方案包含项目-项目包含-类) 例如可以这样比喻: 解决方案:相当于公司 项目:相 ...
- Ubuntu安装redis和redis-php扩展
通过apt-get安装的redis使用方法 sudo apt-get install redis-server sudo apt-get install php-redis vim /etc/redi ...
- android viewHolder static 静态
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 不是静态内部类 会 持有 外部类的 引用. 就像经常自定义的 适配器 类 作为内部类 ...
- ARC 067 E - Grouping
题面在这里! 很显然是个暴力dp. 我们先枚举一下 队伍人数的种类,然后再逆序枚举一下dp数组里的总人数(顺序就会算重),最后枚举一下这种队伍的数量,之后就可以O(1)算方案了. 具体的,O(1)算方 ...
- BZOJ.2527.[POI2011]MET-Meteors(整体二分)
题目链接 BZOJ 洛谷 每个国家的答案可以二分+求前缀和,于是可以想到整体二分. 在每次Solve()中要更新所有国家得到的值,不同位置的空间站对应不同国家比较麻烦. 注意到每次Solve()其国家 ...
- 【BZOJ】4260: Codechef REBXOR【Trie树】【前后缀异或最大】
4260: Codechef REBXOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2218 Solved: 962[Submit][Stat ...
- bzoj 3283 扩展BSGS + 快速阶乘
T2 扩展BSGS T3 快速阶乘 给定整数n,质数p和正整数c,求整数s和b,满足n! / pb = s mod pc 考虑每次取出floor(n/p)个p因子,然后将问题转化为子问题. /*** ...
- Codeforces Round #352 (Div. 1) B. Robin Hood 二分
B. Robin Hood 题目连接: http://www.codeforces.com/contest/671/problem/B Description We all know the impr ...
- Codeforces Round #257 (Div. 2) A. Jzzhu and Children
A. Jzzhu and Children time limit per test 1 second memory limit per test 256 megabytes input standar ...