Page 1 of 1

Socket problems

Posted: Fri Jun 10, 2016 3:50 pm
by davef21370
I need to send simple strings from a laptop to the Pi over the LAN using WiFi so I figure sockets are the way to go. Unfortunately I have zero experience with them and know next to nothing about the ins and outs. I found these bits of code on stack overflow...
This is the server side...

Code: Select all

import socket
from threading import *

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "localhost"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))

class client(Thread):
    def __init__(self, socket, address):
        Thread.__init__(self)
        self.sock = socket
        self.addr = address
        self.start()

    def run(self):
        while 1:
            print('Client sent:', self.sock.recv(1024).decode())
            self.sock.send(b'Oi you sent something to me')

serversocket.listen(5)
print ('server started and listening')
while 1:
    clientsocket, address = serversocket.accept()
    client(clientsocket, address)
and this the client.....

Code: Select all

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host ="192.168.x.x"
port =8000
s.connect((host,port))

def ts(str):
   s.send('e'.encode()) 
   data = ''
   data = s.recv(1024).decode()
   print (data)

while 2:
   r = input('enter')
   ts(s)

s.close ()
The server runs okay but sits waiting for a connection and the client can't connect so quits with the error...
socket.error: [Errno 10060] A connection attempt failed, etc, etc.

So 2 questions...

First, what's wrong with the code(s) that the 2 can't communicate?
Secondly, how can I have the server doing other stuff while waiting for the client to send some data. This is going to be sending commands to a quadcopter, very little packet size and not too often but the Pi (server) needs to be taking care of other things.

Or is there a better alternative?

Dave.


Edit: Don't think this is relevant but at the moment this is running on 2 Windows laptops.

Re: Socket problems

Posted: Fri Jun 10, 2016 3:56 pm
by joan
192.168.x.x is not an address.

Re: Socket problems

Posted: Fri Jun 10, 2016 4:14 pm
by davef21370
joan wrote:192.168.x.x is not an address.
Sorry Joan, that's what I call "healthy paranoia", those x's were actually something like 1.10.
I've now got the 2 PC's talking by using

Code: Select all

host = socket.gethostname()
on the server but now whatever I send from the client appears as ", u'e" on the server (exclude the quotes) although I do get the "Oi you sent something to me" reply.

Regards.
Dave.

BTW. I'm going to be using your pigpio module to control the ESC's so I may be wracking your brains in the coming months :)

Re: Socket problems

Posted: Fri Jun 10, 2016 4:23 pm
by davef21370
Sorry folks, been a bit gormless....

Code: Select all

def ts(str):
   s.send('e'.encode()) 
   data = ''
   data = s.recv(1024).decode()
   print (data)

while 2:
   r = input('enter')
   ts(s)
.....will only ever send "e" to the server and the input loop takes "r" as the text then send "s" to the function.
I really need to stop blindly copying and pasting :oops:

Dave.


Edit: Also, I'm going to have the quadcopter as the client and have it request commands when it can. Makes sense.