【问题标题】:How to send messages to virtual IPV6 Address?如何向虚拟 IPV6 地址发送消息?
【发布时间】:2015-11-05 15:11:50
【问题描述】:

我想从 Windows/Linux 向我在 Ubuntu 上创建的 IPV6 虚拟 IP 地址发送消息。任何人都可以建议这样做的过程吗?

我通过以下代码在 Ubuntu 中创建了虚拟 IPV6: sudo ip -6 addr add 2002:1:1:1::10/64 dev eth0

而且,为了向 IPV6 发送消息,我使用了这个 Pyhton 代码:

对于客户:

# Echo client program
import socket
import sys

HOST = '2002:1:1:1::10'       # The remote host
PORT = 50007              # The same port as used by the server
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except OSError as msg:
        s = None
        continue
    try:
        s.connect(sa)
    except OSError as msg:
        s.close()
        s = None
        continue
    break
if s is None:
    print('could not open socket')
    sys.exit(1)
s.sendall(b'Hello, world')
data = s.recv(1024)
s.close()
print('Received', repr(data))

对于服务器:

# Echo server program
import socket
import sys

HOST = '2002:1:1:1::10'               # Symbolic name meaning all available interfaces
PORT = 50007              # Arbitrary non-privileged port
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
    af, socktype, proto, canonname, sa = res
    # The AF_* and SOCK_* constants are now AddressFamily and SocketKind IntEnum collections.
    try:
        s = socket.socket(af, socktype, proto)
    except OSError as msg:
        s = None
        continue
    try:
        s.bind(sa)
        s.listen(1)
    except OSError as msg:
        s.close()
        s = None
        continue
    break
if s is None:
    print('could not open socket')
    sys.exit(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
  data = conn.recv(1024)
  print(data)
  if not data: break
  conn.send(data) 
conn.close()

当我运行这个程序时,我收到这个错误:[Errno 101] Network is unreachable。而且,我无法 ping 虚拟 IPV6。

【问题讨论】:

    标签: linux sockets ipv6 virtual-ip-address


    【解决方案1】:

    2002: 开头的IPv6 地址是6to4 地址。关于如何使用和路由这些有特殊规则。您使用的地址链接到 IPv4 地址0.1.0.1,该地址不是有效的 IPv4 地址,因此您的 IPv6 地址也无效。

    使用 IPv6 时,您应该使用 ISP 提供的官方地址。如果您无法从您的 ISP 获得地址,并且只希望地址供本地使用,请使用 ULA 地址。你可以自己生成这些。有多个网站都有可供您使用的在线生成器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2018-04-14
      相关资源
      最近更新 更多