鱼C论坛

 找回密码
 立即注册
查看: 3222|回复: 4

端口

[复制链接]
发表于 2023-5-15 21:20:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 fishhh 于 2023-5-15 21:21 编辑

假设我电脑性能最够的情况下,我开65536个应用[doge]
端口不复用,吧端口号全占了,这时在开应用会怎样
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-5-15 21:28:42 | 显示全部楼层
不知道
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-15 22:57:52 From FishC Mobile | 显示全部楼层
端口被占了就不可用了
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-16 09:40:28 | 显示全部楼层
我8G内存  开了300个浏览器窗口  就基本上 啥也干不了了   和崩溃没啥区别
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-5-16 11:02:31 | 显示全部楼层
不需要开65535个“应用”,一个进程就够用了,想知道具体会发生什么可以直接自己试试,这是 Linux 上能运行的代码,编译要链接 libcap(-lcap),需要 root 运行或者给 CAP_NET_BIND_SERVICE
  1. #include <arpa/inet.h>
  2. #include <errno.h>
  3. #include <netinet/in.h>
  4. #include <signal.h>
  5. #include <stdbool.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/capability.h>
  10. #include <sys/socket.h>
  11. #include <unistd.h>
  12. static bool check_capability() {
  13.   cap_t capabilities = cap_get_proc();
  14.   bool result = false;
  15.   do {
  16.     if (capabilities == NULL) {
  17.       perror("cap_get_proc");
  18.       exit(EXIT_FAILURE);
  19.     }
  20.     cap_flag_value_t value;
  21.     if (cap_get_flag(capabilities, CAP_NET_BIND_SERVICE, CAP_EFFECTIVE, &value) == -1) {
  22.       perror("cap_get_flag");
  23.       exit(EXIT_FAILURE);
  24.     }
  25.     if (value == CAP_SET) {
  26.       result = true;
  27.       break;
  28.     }
  29.     if (cap_get_flag(capabilities, CAP_NET_BIND_SERVICE, CAP_PERMITTED, &value) == -1) {
  30.       perror("cap_get_flag");
  31.       exit(EXIT_FAILURE);
  32.     }
  33.     if (value == CAP_CLEAR) {
  34.       result = false;
  35.       break;
  36.     }
  37.     cap_value_t caps[] = {CAP_NET_BIND_SERVICE};
  38.     if (cap_set_flag(capabilities, CAP_EFFECTIVE, 1, caps, CAP_SET) == -1) {
  39.       perror("cap_set_flag");
  40.       exit(EXIT_FAILURE);
  41.     }
  42.     if (cap_set_proc(capabilities) == -1) {
  43.       perror("cap_set_proc");
  44.       exit(EXIT_FAILURE);
  45.     }
  46.     result = true;
  47.   } while (false);
  48.   cap_free(capabilities);
  49.   return result;
  50. }
  51. static void deal_with_permission() {
  52.   if (geteuid() == 0) {
  53.     return;
  54.   }
  55.   if (check_capability() == false) {
  56.   }
  57.   if (check_capability() == false) {
  58.     fprintf(stderr, "permission denied.\n");
  59.     exit(EXIT_FAILURE);
  60.   }
  61. }
  62. static void handler(int sig) { return; }
  63. int main() {
  64.   deal_with_permission();
  65.   int fds[UINT16_MAX + 1];
  66.   memset(fds, -1, sizeof(fds));
  67.   struct sockaddr_in address = {.sin_family = AF_INET, .sin_port = 0, .sin_addr = {inet_addr("127.0.0.1")}};
  68.   for (uint16_t i = 1; i != 0; i++) {
  69.     address.sin_port = htons(i);
  70.     fds[i] = socket(AF_INET, SOCK_STREAM, 0);
  71.     int ret = bind(fds[i], (struct sockaddr *)&address, sizeof(address));
  72.     if (ret == 0) {
  73.       printf("bind (fd)%d -> (port)%d\n", fds[i], i);
  74.     } else {
  75.       printf("failed to bind to (port)%d: %s\n", i, strerror(errno));
  76.       close(fds[i]);
  77.       fds[i] = -1;
  78.     }
  79.   }
  80.   signal(SIGINT, handler);
  81.   printf("signal me (pid=%d) with SIGINT to cleanup, waiting...\n", getpid());
  82.   pause();
  83.   for (uint16_t i = 1; i != 0; i++) {
  84.     if (fds[i] != -1) {
  85.       close(fds[i]);
  86.     }
  87.   }
  88.   return 0;
  89. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 20:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表