简体   繁体   English

Socket.Bind 和 Connect 使用本地地址

[英]Socket.Bind and Connect using local addresses

To test my server/client application, where each client is known by its IP address, I created several network adapters (see How to Create a Virtual Network Adapter in .NET? ).为了测试我的服务器/客户端应用程序,其中每个客户端都通过其 IP 地址知道,我创建了几个网络适配器(请参阅如何在 .NET 中创建虚拟网络适配器? )。 Both 192.168.0.10 and 11 now correspond to local ethernet adaptors (10 being the "real" one, 11 being a loopback adapter). 192.168.0.10 和 11 现在都对应于本地以太网适配器(10 是“真正的”适配器,11 是环回适配器)。

The client can Connect itself to the server as long as it doesn't Bind its socket to a specific address.只要客户端不将其套接字Bind到特定地址,它就可以将自己Connect到服务器。 But if it does, the server doesn't notice anything and a timeout occurs in the client (I want to use Bind as for security reasons the server automatically detects which client is connecting itself by looking at the IP address of the remote end point of the new connection: the server will drop the connection at once if it doesn't know the IP address - previously I was using several virtual machines, but it uses a lot more RAM and is less practical to use).但是如果是这样,服务器不会注意到任何事情并且客户端会发生超时(出于安全原因,我想使用Bind ,服务器会通过查看远程端点的 IP 地址自动检测哪个客户端正在连接自己新连接:如果服务器不知道 IP 地址,它会立即断开连接 - 之前我使用了多个虚拟机,但它使用了更多的 RAM,并且不太实用)。

Here's the code in my server, listening eg on 192.168.0.10:1234这是我服务器中的代码,例如在 192.168.0.10:1234 上监听

IPEndPoint myEP = new IPEndPoint(myAddress, myPort);
Socket listeningSocket = new Socket(myAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
listeningSocket.Bind(myEP);
listeningSocket.Listen(50);
Socket acceptedSocket = listeningSocket.Accept();

Here's the code in my client, binding eg to 192.168.0.11 (any port) and connecting to 192.168.0.10:1234这是我客户端中的代码,例如绑定到 192.168.0.11(任何端口)并连接到 192.168.0.10:1234

Socket socket = new Socket(svrAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(new IPEndPoint(myAddress, 0)); // Bind to local address using automatic port
socket.Connect(new IPEndPoint(svrAddress, svrPort)); // Works fine without Bind, timeout with Bind

I've tried the same using the corresponding IPv6 addresses but I get the exact same result.我已经尝试使用相应的 IPv6 地址进行相同的操作,但得到的结果完全相同。 If I Bind the client on the same address (using a different port than the server), it works fine.如果我将客户端Bind到同一地址(使用与服务器不同的端口),它工作正常。

Any idea what I'm doing wrong?知道我做错了什么吗?

EDIT Here is my test projects (it might be useful to someone)编辑这是我的测试项目(可能对某人有用)

Server part:服务器部分:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace Server
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;

            string line = string.Empty;
            while (line != "q")
            {
                // Gets the IP address to listen on.
                Console.WriteLine("IP to listen on:");
                int count = 0;
                foreach (IPAddress ip in ips)
                    Console.WriteLine("{0}: {1}", ++count, ip.ToString());

                string numString = Console.ReadLine();
                int pos = Convert.ToInt32(numString) - 1;
                IPAddress myAddress = ips[pos]; // Removing or not the scope ID doesn't change anything as "localEndPoint" below will contain it no matter what

                // Binds and starts listening.
                IPEndPoint myEP = new IPEndPoint(myAddress, 12345);
                Socket listeningSocket = new Socket(myAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                listeningSocket.Bind(myEP);
                listeningSocket.Listen(50);

                IPEndPoint localEndPoint = (IPEndPoint)listeningSocket.LocalEndPoint;
                Console.WriteLine("Listening on {0}:{1}", localEndPoint.Address, localEndPoint.Port);

                Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            // Accepts new connections and sends some dummy byte array, then closes the socket.
                            Socket acceptedSocket = listeningSocket.Accept();
                            IPEndPoint remoteEndPoint = (IPEndPoint)acceptedSocket.RemoteEndPoint;
                            Console.WriteLine("Accepted connection from {0}:{1}.", remoteEndPoint.Address, remoteEndPoint.Port);
                            acceptedSocket.Send(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
                            acceptedSocket.Close(5000);
                            Console.WriteLine("-= FINISHED =- Type q to quit, anything else to continue");
                        }
                        catch (Exception ex)
                        { }
                    });

                line = Console.ReadLine();

                // Closes the listening socket.
                listeningSocket.Close();
            }
        }
    }
}

Client part客户端部分

