gpt4 book ai didi

linux - 我们能否以某种自动方式设置用于监听的端口号而不是对其进行硬编码?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:10:17 28 4
gpt4 key购买 nike

<分区>

在网上找到的典型客户端服务器程序示例中,我们可以看到以下指定端口号的硬编码方式,该端口号实际上是将用于处理传入消息的进程的进程 ID。

我们能否以某种自动方式而不是硬编码来设置用于监听的端口号?我的意思是我们怎么总是事先知道端口号?

  serverAddr.sin_family         = AF_INET;
/* Set port number, using htons function to use proper byte order
Port number is the process id of the process which will be used to
handle the incomming messages.
*/
serverAddr.sin_port = htons (7891);
/* Set IP address to localhost */
serverAddr.sin_addr.s_addr = inet_addr ("127.0.0.1");
/* Set all bits of the padding field to 0 */
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);

/*---- Bind the address struct to the socket ----*/
bind(welcomeSocket, (struct sockaddr *) &serverAddr, sizeof(serverAddr));

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com