如何将消息发送到虚拟IPV6地址?

时间:2015-11-05 15:11:50

标签: linux sockets ipv6 virtual-ip-address

我想从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]网络无法访问。而且,我无法ping通虚拟IPV6。

1 个答案:

答案 0 :(得分:0)

public void Compute_Assigned_fitness() { //Np is 200, epsilon=0.22 double Fmin = this.Np + epsilon; double fmin_temp=Fmin; for (int i = 0; i < Front_id.Count; i++) { for (int j = 0; j < Front_id[i].Front_item.Count; j++) { Front_id[i].Front_item[j].Assigned_fitness = Fmin - epsilon; double niche = 0; for (int k = 0; k < Front_id[i].Front_item.Count; k++) { double d = Front_id[i].Front_item[j].distances_to(Front_id[i].Front_item[k]); if (d <= sigma_share) { double shd = 1 - (d / sigma_share) * (d / sigma_share); niche += shd; } Front_id[i].Front_item[j].Niche = niche; } Front_id[i].Front_item[j].Shared_fitness = Front_id[i].Front_item[j].Assigned_fitness / niche; niche = 0; if (fmin_temp > Front_id[i].Front_item[j].Shared_fitness) fmin_temp = Front_id[i].Front_item[j].Shared_fitness; } Fmin = fmin_temp; } for (int i = 0; i < Front_id.Count; i++) { for (int j = 0; j < Front_id[i].Front_item.Count; j++) { P.Add(Front_id[i].Front_item[j]); } } } 开头的IPv6地址是6到4个地址。关于如何使用和路由这些规则有特殊规则。您使用的地址链接到IPv4地址2002:,该地址不是有效的IPv4地址,因此您的IPv6地址也无效。

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