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

10,139

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.

Share:
10,139
youssef hrizi
Author by

youssef hrizi

CMS: Drupal 8, WordPress Php framework: Symfony 4 ( REST API)

Updated on June 04, 2022

Comments

  • youssef hrizi
    youssef hrizi almost 2 years

    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 :  ")
    
    • Error - Syntactical Remorse
      Error - Syntactical Remorse about 5 years
      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.
    • rdas
      rdas about 5 years
      You can't. if A is connected to B in this model, then A is client & B is server by definition.
    • youssef hrizi
      youssef hrizi about 5 years
      @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.
    • rdas
      rdas about 5 years
      Not with this model. You might want to look into peer-to-peer communication: pypi.org/project/pyp2p
    • youssef hrizi
      youssef hrizi about 5 years
      @Error-SyntacticalRemorse, what should i do exactly? , do you have a code for that ?
    • Error - Syntactical Remorse
      Error - Syntactical Remorse about 5 years
      @youssefhrizi Something like this may help you: 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.
  • Trixie the Cat
    Trixie the Cat about 2 years
    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