如何动态地将套接字绑定到只有一个网络接口? [英] How do I dynamically bind a socket to only one network interface?

查看:152
本文介绍了如何动态地将套接字绑定到只有一个网络接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我做以下侦听所有接口的任何可用的端口:

Currently I do the following to listen on any available port on all interfaces:

// hints struct for the getaddrinfo call
struct addrinfo hints, *res;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;

// Fill in addrinfo with getaddrinfo
if (getaddrinfo(NULL, "0", &hints, &res) != 0) {
	cerr << "Couldn't getaddrinfo." << endl;
	exit(-1);
}

我想动态绑定到只有一个接口,该系统的非环回接口

I would like to dynamically bind to only one interface, the non-loopback interface of the system.

我怎么会去这样做?

推荐答案

看看SO_BINDTODEVICE。 Tuxology有这很好的说明

Take a look at SO_BINDTODEVICE. Tuxology has a good description of this

这篇关于如何动态地将套接字绑定到只有一个网络接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