socket bind 随机端口

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

------------------------------------------------------------------------------------------------------------------


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 地址和端口标识了接入互联网主机的唯一 一个进程。
---------------------

原文地址:https://www.cnblogs.com/oxspirt/p/10118348.html

时间: 2024-07-31 16:33:15

socket bind 随机端口的相关文章

Linux下Socket编程的端口问题( Bind error: Address already in use )

Linux下Socket编程的端口问题( Bind error: Address already in use ) 在进行linux网络编程时,每次修改了源代码并再次编译运行时,常遇到下面的地使用错误: Bind error: Address already in use 虽然用Ctrl+C强制结束了进程,但错误依然存在,用netstat -an |grep 5120和ps aux |grep 5120都还能看到刚才用Ctrl+C“强制结束”了的进程,端口还是使用中,只好每次用kill结束进程,

java.lang.Exception: Socket bind failed: [730048]

严重: Error initializing endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.init(AprEndpoint.java:576) at org.apache.coyote.http11.Http11AprProtocol.init(Http11AprP

java.lang.Exception: Socket bind failed

1.错误描述 严重: Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"] java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.bind(AprEndpoint.java:4

启动tomcat报错 java.lang.Exception: Socket bind failed

严重: Error initializing endpointjava.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.init(AprEndpoint.java:647) at org.apache.coyote.http11.Http11AprProtocol.init(Http11AprPr

Tomcat 的 socket bind failed的解决方法

1.socket bind failed的原因一般是Tomcat端口被占用,一般解除端口占用就行了 解决步骤: 1.win+R打开运行,输入cmd,进入dos界面: 2.输入netstat -ano |findstr 8080  查看占用端口8080,每个人的端口号可能占用的不同,要根据实际情况来看 通过 taskkill /f /pid 4228 杀死进程为4228的应用:

linux 网络编程 socket bind failed 问题解决

今天写一个简单的socket网络通讯的程序的时候,用ctrl+c结束服务器端程序之后,再次启动服务器出现了bind failed:the address already in use的错误.在网上查了一下以后找到了原因,在此记录一下.这个IBM的官网上说到了这一点:http://www.ibm.com/developerworks/cn/linux/l-sockpit/.详细介绍如下: bind 普遍遭遇的问题是试图绑定一个已经在使用的端口.该陷阱是也许没有活动的套接字存在,但仍然禁止绑定端口(

socket bind error:Socket operation on non-socket

问题:编译Socket时候出现socket bind error:Socket operation on non-socket时间:2014-7-14解决:注意“==”和“=”的优先级,前者是大于后者的,故而在处理一些表达式的时候,注意加括号注意:注意下面标注闪光字体的地方 #include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <netdb.

java.lang.Exception: Socket bind failed: [730048] ?????????&#215;???(Э?&#233;/???????/???)????&#237;??

严重: Error starting endpoint java.lang.Exception: Socket bind failed: [730048] ?????????×???(Э?é/???????/???)????í?? at org.apache.tomcat.util.net.AprEndpoint.init(AprEndpoint.java:649) at org.apache.tomcat.util.net.AprEndpoint.start(AprEndpoint.java:

The Ephemeral Port Range (各操作系统随机端口范围)

The Ephemeral Port Range A TCP/IPv4 connection consists of two endpoints, and each endpoint consists of an IP address and a port number.  Therefore, when a client user connects to a server computer, an established connection can be thought of as the