I am trying to set up a Raspberry pi to transmit data to my PC via UDP. To do this, both devices are connected to my mobile hotspot.
PC IP: 192.168.78.1
RasPi IP: 192.168.78.57
There are two scripts:
UDP Server
import socket
UDP_IP = '192.168.78.1' #Used when PC is server
#UDP_IP = '192.168.78.57' #Used when RasPi is server
UDP_PORT = 5005
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP,UDP_PORT))
data,addr = sock.recvfrom(4096)
print(str(data))
message = "Hello, I am the UDP Server"
sock.sendto(message.encode("utf-8"), addr)
sock.close()
UDP Client
import socket
UDP_IP = '192.168.78.1' #Used when PC is server
#UDP_IP = '192.168.78.57' #Used when RasPi is server
UDP_PORT = 5005
client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
message = 'Hi, this is a client'
client_socket.sendto(message.encode("utf-8"),(UDP_IP,UDP_PORT))
data,addr = client_socket.recvfrom(4096)
print('Server Says')
print(str(data))
client_socket.close()
The client sends a message to the server and receives a message in response.
When I run the server code on the RasPi and the client code on the PC, everything works fine.
However, when I run the server code on the PC and the client code on the Raspi, it does not work.
The server gets stuck on the line data,addr = sock.recvfrom(4096) presumably waiting for a message, while the client gets stuck on the line client_socket.sendto(message.encode("utf-8"),(UDP_IP,UDP_PORT)) presumably trying to send the message.
Can anyone help me explain why the connection works with the RasPi as the server but not with the PC as the server?
Related
I'm trying to create a simple web server with Raspberry Pi 4 - Model B using sockets in Python.
The problem is that I can request from any web browser in my loclal network and the server works, but when I try to request from an external network (different IP of the Raspberry Pi server) nothing happens.
I have read a bit about it and in most of cases the problem was solved writing as IP "0.0.0.0" in the bind method of the server code, but it didn't worked for me... Is there any other error?? please help me, I'm stuck
import socket #Importa la libreria Socket
message = "Hello Internet, I'm Here!!"#Message to send to the client
IP = "0.0.0.0" #Listens to any IP client
PORT = 2020 #Server Port
mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mySocket.bind((IP, PORT))
mySocket.listen(5)
while True:
print("\n"+"Waiting for a client...")
client1, client1_addr = mySocket.accept()
print("Got a request from client")
request = client1.recv(1024)
print("Sending Response...")
http_response = b'HTTP/1.1 200 OK\r\n\r\n'
client1.send(http_response)
byts = bytes(message, 'utf-8')
client1.send(byts) #Envia el mensaje al cliente
print("Response sent!!!")
client1.close()
mySocket.close()
I am establishing a TFTP connection using UDP.
My client is able to successfully send a request to server
cSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
cSocket.bind(('', clientPort))
cSocket.settimeout(1)
cSocket.sendto(key, (Serverip, 69))
And server is able to receive it
serverSock.bind((serverIP, 69))
request, clientIP = serverSock.recvfrom(1024)
Now from anthother port server is responding back to client with an acknowledgement
aSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
aSocket.settimeout(0.1)
aSocket.sendto(byteArray, (clientIP, clientPort))
From Wireshark I am able to see the acknoledgement send from server but
data, addr = cSocket.recvfrom(1024)
Leads to a timeout exception. Where did i went wrong?
I am setting up a basic python application that will listen for UDP packets at a specific port.
I am using an example code found online to begin to familiarize myself with UDP and socket connection.
When I invoke client.py and then server.py - the server does not respond and the terminal remains idle - any solutions for this problem? Below is the basic code I am working with
Client.py
import socket
UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789
Message = b"Hello, Server"
clientSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
clientSock.sendto(Message, (UDP_IP_ADDRESS, UDP_PORT_NO))
Server.py
import socket
UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789
serverSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSock.bind((UDP_IP_ADDRESS, UDP_PORT_NO))
while True:
#data, addr = serverSock.recvfrom(1024)
data, addr = serverSock.recvfrom(1024)
print ("Message: ", data)
When I invoke client.py and then server.py
Well that's your problem--by invoking the client which sends, then later invoking the server which receives, you are preventing the two from communicating. The server needs to be running at the moment the client sends.
I have a server where I have a script which sends UDP packets to my ip address and I have a client script on my PC that receives UDP packets.
#python3.6.1
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('myip', 49999))
data, address = sock.recvfrom(100)
print('Received {}'.format(data.decode('ascii')))
On the server side my scripts connects to ('myip', 49999) and starts sending packets.
I took my ip from ip4.me i tried to bind my socket to '0.0.0.0' too and i tried other ports nothing seems to work.
If that is the server script, i guess it should have a while loop in it:
while True:
data, address = sock.recvfrom(100)
print('Received {}'.format(data.decode('ascii')))
Two computers in a LAN connecting to a wireless router, one IP address is 192.168.1.106 (server), the other one is 192.168.1.107 (client), the gateway on both computer is 192.168.1.1 (the router itself).
The two computer can ping each in two directions which means there should be no problem with routing and the router itself. But I failed when I tried to use Python UDP socket, the server cannot get any information from the client, and same happened when I change the ip address. (But it works fine when server and client are on a same computer using local ip address, so the code is should be ok)
I am using the following code:
server:
import socket
address = ('192.168.1.106', 5678) # the server listening on address 192.168.1.106
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(address)
while True:
data, addr = s.recvfrom(2048)
if data == "empty":
print "no data from client"
else:
print "received:", data, "from", addr
s.close()
client:
import socket
address = ('192.168.1.106', 5678) # the client send to address 192.168.1.106
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
msg = raw_input()
if not msg:
msg = "empty"
s.sendto(msg, address)
s.close()
Did you open the UDP port on the firewall on both comoutera?