代码之家  ›  专栏  ›  技术社区  ›  Alistair Evans

设置套接字的本地端点

  •  7
  • Alistair Evans  · 技术社区  · 15 年前

    我需要创建一个连接到服务器进程的套接字,但必须限制为使用指定的本地适配器。默认行为是尽可能地使用它,但是我需要确保给定的套接字只尝试(例如)LAN连接(如果WIFI和LAN都可用)。

    我使用的是C和.NET 2.0。

    干杯

    3 回复  |  直到 15 年前
        1
  •  14
  •   Svish    15 年前
        2
  •  4
  •   Pat    15 年前

    workaround

    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
    {
        var endPoint = new IPEndPoint(ip, port);
        socket.Bind(endPoint);
        using (var client = new UdpClient() {Client = socket})
        {
            var destinationIP = IPAddress.Broadcast;
            client.Connect(destinationIP, port);
            client.Send(bytes, bytes.Length);
        }
    }