如何指定IP上网的问题?


一台机器配置了多个IP,如何在上网的时候,指定用某一个特定的IP上网?

TCP/IP VC windows

我想骑狮子 12 years, 5 months ago

socket bind

示例:

   
  struct   sockaddr_in   addr;
  
int n = 1;

if ((fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
DEBUG(LOG_ERR, "socket call failed: %s ", sys_errlist[errno]);
return -1;
}

memset(&addr;, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
addr.sin_addr.s_addr = ip;
if (bind(fd, (struct sockaddr *)&addr;, sizeof(struct sockaddr)) == -1) {
close(fd);
return -1;
}

NEET蕾米 answered 12 years, 5 months ago

Your Answer