2

Hi I am doing a project that I want to make Raspberry pi and Pc communicate and then Pc can get pulse signal sent from raspberry pi. The first step I think to do is to write a python server on raspberry pi side, and a client on windows side. Is it possible to get signal in this way?

and this is the code that I get from internet

The server :


import socket

s = socket.socket()
host = socket.gethostname()
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 :


import socket

s = socket.socket()
host = socket.gethostname()
print (host)
port = 12345

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

But there is an error :

**Traceback (most recent call last):
  File "C:\Python33\SimpleClient.py", line 9, in <module>
    s.connect((host, port))
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it**

I don't know what's wrong with it. Please help me :roll: Any answer are greatly accepted! Big thanks !!

  • oh my !! I didnt realize my code above became like this. It's hard to read! I am sorry! – serene Feb 4 '14 at 7:51
  • will update a new one for reading easier! I am sorry! – serene Feb 4 '14 at 7:58
  • As a general note, PEP-8 gives some suggestions for the formatting of Python code. – Klaus-Dieter Warzecha Feb 5 '14 at 7:53
4

For the server, it makes sense to obtain the hostname via

host = socket.gethostname()

But how should the client know to whom to connect if you use the same command there?

In the client

host = socket.gethostname()
print (host)

prints the name of the client, not the host, isn't it?

Here, you might want to give the hostname of the server explicitly.


When setting up the sockets, you also might want to state the socket family and the type.

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

EDIT:

You wrote:

There're nothing wrong with the server.

Are you going to bet on that? Paypal accepted :D

Now seriously, simply binding the socket on the server to the hostname obtained by

socket.gethostbyname() 

does NOT work in my local network either and casts an error (111 Connection refused).

There are several options on the server side, apart from using its ip:

  1. leave the hostname empty when binding
  2. use the fully qualified domain name, such as whatever.fritz.box

On the client side, you can use the simple hostname of the server.

I tried these options when connecting from a Linux box to a Raspberry and it works nicely.

2

Easy way I solved this...

Client.py

import socket               

s = socket.socket()        
host = ''# ip of raspberry pi 
port = 12345               
s.connect((host, port))
print(s.recv(1024))
s.close()

Server.py

import socket

s = socket.socket()
host = '' #ip of raspberry pi
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()

protected by Community Jul 5 '18 at 13:43

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.