1

Using UDP, is it possible to have client-to-client communication? I am using the python socket library in the code below. I can send messages over the UDP sockets I create, but I cannot receive messages that are sent to the IP address I have established for a given client.

The process is to:

  1. Run the python script below in two different terminal windows and observe the messages sent between the sockets
  2. I can receive the messages only when I bind to a local host using socket.bind(), but I cannot bind to an external IP address (necessary for facilitating client-to-client information). As a result, I am using socket.connect().

Does anyone have any tips for how I can connect to other clients using external IP addresses and send messages across the UDP sockets?

import socket
import threading
import os

s = socket.socket(socket.AF_INET , socket.SOCK_DGRAM )
s.connect(("198.168.225.242", 2222))


print("\t\t\t====>  UDP CHAT APP  <=====")
print("==============================================")
nm = input("ENTER YOUR NAME : ")
print("\nType 'quit' to exit.")

ip,port = input("Enter IP address and Port number that you want to send messages to: ").split()

def send():
    while True:
        ms = input(">> ")
        if ms == "quit":
            os._exit(1)
        sm = "{}  : {}".format(nm,ms)
        s.connect((str(ip), int(port)))
        s.sendto(sm.encode() , (ip,int(port)))

def rec():
    while True:
        s.connect_ex((str(ip), int(port)))
        msg = s.recvfrom(1024)
        print("\t\t\t\t >> " +  msg[0].decode()  )
        print(">> ")
x1 = threading.Thread( target = send )
x2 = threading.Thread( target = rec )

x1.start()
x2.start()
C.Nivs
  • 12,353
  • 2
  • 19
  • 44
  • Depends on your network. Connecting from one private network into another requires some extra measures. – Klaus D. Jul 08 '22 at 21:30
  • Can you expound on how this could be done in python? – supernova08 Jul 08 '22 at 23:20
  • 1
    Most firewalls and Internet gateways will block incoming UDP packets by default. I suggest getting UDP communication working first on two different instances of your program that are both running on the same computer; then once that works, you can disable the firewalls on two machines on the same LAN and get UDP communication working between instances of your program running on those two machines. Once you have it working across the LAN you can start worrying about UDP across the Internet (which in general won't be easy -- Google "UDP Hole-punching" for details about what's involved) – Jeremy Friesner Jul 08 '22 at 23:27
  • `connect()`'ing UDP sockets doesn't actually establish a link between two peers, like it does for TCP sockets. `connect()` simply assigns a static peer IP to the socket, which allows you to use `send()` and `recv()`, but you are using `sendto()` and `recvfrom()` instead, so `connect()` is useless in this situation. As for `bind()`, you are correct that you can't bind a socket to an external IP, only a local IP. For communication over the Internet, you need to `sendto()` a peer's public IP, and if that happens to belong to a router, that router needs to have port forwarding configured. – Remy Lebeau Jul 09 '22 at 00:28

0 Answers0