Click here to Skip to main content
13,868,977 members
Rate this:
Please Sign up or sign in to vote.
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
import socket

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

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

Client side

import socket

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


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

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web03 | 2.8.190214.1 | Last Updated 8 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100