umair durrani
umair durrani

Reputation: 6179

How to send the data from one computer to another device on the same WiFi network via python socket program?

Objective

I receive data from a driving simulator in real-time via a socket program. The data are vehicle position over time that can be printed out in a shell on the same computer which also runs the driving simulator.
I want to send these data to another device (laptop) which is on the same Wifi Network as shown below. This last part is what I can't figure out and need help with. My attempt is explained below.

enter image description here

Socket Program to receive data from the driving simulator

The following is a socket program that receives the data from the simulator and then prints it:

import socket
import struct

UDP_IP = "127.0.0.1"
UDP_PORT = 4001

sock = socket.socket(socket.AF_INET, # Internet
                     socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    fields = struct.unpack_from('=ddd', data)
    print(fields[0],fields[1],fields[2])

Output

The printed data appear as follows in a shell:

enter image description here

What I have tried:

I understand that I need to use the IP address of the laptop to send the data from the driving simulator computer. However, I am not sure where to specify that in the above code.

For my own understanding, I created client and server .py files and then put the server file on the driving simulator computer and the client file on the laptop. Then run the server.py and then run the client.py. client.py waited for a few seconds but times out.

client.py

import socket

HOST = '137.207.13.91'  # The server's hostname or IP address  (changed to hide actual IP)
PORT = 9000        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)

print('Received', repr(data))

server.py

import socket

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 9000        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

How can I send the data to laptop?

Upvotes: 3

Views: 1861

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54733

You have two very different concepts there. Your first example uses UDP, your second example uses TCP.

TCP is point to point. The client must know the server's address in order to make a connection. Once connected, either side can send or receive, but you have to have the address to start.

UDP is not like that. A UDP server can broadcast (to 0.0.0.0) without knowing a specific IP address. Any computer on the same subnet can listen for that port and receive the broadcasts. That's what your first example did. The disadvantage is that only one process on a given computer can listen on a UDP port.

So, it depends what you need.

Upvotes: 2

Related Questions