am trying to establish a communication link using python sockets and send messages that is received by the other side(server) , in which i always encounter the same problem here is the code
Client Code :
import socket
s = socket.socket()
host = raw_input("Enter a Hostname : ")
port = input ("Enter a port to communicate on : ")
message = raw_input("What do you want to send (Up , Down , Left , Right) : ")
s.connect((host,port))
while True :
if message == "Up" or "up" :
M = 'U'
elif message == "Down" or "down" :
M = 'D'
elif message == "Left" or "left" :
M = 'L'
elif message == "Right" or "right" :
M = 'R'
else :
print ("Please Enter a Valued Word : ")
M = '0'
s.send(M)
print ("This is the sent messages %s to IP %s", (M , host))
print (s.recev(1024))
s.close
Server Code :
import socket
s = socket.socket()
host = socket.gethostname()
port = 8080
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Connected')
c.close()
i always get the following error
Traceback (most recent call last):
File "third_att.py", line 9, in <module>
s.connect((host,port))
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10060] A connection attempt failed because the connected pa
rty did not properly respond after a period of time, or established connection f
ailed because connected host has failed to respond
1 Answer 1
Bind to INADDR_ANY "0.0.0.0":
s.bind(("0.0.0.0", port))
or enter the hostname instead of localhost.
Try printing hostname on server before 'bind()'.
Also, do not forget to enable SO_REUSEADDR just before bind:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
answered Mar 15, 2015 at 15:15
fukanchik
2,93427 silver badges35 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
notomera
I've changed the protocol of sending from TCP to UDP and its going well , Thanks for your help.
lang-py