Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :  ")
like image 878
youssef hrizi Avatar asked Mar 09 '26 19:03

youssef hrizi


1 Answers

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.

like image 84
Abdella Solomon Avatar answered Mar 12 '26 10:03

Abdella Solomon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!