user16305947
user16305947

Reputation: 53

Multiple TCP socket connections Python

I'm new to socket programming and I'm struggling to connect to and exchange information using multiple TCP sockets.

I have a setup with two development boards written in C and a PC with a Python script. The two boards and PC are connected to a switch with LAN-cables. Both development boards are programmed to send back "OK" as soon as they receive data from the PC.

I want to connect to both of the boards and keep sockets open while I send and receive info with my Python script. If I connect to only one board I can do that without any problems. But if I try to connect to two boards, the connection times out either during establishing the connection or during communication.

Here is my code:

main.py

from board import Board

b1 = Board(1, "192.168.0.254", 5)
b2 = Board(2, "192.168.0.77", 7)

b1.establish_connection()
b2.establish_connection()

while True:
    pass

board.py

import threading
import socket
import random
import time
import select

class Board():
    def __init__(self, nr, host_address, port_nr):
        self.server_addr = (host_address, port_nr)
        self.nr = nr
        self.dt = 10


    def establish_connection(self):
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.settimeout(5)
        print("Trying to establish a connection board {}".format(self.nr), self.server_addr)
        try:
            self.s.connect(self.server_addr)
            threading.Thread(target = self.communication).start()
            print("Connection established board {}".format(self.nr))
            return True
        except socket.timeout:
            print("Not connected. Connetion timeout.")
            return False
        except socket.error as e:
            print("Not connected. " + str(e) + ".")
            return False


    def communication(self):
        while True:
            self.send_data([random.randint(0,500), random.randint(0,500), random.randint(0,500)])
            start_time = time.time()
            while (start_time + self.dt) >= time.time():
                data = self.receive_data()
                if data is not None:
                    print(data)
                    break

    def send_data(self, a):
        x, y, z = a[0], a[1], a[2]
        msg = "S,X%i,Y%i,Z%i,E" %(x,y,z)
        print("Sending msg {0}".format(msg))
        nsent = self.s.send(msg.encode('utf-8'))

    def receive_data(self):
        readable_s, _, _ = select.select([self.s], [], [], 0.001)
        if readable_s:
            msg = self.s.recv(20).decode('utf-8')
            return msg

I also have the same problem when I use Hercules instead of the python script. In the picture you can see that it was able to connect to both of the development boards, but didn't send "OK" back after receiving data. picture

Upvotes: 1

Views: 431

Answers (1)

user16305947
user16305947

Reputation: 53

The problem was not in the python code, but the fact that the boards had a gateway specified. Simultaneous communication started working once the gateway was changed back to 0.0.0.0 in the C code uploaded on the development boards. In addition each board had to have different MAC address.

Upvotes: 2

Related Questions