Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.error: Invalid Argument supplied

Tags:

python

sockets

I am learning networking programming and trying to grasp the basics of sockets through this example.

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 '

However,its throwing up an exception saying the arguments given by getsockname() were invalid specifically on line 22.The code is correct as far as I know.Here's the exception

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

Using PyScripter 2.5.3.0 x86

like image 336
devsaw Avatar asked Mar 26 '13 13:03

devsaw


1 Answers

Well I got the problem.The socket doesn't have an address untill its either binded or data is sent. Just had to comment it out.

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

Thanks

like image 88
devsaw Avatar answered Nov 10 '22 02:11

devsaw