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] ?????????×???(Э?é/ ...
随机推荐
- HAproxy 代理技术原理探究
HAproxy 技术分享 简介 HAProxy是一款提供高可用性.负载均衡以及基于TCP(第四层)和HTTP(第七层)应用的代理软件 Features 1.免费 2.能够做到4层以上代理 3.高性能 ...
- pycharm 激活相关
https://blog.csdn.net/u014044812/article/details/78727496dfasdfasdfdfaasdkjfhaskljdfhkajsdhfljahsdfk ...
- Java实现杨辉三角形
Java实现杨辉三角形 一.源代码:YFTriangle.java package cn.com.zfc.day009; import java.util.Scanner; /** * 打印杨辉三角形 ...
- SPOJ GSS
GSS1 题目大意:给出一个数列,多次询问区间最长连续子段和 题解:线段树维护区间最长连续子段和gss,区间从最左元素开始的最长连续子段和lgss 区间以最右元素为结尾的最长连续子段和rgss以及区间 ...
- PHP--SPL扩展学习笔记
一. SPL是干嘛的 SPL是用于解决典型问题(standard problems)的一组接口与类的集合. 数据结构: .实现双向列表 SplDoublyLinkedList implements I ...
- Codeforces Round #248 (Div. 1) B. Nanami's Digital Board 暴力 前缀和
B. Nanami's Digital Board 题目连接: http://www.codeforces.com/contest/434/problem/B Description Nanami i ...
- div模拟selection标签 下拉列表
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- blkblock 2工具
http://blog.yufeng.info/archives/tag/blktrace
- erlang debug
http://www.cnblogs.com/goldli/archive/2011/03/08/1976998.html erlang 模块文件 helloworld.erl 文件内容: -modu ...
- ARCGIS将WGS84坐标投影到高斯平面
将WGS84坐标投影到平面,一般採用的是UTM(通用横轴莫卡托投影).该方式多用于美国地区,而我国多用北京54和西安80高斯克吕格投影坐标.假如我们想把影像採用高斯克吕格投影到在平面上,而ARCGIS ...