简体   繁体   English

Mono Socket.Bind在连接之前使用特定接口

[英]Mono Socket.Bind before connecting to use a specific interface

I'm attempting to connect to a remote server using a specific local interface. 我正在尝试使用特定的本地接口连接到远程服务器。 My logs tell me everything is working as intended, but checking with netstat, every connection is using the default interface. 我的日志告诉我一切都按预期进行,但是使用netstat检查时,每个连接都使用默认接口。

I'm using the following code to bind a TcpClient to a specific Local Endpoint 我正在使用以下代码将TcpClient绑定到特定的本地端点

Console.WriteLine("Binding to {0}", connectionArgs.LocalBindingInterface.ToString());
client = new TcpClient(connectionArgs.LocalBindingInterface);
Console.WriteLine("Bound to {0}", client.Client.LocalEndPoint.ToString());

Where connectionArgs.LocalBindingInterface is an IPEndPoint specified as such 其中connectionArgs.LocalBindingInterface是这样指定的IPEndPoint

IPEndPoint[] localEndPoints = new IPEndPoint[2];
localEndPoints[0] = new IPEndPoint(IPAddress.Parse("192.168.0.99"), 0);
localEndPoints[1] = new IPEndPoint(IPAddress.Parse("192.168.0.100"), 0);

The IP addresses listed here are not the actual addresses. 这里列出的IP地址不是实际的地址。

When i check my logs, this is the info I get 当我查看日志时,这就是我得到的信息

Binding to 192.168.0.99:0
Bound to 192.168.0.99:59252
Binding to 192.168.0.100:0
Bound to 192.168.0.100:53527

But when i netstat -n -p --tcp -a I get 但是当我netstat -n -p --tcp -a我得到

tcp        0      0 192.168.0.98:39948    remote_addr_here:443       ESTABLISHED 17857/mono
tcp        0      0 192.168.0.98:60009    remote_addr_here:443       ESTABLISHED 17857/mono

Clearly something's wrong here. 显然这里有问题。 None of the ports, nor the interfaces match. 没有端口,也不匹配接口。 Netstat is run as sudo so I can't assume it's wrong. Netstat作为sudo运行,所以我不能认为这是错误的。 I also tried to manually create a socket, call it's bind method, and set the TcpClient's Client property to the manually bound socket, but I get the same result. 我也尝试过手动创建一个套接字,调用它的bind方法,并将TcpClient的Client属性设置为手动绑定的套接字,但是得到的结果相同。

Is there something i'm doing wrong here? 我在这里做错什么了吗? Is there a different way to force a Socket to use a specific Local EndPoint on mono? 有没有其他方法可以强制Socket在Mono上使用特定的Local EndPoint?

I'm running this app as a non-root user, mono --version is Mono JIT compiler version 3.2.8 (Debian 3.2.8+dfsg-4ubuntu1.1) , server's ubuntu version is Ubuntu 14.04.3 LTS 我以非root用户身份运行此应用程序,mono --version是Mono JIT compiler version 3.2.8 (Debian 3.2.8+dfsg-4ubuntu1.1) ,服务器的ubuntu版本是Ubuntu 14.04.3 LTS

Edit 1: 编辑1:

Added an extra logging call after calling TcpClient.Connect() 在调用TcpClient.Connect()之后添加了一个额外的日志记录调用

Binding to 192.168.0.100:59000
Bound to 192.168.0.100:59000
After connect bound to 192.168.0.98:55484

Bottom line: you can't do this, not at the socket level. 底线:您不能这样做,而不是在套接字级别。

The routing of outbound traffic is determined by the network routing configuration. 出站流量的路由由网络路由配置确定。 You would have to create an explicit routing table entry for your destination to force a specific adapter to be used. 您将必须为目的地创建一个明确的路由表条目,以强制使用特定的适配器。

You can bind to a specific IP address, but this only causes inbound traffic to be filtered, ie you'll only receive traffic sent to that IP address. 您可以绑定到特定的IP地址,但这只会导致对入站流量进行过滤,即,您将只接收发送到该IP地址的流量。


There are related questions you may want to read as well: 您可能还需要阅读一些相关的问题:

How to stop behaviour: C++ Socket sendto changes interface — context is C++ and not constrained to Windows, but it has what is IMHO the most direct, most relevant notes on the topic. 如何停止行为:C ++套接字sendto更改接口 -上下文是C ++且不限于Windows,但是它具有IMHO关于该主题最直接,最相关的注释。

Using a specific network interface for a socket in windows — fairly poor question and answer both, frankly. 在Windows中为套接字使用特定的网络接口 -坦白地说,这两个问题的回答都相当差。 But it does contain some quotes and links that you might find useful anyway. 但是它确实包含一些引号和链接,您可能仍然会发现它们有用。

Arguably, this question might have been closed as a duplicate of one of those, or perhaps even another similar question. 可以说,这个问题可能是其中一个问题的重复,或者甚至是另一个类似的问题。 But those two don't really answer the question in an accurate, C#/.NET-specific way, and I didn't actually find any others that seemed any better. 但是,这两个人并没有以准确的,特定于C#/。NET的方式真正地回答了这个问题,而且我实际上没有发现其他任何看起来更好的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM