i want to define the sourceport(localport) of this socket connection -- some help 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);
CodePudding user response:
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
// Hard code src port address
address.sin_port=htons(9999);
// Bind to port - warning check return code for failure
bind(fd,(struct sockaddr *)&address,sizeof(address));
// Connect
connect(fd, (struct sockaddr *)&addr, sizeof (struct sockaddr_in));