RasCEN
Posts: 3
Joined: Wed Sep 18, 2019 9:02 am

creat UDP connection

Wed Sep 18, 2019 9:36 am

hi ,

i want to create a UDP connection between two Raspberry pi, using python in both sides what i want to do.

1- control LED on each side from the other ( each pi control the other pi )

2- send text file to each other

i wish if someone can provide good resources such as a library for socket programming and example

thx.

jahboater
Posts: 4762
Joined: Wed Feb 04, 2015 6:38 pm

Re: creat UDP connection

Wed Sep 18, 2019 12:19 pm

RasCEN wrote:
Wed Sep 18, 2019 9:36 am
i want to create a UDP connection between two Raspberry pi, using python in both sides what i want to do.
You cannot create a UDP "connection", that's not how it works. You probably want TCP (a Connection Oriented Transport Service).

UDP is individual, unrelated, datagrams. Furthermore, there is no guarantee of delivery or even the order of delivery.
So you will have to manage the acknowledgements and sequence numbers etc by hand.

TCP will do all that for you.

You could start by reading "man socket" which is for the C library but the Python library will be similar.

Even with TCP, you may still need ACK's but in this case they would simply confirm that the data has been received and acted upon (perhaps a received text file has been successfully written to disk, or the LED is actually lit).

Heater
Posts: 13603
Joined: Tue Jul 17, 2012 3:02 pm

Re: creat UDP connection

Wed Sep 18, 2019 12:31 pm

There are a million ways to do what you want.

Firstly, what programming language would you like to use?
Memory in C++ is a leaky abstraction .

jahboater
Posts: 4762
Joined: Wed Feb 04, 2015 6:38 pm

Re: creat UDP connection

Wed Sep 18, 2019 12:32 pm

Heater wrote:
Wed Sep 18, 2019 12:31 pm
There are a million ways to do what you want.
Indeed!
Heater wrote:
Wed Sep 18, 2019 12:31 pm
Firstly, what programming language would you like to use?
The OP said Python on both sides.

Heater
Posts: 13603
Joined: Tue Jul 17, 2012 3:02 pm

Re: creat UDP connection

Wed Sep 18, 2019 1:03 pm

Oops, sorry, missed that....
Memory in C++ is a leaky abstraction .

Andyroo

Re: creat UDP connection

Wed Sep 18, 2019 1:33 pm

The Real Python site has a good walkthrough for sockets programming at https://realpython.com/python-sockets/

I would stick with TCP rather than UDP as it is possible the UDP packets get lost (not good in a control system) or out of order (very strange text file could result)...

If you want to try UDP then have a look at https://wiki.python.org/moin/UdpCommunication for some basic examples and https://pymotw.com/2/socket/udp.html for the smallest working code.

Good luck - remember though, you are reinventing the wheel here (file transfer - scp and messaging - MQTT) and being a lazy programmer (i.e. re-using tools) is fine :lol:

User avatar
B.Goode
Posts: 8846
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: creat UDP connection

Wed Sep 18, 2019 2:02 pm

RasCEN wrote:
Wed Sep 18, 2019 9:36 am

i want to create a UDP connection between two Raspberry pi,


Is this a 'homework' or 'project' assignment you have been set?

Because the two sample applications look like the sort of thing that could lead a student to learn at first hand when UDP might be an appropriate or inappropriate means of communication.

Any decent text describing the Python socket library is likely to cover the use of UDP messages.

Return to “Networking and servers”