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')))
Related
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?
I'm trying to receive UDP Broadcast packets sent from FPGA connected via a LAN cable. the FPGA sends continuous packets to port 5001.
My python receiver code is simple:
from socket import *
s=socket(AF_INET, SOCK_DGRAM)
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
s.bind(('', 5001))
print "trying to receive"
msg = s.recvfrom(1024)[0]
print msg
print "I'm outta here! Bye!"
I checked using Wireshark, and I found that the PC receives the packets. However, my Python code doesn't. I also checked sending packets from another local python code (to the same address and port) and my receiver got those packets.
Wireshark captures:
The issue was the firewall permissions for python
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?
I've read something about port translation and now I want to test it.
I have a local machine behind a NAT router and a server with external IP address.
This is how I send packet from 5000th port on my machine to 4000th port on the server.
import socket
import sys
UDP_IP = #external server IP address
UDP_PORT = 4000
MESSAGE = "Hi!"
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind(('0.0.0.0', 5000))
sock.sendto(MESSAGE, (UDP_IP, UDP_PORT))
Right after that I start to listen 5000th on local machine
import socket
import sys
UDP_IP = #my ip address in the local network
UDP_PORT = 5000
sock = socket.socket(socket.AF_INET, # Internet
socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024)
print "received message:", data
On the server when I see incoming UDP from (someIP, somePort) I send response to the same someIP and somePort (use the same scripts with other port and address). But I never receive this response on my local machine. Why?
Also, this code is correctly work when server is in the local network.
The problem is that you are behind the NAT, the packet that you are sending to the server(which is external to the NAT) will have the source IP of the NAT server. The reply that the external server would send would have the destination IP of the NAT. When a reply comes to the NAT, it does not know what to do with that packet as there would be no address/port mapping available.
You should create a mapping on NAT saying the following
NAT Address:5000 <---> localaddress:5000
In this case the NAT would know that if it receives a packet at port 5000, it has to send that packet to you local machine.
I've been in a similar situation (not getting responses from the server via UDP while client being behind the NAT), and what helped in my case was sending responses from the same port of the server that requests had been sent to. Different types of NATs work differently, and in my case the router must have built a strict mapping client:CLIENT_PORT <---> server:SERVER_PORT, so "responses" from the different port of the same server were declined. Maybe your case, too.
I have set up a server socket (plain raw socket) listening on port A. A client now connects to this server. OS opens up a port for the client for this purpose. Say port B is allocated to this client. Now my question is, can a 3rd script connect to this port B and send data. Or in other words can I spoof a response to the client as if it was coming from the server? I tried spoofing it using scapy, but it wasnt working.
server.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("localhost", A))
s.listen(10)
ns, cli_addr = s.accept()
time.sleep(30) # so that i can trigger my 3rd script
goodclient.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", A))
print s.getsockname() # to get the local port of client - B
s.recv(1024)
badboy.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", B)) # connection refused error
s.send("hihihi")
scapybadboy.py
pack = IP(src="localhost", dst="localhost") / TCP(sport=A, dport=B) / "Hello"
send(pack) # Packet sent but not received by the client
Because server and client using SOCK_STREAM sockets, they both aware of TCP session(including port, IP and (SEQ_NUMBER,ACK_NUMBER)), so when session is already in process, you will have to perform TCP hikacking and IP spoofing in order to send messages in stream.
In other words, you will have to guess(or steal) ACK number of server in order to send fake messages to client using badclient.
However, if you will make somehow goodclient answer you and not a server you should run the following:
iptables -A FORWARD -j NFQUEUE --queue-num 1 , because your operating system doesn't know about session that you just "opened" with goodclient and it will send RST packet. This command will prevent it.