Socket.error:提供的参数无效

时间:2013-03-26 13:26:21

标签: python sockets

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

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 '

然而,它抛出了一个异常,说getockname()给出的参数在第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

1 个答案:

答案 0 :(得分:8)

嗯,我遇到了问题。套接字没有地址,直到其绑定或数据发送。 只是不得不发表评论。

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

谢谢