I’m coding a game in python. I decided before coding the game’s multiplayer server I’ll experiment with a smaller server(just to see how server coding is in python) but it seems my program only works on my own windows 10 computer. When I share the same client to my friend(his computer also uses windows 10) then the client did not connect(It’s not that I ran the server after my friend ran client, I started the server first). Anyone know why? The client side returns an error saying it isn’t able to connect to my computer(This is the only info my friend gave, and he isn’t lying either. If the client connected without any issues then I would get a message logging that). Here’s my server’s code:
import socket
from time import sleep
#Default port number: 5555
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
def run_server(port=5555):
print('Booting server...')
print('|-|-|-',end='')
sleep(0.05)
server.bind(('',port))
print('|-|-|-',end='')
sleep(0.05)
server.listen(5)
print('|-|-|',end='')
sleep(0.05)
print('\nServer is running and can be accessed now\n===============================================')
while True:
c,addr=server.accept()
print('recieved connection from: ',addr)
try:
c.send(bytes("ip=bytes(input('Welcome. Enter hostname to extract ip from: '),'utf-8')",'utf-8'))
c.send(bytes('_socket.send(ip)','utf-8'))
reply=c.recv(1024).decode('utf-8')
except ConnectionResetError:
print(addr,'closed connection')
continue
try:
ip=socket.gethostbyname(reply)
except:
c.send(bytes('''print("The hostname is either invalid or wasn't found")''','utf-8'))
c.send(bytes('_socket.close()','utf-8'))
continue
c.send(bytes("print('"+ip+"')",'utf-8'))
c.send(bytes('close','utf-8'))
run_server()
And the client’s code:
import socket
def run(mode='client'):
_socket=socket.socket()
_socket.connect(('192.168.1.3',5555))
while True:
command=_socket.recv(1024).decode('utf-8')
if command=='close':
_socket.close()
break
else:
exec(command)
while True:
try:
run()
except OSError as e:
print(e)
continue