Data over Ethernet raspberry pi - python

I am using raspberry pi 4 model B, and I am trying to communicate with my pc using Ethernet. I wrote a python code that create a socket with the following:
import socket
UDP_IP = "127.0.0.1"
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
This code worked over the wifi, so I googled it and I found that we should use “socket.AF_PACKET” instead. Does someone have any idea?
Also I noticed that there is 2 ip when I write ifconfig, one is under lo and the other is under wlan0.

lo, 127.0.0.1 is loopback and is only applicable to the system it is seen on. Virtually all systems will have one. You should use the IP address from wlan0.

Related

Python Socket does not receive UDP packets

I am currently setting up a python socket for receiving UDP packets via ethernet and have problems with configuring the socket for obtaining data.
The ethernet adapter is configured with a fixed IP address. Using tcpdump, I am able to view the incoming UDP packets (as seen here) on port 4098, IP 192.168.33.30.
I am using the following example code for the socket:
import socket
UDP_IP = "192.168.33.30"
UDP_PORT = 4098
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(4096)
print("received message: %s" % data)
Unfortunately, there is no data return, even if the UDP packets are constantly send to the device. I am using a NVIDIA Jetson NX with Ubuntu 18.04.
So far I have checked the firewall and disabled it completely, but things did not change.
I am thankful for every advice on further troubleshooting!

binding a UDP port and listening for connections

When I try to ping a Minecraft server via LAN, the documents say the following:
In Singeplayer there is a function called "Open to LAN". Minecraft (in the serverlist) binds a UDP port and listens for connections to 224.0.2.60:4445 (Yes, that is the actual IP, no matter in what network you are or what your local IP Address is)" ....... client side, bind a UDP socket and listen for connections. You can use a MulticastSocket for that.
I tried to implement this in Python in the following way:
import socket
UDP_IP = "224.0.2.60"
UDP_PORT = 4445
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
print("received message: %s" % data)
Which gave me an OSError: [WinError 10049] error.
Please help :( I have no idea what's wrong with my code.
By the way, sending packets to that port works, and the fake server shows up on the Minecraft app.
You cannot bind to a multicast address like that. It is somewhat more involved.
I recommend to read this article that explains all the details of using multicast with Python.

python socket does not connect to other networks

hi guys i'm studying socket in python, i'm having a hard time connecting with other machines
I have this simple code
import socket
host = ''
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, 222))
print('wait...')
sock. listen(1)
conn, addr = sock.accept()
print('connected')
the code above is a server, I try to connect using this simple code
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.0.14', 222))
when I execute the client code, nothing happens, it is in an infinite wait, neither the server responds nor the client responds.
obs: this is my goal to connect to an external network on my network. The server code is running on another network, I want to connect to another network
I ran your code, and it's basically correct. But I don't think your port is perfect, it should be at least 1024.
You should make sure that the port(222) in firewall on your server computer is open.
I changed your port to 12345, it works on my computer.

Directing traffic from USB(3.0) port to Ethernet output

I am using an ASUS Tinker Board Single Board Computer(SBC) and am trying to direct all traffic into the USB(3.0) port and output it to the next device over the SBC's Ethernet connection as a UDP packet. The input data into the USB port is coming from an audio-to-USB adapter, as PCM samples. I have written the below code however this only works if the USB port on the SBC has a network address (which it does not).
import socket
UDP_IP_RX = "local_host"
UDP_PORT_RX = 5005
UDP_IP_TX = "local_host"
UDP_PORT_TX = 5006
sock_RX = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_TX = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock_RX.bind((UDP_IP_RX, UDP_PORT_RX))
while True:
MESSAGE, addr = sock_RX.recvfrom(2062)
sock_TX.sendto(MESSAGE, (UDP_IP_TX, UDP_PORT_TX))
I understand that the USB port can't have a network address, however I have searched and can't find how I can direct traffic into the USB to somewhere else. The example code I know does not work but wanted to give insight as to what I am trying to do. Is there any way that I can direct the PCM samples from the USB port (input to SBC) to the Ethernet connection (output from SBC)?Thanks in advance.

How to read arduino data output using python via internet?

I have arduino project using uno and ethernet shield. I wan't to print arduino data output in python. I have done try this case using pySerial python and successful.
Then now, I wan't to try my python can read arduino data output over internet, not serial python.
Topology:
Arduino with eth. shield--->switch<---laptop
*Note: the switch have internet connection from my router.
Can you help me, guys?
The IP that you use here should be "localhost" since only the arduino needs the address of the computer and not the other way around. Your Python script is just listening for anyone to connect. Try this:
import socket
UDP_IP = "localhost" # this computer
UDP_PORT = 3939 # the port that the arduino should connect to
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data

Categories