【发布时间】:2021-11-16 06:30:14
【问题描述】:
我想定义这个套接字连接的源端口(本地端口)——一些帮助 plox xD
if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
{
continue;
}
// Set it in nonblocking mode
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
// Set up address to connect to
addr.sin_family = AF_INET;
if (targs[i].netmask < 32)
addr.sin_addr.s_addr = htonl(ntohl(targs[i].addr) + (((uint32_t)rand_next()) >> targs[i].netmask));
else
addr.sin_addr.s_addr = targs[i].addr;
if (dport == 0xffff)
addr.sin_port = rand_next() & 0xffff;
else
addr.sin_port = htons(dport);
connect(fd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in));
usleep(1000000);
start_recv = time(NULL);
【问题讨论】:
-
使用
bind()选择本地端口。 -
为什么?这是一个非常糟糕的主意,因为在等待时间到期之前套接字将不可用。
-
@Barmar 谢谢我会试试这个
-
@RowanSmith 这正是我需要保持套接字打开直到我完成他
-
展示你的尝试,我将展示如何解决它。