Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I'm pretty new to network programming and I'm trying to communicate between two computers on the same network using sockets in Python.
I've learned about sockets and how to use them but for some reason, I couldn't make this thing work.
I tried doing this on the same computer with two different Python IDEs and it worked, but when I tried to do this on two different computers it didn't.

What I have tried:

Server side
Python
import socket

s = socket.socket()
host, port = socket.gethostname(), 12345
s.bind((host, port))
s.listen(5)
c, addr = s.accept()

c.send("Thank you for connecting to my server")
c.close()


Client side

Python
import socket

s = socket.socket()
host, port = socket.gethostname(), 12345
s.connect((host, port))

print(s.recv(1024)


Please help me to solve this, thanks in advance.
Posted
Updated 8-Dec-18 17:38pm
Comments
Richard MacCutchan 9-Dec-18 3:15am    
Google for "python socket samples" and you will find what you need.

1 solution

Not sure if I understand the question correctly, but it looks like you're using the current python host name for both client and server side. So it seems that you always try to connect to the same computer where the code is running. Instead, you should probably use the name of the host you're trying to connect to and use for example gethostbyname to resolve the address.

Another thing is that the port you use should be open so ensure that a firewall isn't blocking the communication.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900