youssef hrizi
youssef hrizi

Reputation: 303

How to send and receive message from client to client with Python (socket)?

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 :  ")

Upvotes: 3

Views: 20031

Answers (2)

Abdella Solomon
Abdella Solomon

Reputation: 76

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.

Upvotes: 5

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()

Upvotes: 0

Related Questions