gpt4 book ai didi

python - 套接字错误 : Invalid Argument supplied

转载 作者:太空狗 更新时间:2023-10-29 20:59:10 24 4
gpt4 key购买 nike

我正在学习网络编程,并试图通过这个例子掌握套接字的基础知识。

import socket,sys


s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

MAX = 65535
PORT = 1060

if sys.argv[1:] == ['server']:
s.bind(('127.0.0.1',PORT))
print 'Listening at ' , s.getsockname()
while True:
data,address = s.recvfrom(MAX)
print ' The address at ' , address , ' says ' , repr(data)
s.sendto('your data was %d bytes' % len(data),address)

elif sys.argv[1:] == ['client']:
print ' Address before sending ' ,s.getsockname()
s.sendto('This is the message',('127.0.0.1',PORT))
print ' Address after sending ' ,s.getsockname()
data,address = s.recvfrom(MAX)
print ' The server at ' , address , ' says ' , repr(data)

else:
print >> sys.stderr, 'usage: udp_local.py server | client '

但是,它抛出一个异常,指出 getsockname() 给出的参数在第 22 行特别无效。据我所知,代码是正确的。这是异常

Traceback (most recent call last):
File "udp_local.py", line 23, in <module>
print ' Address before sending ' ,s.getsockname()
File "c:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10022] An invalid argument was supplied

使用 PyScripter 2.5.3.0 x86

最佳答案

好吧,我遇到了问题。在绑定(bind)或数据发送之前,套接字没有地址。只需将其注释掉即可。

elif  sys.argv[1:] == ['client']:
## print ' Address before sending ' ,s.getsockname()

谢谢

关于python - 套接字错误 : Invalid Argument supplied,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15638214/

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