如何使用 python 合并 VOIP?
我目前正在尝试将套接字实时多播音频到IP和端口。
import socket
MCAST_GRP = '000.0.0.00'
MCAST_PORT = 00000
MULTICAST_TTL = 2
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)
with open('C:\\Users\\User\\Downloads\\dog_bark_x.wav', 'rb') as f:
for l in f:
sock.sendto(sock.sendall(l), (MCAST_GRP, MCAST_PORT))
sock.close()
我目前正在使用WAV文件对此进行测试。但是,当我运行代码时,我会收到此错误:
[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
我可以使用下面的代码发送字符串而无需错误,这意味着客户端已连接并侦听,所以我不确定为什么我会遇到上述错误:
import socket
MCAST_GRP = '000.0.0.00'
MCAST_PORT = 00000
MULTICAST_TTL = 2
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)
sock.sendto("Test".encode(), (MCAST_GRP, MCAST_PORT))
I am currently trying to use Socket to multicast audio in real time to an IP and Port.
import socket
MCAST_GRP = '000.0.0.00'
MCAST_PORT = 00000
MULTICAST_TTL = 2
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)
with open('C:\\Users\\User\\Downloads\\dog_bark_x.wav', 'rb') as f:
for l in f:
sock.sendto(sock.sendall(l), (MCAST_GRP, MCAST_PORT))
sock.close()
I am currently testing this by using a WAV file. however when i run the code I receive this error:
[WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied
I can send strings without error using code below, meaning the client is connected and listening so im not sure why i am encountering the error above:
import socket
MCAST_GRP = '000.0.0.00'
MCAST_PORT = 00000
MULTICAST_TTL = 2
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, MULTICAST_TTL)
sock.sendto("Test".encode(), (MCAST_GRP, MCAST_PORT))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
内部
sock.sendall(l)
正在未连接的套接字上工作,这就是错误的原因。您可能根本不想在这里使用sendall
,而只是注意,尽管您在这里使用的是 UDP,这是一个不可靠的协议,即数据报在传输过程中可能会丢失、重复或重新排序。因此,您不能指望接收者将完全按照发送时的方式读取数据。
除此之外,在读取二进制数据时使用
for line in file
并不是一个好主意。The inner
sock.sendall(l)
is working on the unconnected socket, that's why the error. It is likely that you did not mean to usesendall
here at all but simplyNote though that you are using UDP here which is an unreliable protocol, i.e. datagrams might be lost, duplicated or reordered during transmit. You thus cannot expect that the data will be read by the recipient exactly as they were sent.
Apart from that it is not a good idea to use
for line in file
when reading binary data.