0

I accessed raspberry pi on my pc(windows 10). The server is running on raspberry pi and I want to send data to the server from my pc(client running on my PC, not in raspberry pi).

client.py

import socket
mysoc=socket.socket()
mysoc.connect(("xxx.xxx.xxx.xxx",1234)) //ip address of raspberrypi
while 1:
 s=input("Enter command")
 mysoc.sendall(s.encode())
 if s==exit:
 break
mysoc.close()

server.py

import socket
from gpiozero import LED
led=LED(17)
server_soc=socket.socket()
server_soc.bind(("",1234))
server_soc.listen(5)
print("Listening to client ...")
conn,addr=server_soc.accept()
print("connected to client:",addr)
while 1:
 data=conn.recv(1024)
 print(data)
 if data==b"on\n":
 print("LED is on")
 led.on()
 elif data==b"off\n":
 print("LED is off")
 led.off()
 else:
 break 
conn.close()
server_soc.close()

I get the following error when client.py is executed.

>>> 
= RESTART: C:\Users\Lenovo\AppData\Local\Programs\Python\Python38-32\My_Programs\client.py
Enter commandon
Enter commandoff
Enter commandon
Traceback (most recent call last):
 File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python38-32\My_Programs\client.py", line 7, in <module>
 mysoc.sendall(s.encode())
ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine
>>>

server.py

>>> %Run server_control_led.py
Listening to client ...
connected to client: ('xxx.xxx.xxx.x', 50603) //ip address of my pc
b'on'
>>> 

The server receives the first data and stops listening.
I tried to disable antivirus and firewall but the issue still exists.
I unable to figure the root cause of this closing connection. whether issue is on the server side or client-side.
I am using putty and xming server and python code. I tried doing some changes in inbound and outbound rules in the firewall but still doesn't work.
what should be configured?
asked Jun 9, 2020 at 8:28
1
  • it is in 6th line in client.py Commented Jun 9, 2020 at 10:14

1 Answer 1

1

You should specify the type of encoing in your client's 6th line. Instead of mysoc.sendall(s.encode()) you should use mysoc.sendall(s.encode('utf-8')). If you don't want to use utf-8 you can always pass other encoding methods such as ascii. -Update. There's another error in the 7th line, where you try to compare s with a string but forget to put the quotes: Your code: if s==exit: Correction: if s=="exit":

-Update #2: I've just found the actual error: you don't decode the string properly on the server side, so the conditional doesn't identify the command as 'on' or 'off' but as something else, that's why the server exits after receiving the first message. Here's the fixed code: (Server)

import socket
server_soc=socket.socket()
server_soc.bind(("",1234))
server_soc.listen(5)
print("Listening to client ...")
conn,addr=server_soc.accept()
print("connected to client:",addr)
while 1:
 data=conn.recv(1024)
 print(data)
 data=data.decode('utf-8')
 if data=="on":
 print("LED is on")
 elif data=="off":
 print("LED is off")
 else:
 break 
conn.close()
server_soc.close()

(Client)

import socket
mysoc=socket.socket()
mysoc.connect(("127.0.0.1",1234))
s=""
while s!="exit":
 s=input("Enter command")
 mysoc.sendall(s.encode('utf-8'))
mysoc.close()

(I deleted all of the commands related to LED so as to be able to run the scripts in my computer, you'll have to add them to the code again).

answered Jun 9, 2020 at 15:18
Sign up to request clarification or add additional context in comments.

7 Comments

How would either one of those problems account for the observed error message?
Even if I specify the encoding, it doesn't make any difference. I am trying to figure out, maybe some issues with socket configuration.
@PresidentJamesK.Polk I wasn't sure, they were the only errors I found so I pointed them out just in case it solved it.
@PadmaR I've just found the actual error, I hope the edited answer solves the problem.
Cool! Great! So I need to decode the data at the server end. It works now.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.