IleNea
IleNea

Reputation: 609

Python server client socket

I've tried to connect two computers with a socket in Python and I don't know why it doesn't work. The files are from internet and it compiles for me but without any results.

The server.py:

#!/usr/bin/python           

import socket               

s = socket.socket()       
host = '' 
port = 12345                
s.bind((host, port))        

s.listen(5)                 
while True:
    c, addr = s.accept()     
    print 'Got connection from', addr
    c.send('Thank you for connecting')
    c.close() 

and the client.py:

#!/usr/bin/python           

import socket               

s = socket.socket()         
host = # here I put the ip of the server's laptop
port = 12345                

s.connect((host, port))
print s.recv(1024)
s.close()

What's wrong?

Upvotes: 0

Views: 485

Answers (1)

julz12
julz12

Reputation: 452

You have to run the server first. Then run the client at the same time with the IP of the server (I used localhost because it was running on one computer, maybe you should try if that works). The code worked fine for me, every time I ran the client, the server printed a message. If it doesn't work for you, maybe your firewall is not letting you open ports.

Just for the future, please always post any error messages you see.

BTW, isn't this the Python Documentation example for sockets?

Upvotes: 1

Related Questions