0

I'm trying to get a socket connection between two raspberry pis. One will be client, the other will return whatever the client send out back to client. However, when I execute it, it returns that client can't connect to the server and stopped on the line "s.connect((serverMACAddress,port))".

Been working on this for a whole day and got no where, any help appreciated.

Here's the client code

import os
import platform
import socket
import sys
import json

def errorinput():
    print("Server.py:")
    print("       python3 ./client.py -a pull <query expressions>")
    sys.exit(1)

def main():
    #check if the arg is in right format
    if len(sys.argv)<=1:
        errorinput()
    elif sys.argv[1] != "-a" :
        errorinput()
    elif len(sys.argv) >= 3:
        if sys.argv[2] != "pull":
            if sys.argv[3] != "-s" or sys.argv[5] != "-m":
                errorinput()
        else:
            if len(sys.argv) !=4:
                errorinput()
    else:
        errorinput()

    #establish connection via bluetooth
    serverMACAddress = 'xx:xx:xx:xx:xx:xx'
    port = 3
    size = 1024
    s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
    s.connect((serverMACAddress,port))
    data = "100"     #send "100" to the server side as a test string
    s.send (data)
    receiveddata = s.recv(size)
    s.close()
    print (receiveddata)        

if __name__ == "__main__":
    main()

And here's the server side code (return whatever message received back to the client)

import socket
import sys
import time

def main():
    hostMACAddress = 'B8:27:EB:AA:49:DF'
    port = 3
    backlog = 5
    size = 1024
    s = None

    try:
        # Create a Bluetooth socket
        s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
        # Bind the socket to the port
        server_address = (hostMACAddress, port)
        s.bind(server_address)
        s.listen(backlog)

    except s.error as e:
        if s:
            s.close()
        print("Error: Cannot open socket: " + str(e))
        sys.exit(1)
    while True:
        print('waiting for message')
        client, client_address = s.accept()
        data = s.recv(size)
        if data:
            client.send(data)
            client.close()

if __name__ == "__main__":
    main()
SYM2RR
  • 31
  • 2
  • 8
  • Hi, the issue seems to be similar to this: http://stackoverflow.com/questions/14618277/rfcomm-without-pairing-using-pybluez-on-debian/14827036#14827036 . Here, it's suggested to disable pnat in /etc/bluetooth/main.conf . Hope this helps.. let us know if it worked or not. – aksonlyaks Oct 23 '16 at 06:02
  • Hi there, thanks for the reply. I tried it and it result in a different error says No route to host. – SYM2RR Oct 23 '16 at 17:10

0 Answers0