using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            IPAddress[] ips = Dns.GetHostEntry(Dns.GetHostName()).AddressList;

            string line = string.Empty;
            while (line != "q")
            {
                // Gets the IP address to connect to (removes the "scope ID" if it's an IPv6).
                Console.WriteLine("IP to connect to:");
                int count = 0;
                foreach (IPAddress ip in ips)
                    Console.WriteLine("{0}: {1}", ++count, ip.ToString());

                string numString = Console.ReadLine();
                int pos = Convert.ToInt32(numString) - 1;
                IPAddress svrAddress = ips[pos].AddressFamily == AddressFamily.InterNetworkV6
                    ? new IPAddress(ips[pos].GetAddressBytes())
                    : ips[pos];

                Console.WriteLine("Connecting to " + svrAddress);

                // Gets the IP address to bind on (can chose "none" - also removes the "scope ID" if it's an IPv6).
                Console.WriteLine("IP to bind to:");
                Console.WriteLine("0: none");
                count = 0;
                IPAddress[] filteredIps = ips.Where(i => i.AddressFamily == svrAddress.AddressFamily).ToArray();
                foreach (IPAddress ip in filteredIps)
                    Console.WriteLine("{0}: {1}", ++count, ip.ToString());

                numString = Console.ReadLine();
                pos = Convert.ToInt32(numString) - 1;
                IPEndPoint localEndPoint = (pos == -1)
                    ? null
                    : new IPEndPoint(
                        filteredIps[pos].AddressFamily == AddressFamily.InterNetworkV6
                            ? new IPAddress(filteredIps[pos].GetAddressBytes())
                            : filteredIps[pos]
                        , 0);
                Console.WriteLine("Binding to " + (localEndPoint == null ? "none" : localEndPoint.Address.ToString()));

                // Binds to an address if we chose to.
                Socket socket = new Socket(svrAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                if (localEndPoint != null)
                    socket.Bind(localEndPoint);

                Task.Factory.StartNew(() =>
                {
                    try
                    {
                        // Connects to the server and receives the dummy byte array, then closes the socket.
                        socket.Connect(new IPEndPoint(svrAddress, 12345));
                        IPEndPoint remoteEndPoint = (IPEndPoint)socket.RemoteEndPoint;
                        Console.WriteLine("Connected to {0}:{1}", remoteEndPoint.Address, remoteEndPoint.Port);
                        byte[] buffer = new byte[10];
                        Console.WriteLine((socket.Receive(buffer) == buffer.Length) ? "Received message" : "Incorrect message");
                        socket.Close();
                    }
                    catch (Exception ex)
                    {
                        // An exception occured: should be a SocketException due to a timeout if we chose to bind to an address.
                        Console.WriteLine("ERROR: " + ex.ToString());
                    }
                    Console.WriteLine("-= FINISHED =- Type q to quit, anything else to continue");
                });

                line = Console.ReadLine();
            }
        }
    }
}

Actually it was a configuration issue with my network adapters and it has to do with " Weak and Strong Host model ".实际上这是我的网络适配器的配置问题,它与“弱主机模型”有关。

From what I've read ( Using a specific network interface for a socket in windows ) binding on Windows previous to Vista would only work for incoming traffic, and it wouldn't do anything for outgoing traffic.从我读过的内容来看( 在 windows 中为套接字使用特定的网络接口)在 Vista 之前的 Windows 上绑定仅适用于传入流量,它不会对传出流量执行任何操作。

Starting with Vista it's possible but by default it won't work: you need to allow the "weak host model" using从 Vista 开始它是可能的,但默认情况下它不会工作:你需要允许“弱主机模型”使用

netsh interface ipv4 set interface "loopback" weakhostreceive=enabled
netsh interface ipv4 set interface "loopback" weakhostsend=enabled

See https://web.archive.org/web/20150402200610/http://blog.loadbalancer.org/direct-server-return-on-windows-2008-using-loopback-adpter/ for more info.有关详细信息,请参阅https://web.archive.org/web/20150402200610/http://blog.loadbalancer.org/direct-server-return-on-windows-2008-using-loopback-adpter/

EDIT编辑

Actually, instead of creating several loopback adapters and changing their host model, it's a lot better and easier to just create one loopback adapter, give it several IP addresses on a different network than your real IP, and then only use those IPs for your test.实际上,与其创建多个环回适配器并更改它们的主机模型,不如只创建一个环回适配器,在与真实 IP 不同的网络上为其提供多个 IP 地址,然后仅将这些 IP 用于测试会更好、更容易. That way there's no routing issue, and you're sure everything stays local (as there's no routing between the real and loopback adapter).这样就没有路由问题,而且您可以确定一切都在本地(因为真实适配器和环回适配器之间没有路由)。

Use below code in the server for binding the connection on all the interface on the same port.在服务器中使用以下代码将连接绑定到同一端口上的所有接口。

// Binds and starts listening.
IPEndPoint myEP = new IPEndPoint(IPAddress.Any, 12345);

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

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