3

We are working on a project "ByZantine Generals Problem" with Python(socket), we manage to create a successful connection between the server and the two clients (client1, client2). But we didn't know how to create a connection between the two clients , any help ?

Link model project problem : https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/4generalestenientetraidor.svg/400px-4generalestenientetraidor.svg.png

Server.py

import socket


host = '192.168.43.209'  # Standard loopback interface address 
(localhost)
port = 65432        # Port to listen on (non-privileged ports are > 1023)

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

serv.bind((host, port))
serv.listen(5)

while True:
    conn, addr = serv.accept()
    conn.send(b"Attack ")
    data = conn.recv(4096)
    if not data: break
    print (data)

client1.py

import socket

host = '192.168.43.209'  # Standard loopback interface address         
(localhost)
port = 65432        # Port to listen on (non-privileged ports are > 1023)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))



from_server = client.recv(4096)
print (from_server)
client.send(b"I am client 1 :  ")

client2.py

import socket

host = '192.168.43.209'  # Standard loopback interface address 
(localhost)
port = 65432        # Port to listen on (non-privileged ports are > 1023)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port))



from_server = client.recv(4096)
print (from_server)
client.send(b"I am client 2 :  ")
unrealapex
  • 578
  • 9
  • 23
youssef hrizi
  • 303
  • 1
  • 3
  • 13
  • You can't. if `A` is connected to `B` in this model, then `A` is client & `B` is server by definition. – rdas Apr 03 '19 at 13:56
  • 1
    By definition of server and client you can't just go client to client. The best way to do it would be to send a message to the server from one client and have the server forward it to the other client. – Error - Syntactical Remorse Apr 03 '19 at 13:56
  • @DroidX86, we have A server , B and C are two clients , we want B and C communicate , ps : B and C can communicate with the server A. – youssef hrizi Apr 03 '19 at 14:12
  • Not with this model. You might want to look into peer-to-peer communication: https://pypi.org/project/pyp2p/ – rdas Apr 03 '19 at 14:16
  • @Error-SyntacticalRemorse, what should i do exactly? , do you have a code for that ? – youssef hrizi Apr 03 '19 at 14:16
  • @youssefhrizi Something like this may help you: https://stackoverflow.com/a/27139338/8150685. If you don't want to send to all clients (which you probably don't) then store the clients in a list or dictionary instead. When you receive a message from a client interpret it and send it to a client you have stored. Make sense? I could try to write an example if you want. – Error - Syntactical Remorse Apr 03 '19 at 14:38

2 Answers2

5

There are two approaches to make client 1 and client 2 communicate together.

  1. Make them communicate through the server
  • This means they will communicate by just connecting to the server. And the server will forward the message between them.
  • To make this work, you need to make a function and pass their socket object to the function when they connect. In the function, you will just receive data from the client. Every time there is data received, you will broadcast it to all other clients.

Tip: You can add each client's socket object to a list so that you can easily broadcast the message to each client in the network.

  1. Peer to peer communication
  • To communicate in p2p, they don't need to connect to the server. They will just communicate with each other directly. The preferred protocol for p2p communication is, UDP protocol.

If clients are gone exchange secure data like the server shouldn't access it, p2p is the best approach. Because there is no interference of the server while they are communicating.

  • I am working on a similar problem, could you give a hint as far as how to define this function? I'm still very new to the syntax of socket/server stuff in python – Trixie the Cat Apr 10 '22 at 01:30
0

You can do client to client communication through the server with something like this. Note: This is not currently tested because I am not on a computer where I can run this:

The core of this code is from this answer which explains how to send a message to ALL clients: https://stackoverflow.com/a/27139338/8150685

I used a list for clients but you may find it easier to use a dictionary.

clients = [] # The clients we have connected to
clients_lock = threading.Lock()

def listener(client, address):
    print "Accepted connection from: ", address
    with clients_lock:
        clients.append(client) # Add a client to our list
    try:    
        while True:
            data = client.recv(1024)
            if not data:
                break
            else:
                print repr(data)
                # Here you need to read your data
                # and figure out who you want to send it to
                client_to_send_to = 1 # Send this data to client 1
                with clients_lock:
                    if client_to_send_to < len(clients):
                        clients[client_to_send_to].sendall(data)
    finally:
        with clients_lock:
            clients.remove(client)
            client.close()