9

This is the server side program

import socket

s = socket.socket()
host = socket.gethostname()
port = 9077
s.bind((host,port))
s.listen(5)

while True:
    c, addr = s.accept()
    print("Connection accepted from " + repr(addr[1]))
    c.send("Thank you for connecting")
    c.close()

This is the client program

import socket

s = socket.socket()
host = socket.gethostname()
port = 9077
s.connect((host, port))
print s.recv(1024)

When i run these two programs on the same computer, it works perfectly. But when i run the client and server programs in two different computers on the same network, the program doesn't work.

Can anyone please tell me how to send message from one computer to another on the same network.

This is the first time i'm doing any network programming. Any help would be appreciated

Thanks in advance

2 Answers 2

4

You are connecting from the client to the client's computer, or well attempting to, because you are using the client's hostname rather than the servers hostname/ip address.

So, to fix this change the line s.connect((host, port)) so that the host points to the servers ip address instead of the client's hostname.

You can find this by looking at your network settings on the server and doing the following:

host = "the ip found from the server's network settings"
Sign up to request clarification or add additional context in comments.

Comments

0

host must be edited to the server's ip if the server is not the same computer.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.