关于多网卡时socket bind失效的问题



On Windows, a call to bind() affects card selection only incoming traffic, not outgoing traffic.

Thus, on a client running in a multi-homed system (i.e., more than one interface card), it's the network stack that selects the card to use, and it makes its selection based solely on the destination IP, which in turn is based on the routing table. A call to bind() will not affect the choice of the card in any way.

It's got something to do with something called a "Weak End System" ("Weak E/S") model. Vista changed to a strong E/S model, so the issue might not arise under Vista. But all prior versions of Windows used the weak E/S model.

With a weak E/S model, it's the routing table that decides which card is used for outgoing traffic in a multihomed system.

See if these threads offer some insight:

"Local socket binding on multihomed host in Windows XP does not work" at http://www.codeguru.com/forum/showthread.php?t=452337

"How to connect a port to a specified Networkcard?" at http://www.codeguru.com/forum/showthread.php?t=451117. This thread mentions the CreateIpForwardEntry() function, which (I think) can be used to create an entry in the routing table so that all outgoing IP traffic with a specified server is routed via a specified adapter.

"Working with 2 Ethernet cards" at http://www.codeguru.com/forum/showthread.php?t=448863

"Strange bind behavior on multihomed system" at http://www.codeguru.com/forum/showthread.php?t=452368



转载:http://blog.sina.com.cn/s/blog_54b5ea250100ktgi.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
socketbind函数是用于将一个套接字(socket)绑定到一个特定的地址和端口号上。它在网络编程中经常被用来指定服务器监听的IP地址和端口号。 在C语言中,使用socketbind函数的原型如下: ```c #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen); ``` 其中,参数说明如下: - sockfd:需要绑定的套接字描述符; - addr:指向目标地址结构体的指针; - addrlen:目标地址结构体的长度。 常见的使用场景是在服务器程序中,通过bind函数将服务器的套接字绑定到服务器的IP地址和端口上,以便客户端能够连接到服务器。 例如,以下代码片段展示了一个简单的服务器使用socketbind函数进行绑定的示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <arpa/inet.h> int main() { int sockfd; struct sockaddr_in server_addr; // 创建套接字 sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd == -1) { perror("socket"); exit(EXIT_FAILURE); } // 设置服务器地址结构体 memset(&server_addr, 0, sizeof(server_addr)); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(8080); server_addr.sin_addr.s_addr = htonl(INADDR_ANY); // 绑定套接字到指定地址和端口 if (bind(sockfd, (struct sockaddr*)&server_addr, sizeof(server_addr)) == -1) { perror("bind"); exit(EXIT_FAILURE); } // 其他操作... return 0; } ``` 在这个示例中,首先创建了一个套接字,然后设置服务器地址结构体,最后使用bind函数将套接字绑定到指定的地址和端口。绑定成功后,服务器可以监听客户端的连接请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值