Using TCP or UDP to control PsychoPy experiments

There is nothing specific to PsychoPy here: just look at a general guide to programming sockets in Python, e.g.

If you have done this before in C++, I imagine the principles will all seem very familiar. It only takes a few lines to create a socket and send data on it, the only tricky thing sometimes is remembering to turn strings into bytes:

That is usually determined by the demands of what is expected at the receiver end. But I’d say if you have a choice, go with UDP. It is easier to program: you don’t need to maintain a connection, all you need to send to a target is its IP address and the port number it is listening on. Conventional wisdom says UDP packets aren’t as reliable as communicating via TCP, as receipt isn’t guaranteed (UDP is designed for streaming applications like audio and video, where if a pocket is dropped, it just gets ignored. TCP is more complex, and its meta data means packets can be reconstructed into the correct sequence if they are received out of order). But in a lab environment, where you can ensure there isn’t much competing traffic on the network, UDP packets will be received, and promptly. And you are just generally sending small commands or data values, and not constructing a large data structure, which is what TCP is best for.
I’ve found UDP to be very reliable in controlling an eye tracker, for example.