gpt4 book ai didi

sockets - 如何更改套接字的 IP_MULTICAST_IF

转载 作者:行者123 更新时间:2023-12-02 18:41:46 25 4
gpt4 key购买 nike

我有一台有 5 个接口(interface)的计算机:3 个具有公共(public) IP 接口(interface),2 个具有本地 IP 接口(interface)。我正在尝试向 UDP 服务器发送“HELLO”,但我想将接口(interface)更改为本地 IP 地址之一。我做了一些研究,这是我需要在 python 中添加的行:

sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton("64.195.10.11"))

64.195.10.11 是我想要更改的 IP

所以我的 python 代码如下所示:

import socket

MCAST_GRP = '224.0.0.1'
MCAST_PORT = 10222

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton("64.195.10.10"))
sock.sendto("HELLO", (MCAST_GRP, MCAST_PORT))

当我在 3 个公共(public) IP 地址之间进行更改时,它可以正常工作。但是,如果我在参数中输入本地 IP,则它不起作用(数据包未传递)。我不确定是脚本没有发送,还是服务器没有接收。发生了什么事?

SL

编辑:我正在使用两种方式收听:

import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("", 10222))
sock.setsockopt(socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton("224.0.0.1") +
socket.inet_aton("64.195.10.11"))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)

while True:
print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(1024)

print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
print >>sys.stderr, data

print >>sys.stderr, 'sending acknowledgement to', address
sock.sendto('ack', address)

并使用扭曲:

from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
from twisted.application.internet import MulticastServer

class MulticastServerUDP(DatagramProtocol):
def __init__ (self, group, name):
self.group = group
self.name = name
def startProtocol(self):
print '%s Started Listening' % self.group
self.transport.joinGroup(self.group)

def datagramReceived(self, datagram, address):
print "%s Received:"%self.name + repr(datagram) + repr(address)

reactor.listenMulticast(10222, MulticastServerUDP('224.0.0.1', 'SERVER1'), listenMultiple = True)
reactor.run()

最佳答案

如果服务器和客户端位于同一台计算机上(用于测试),也许您需要设置 IP_MULTICAST_LOOP 选项,如下所示(c 代码):

int loop_on = 1;
setsockopt(socket, IPPROTO_IP, IP_MULTICAST_LOOP, &loop_on, sizeof(loop_on));

在 python 中你可以尝试这样的事情:

sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)

希望这有帮助。

编辑:添加一些跟踪(来 self 的 Linux 盒子)

我使用的代码与 SquallLeohart 发布的代码相同:

#! /usr/bin/python

import socket

MCAST_GRP = '224.0.0.1'
MCAST_PORT = 10222

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton("192.168.1.7"))
sock.sendto("HELLO", (MCAST_GRP, MCAST_PORT))

还有:

#! /usr/bin/python
import socket
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(("", 10222))
sock.setsockopt(socket.IPPROTO_IP,
socket.IP_ADD_MEMBERSHIP,
socket.inet_aton("224.0.0.1") +
socket.inet_aton("192.168.1.7"))
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)

while True:
print >>sys.stderr, '\nwaiting to receive message'
data, address = sock.recvfrom(1024)

print >>sys.stderr, 'received %s bytes from %s' % (len(data), address)
print >>sys.stderr, data

print >>sys.stderr, 'sending acknowledgement to', address
sock.sendto('ack', address)

我得到了这个输出:

waiting to receive message
received 5 bytes from ('192.168.1.7', 43761)
HELLO
sending acknowledgement to ('192.168.1.7', 43761)

waiting to receive message
received 5 bytes from ('192.168.1.7', 52326)
HELLO
sending acknowledgement to ('192.168.1.7', 52326)

waiting to receive message

使用 tcpdump:

21:01:33.814728 IP localhost.localdomain.34956 > all-systems.mcast.net.10222: UDP, length 5
21:01:35.364605 IP localhost.localdomain.39078 > all-systems.mcast.net.10222: UDP, length 5
21:01:36.228477 IP localhost.localdomain.49926 > all-systems.mcast.net.10222: UDP, length 5

问候。

关于sockets - 如何更改套接字的 IP_MULTICAST_IF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11717039/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com