Reputation: 117
I have been teaching myself python sockets module with the help of sentdex. When i attempted to run the code the were no errors. The code was:
`import socket
from _thread import *
host = 'localhost'
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((host, port))
except socket.error as e:
print(str(e))
s.listen(5)
print('Waiting for a connection.')
def threaded_client(conn):
conn.send(str.encode('Welcome, type your info\n'))
while True:
data = conn.recv(2048)
reply = 'Server output: '+ data.decode('utf-8')
if not data:
break
conn.sendall(str.encode(reply))
conn.close()
while True:
conn, addr = s.accept()
print('connected to: '+addr[0]+':'+str(addr[1]))
start_new_thread(threaded_client,(conn,))
`
When I attempted to connect to it on my raspberry pi, it gave the error:
telnet: Unable to connect to remote host: Connection refused
I tried modifying the host to a myriad of different options that I read in other question such as host=''
, host=127.0.0.1
, and host=0.0.0.0
. All to no avail;however, it did make a connection when I tried host='localhost'
on the computer running the script. When i attempted to ping it from my raspberry pi 3, it did nothing. At first, it appeared to be working, but after a while of no change, when I canceled it, the raspberry pi showed that no packages have been received. Please tell me what could be the problem. Could it be either syntactical or firewall-based?
Upvotes: 0
Views: 2818
Reputation: 117
I found my error: I had been attempting to connect to the ip of my router, not the ip of my computer (on which I was running the socket server). Sorry to be a bother.
Upvotes: 3