8
我正在學習網絡編程,並嘗試通過此示例掌握套接字的基礎知識。Socket.error:無效提供的參數
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.The代碼是正確的,因爲據我know.Here是例外
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
那是Winsock的? – wRAR 2013-03-26 13:55:16
一個套接字可能實際上不會在發送之前擁有一個地址,除非您先調用bind。在Mac上,我沒有收到錯誤,但返回的端口是'0'(表示尚未分配端口)。 – robertklep 2013-03-26 14:07:56
我正在使用Python.The標準套接字模塊 – devsaw 2013-03-26 14:27:53