i am trying to send a message through UDP (a list of dictionaries that i used json.dumps on it) and i get this error:
OSError: [WinError 10040] A message sent on a datagram socket was larger than the internal message buffer or some other network limit, or the buffer used to receive a datagram into was smaller than the datagram itself
This is the client side code:
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (SRVR_NAME,DST_PORT)
packet_info = json.dumps(packet_info)
packet_info = packet_info.encode()
sock.sendto(packet_info,server_address)
sock.close()
and this is the server side code:
listening_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = (IP, PORT)
listening_sock.bind(server_address)
client_msg, client_addr = listening_sock.recvfrom(MSG_SIZE)
d = json.loads(client_msg)
d = d.decode()
print(d)
My psychic powers suggest you are trying to put more than 64KB of data into a single UDP packet.
Maximum size of an IP packet including all headers is 65535 bytes. IP and UDP headers combine for at least 28 bytes. So the max size of the data portion of a UDP datagram is 65535-28 == 65507.
Check the size of your encoded packet_info before sending. If it's too big to fit, then split into multiple messages and handle accordingly.
Related
I have the following UDP / DGRAM socket in Python:
sock.bind((UDP_IP, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
This code is running in a proxy server, so destination IP and port is not matching the socket. I'm using tproxy to intercept the packets.
How can I get the destination IP and port, not the source IP and port?
from the docs of
socket.getsockname():
Return the socket’s own address. This is useful to find out the port number of an IPv4/v6 socket, for instance.
so you'd want to print out sock.getsockname()
I have a radar and I want to read the UDP packets that are sent by the radar via ethernet port.
So I created this script :
import socket
UDP_PORT = 2700
interface=""
sock = socket.socket(socket.AF_INET, # Ethernet
socket.SOCK_DGRAM) # UDP
sock.bind((interface, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
But when I runt it, I receive strange messages like this:
In the user manual of the radar, it says that the packet consists of the following fields:
PacketType:(unsigned byte)
TargetCount: (unsigned byte)
PacketID: (unsigned int32)
Reserved: (unsigned byte[32])
Targets: (RadarTarget[TargetCount]: An array of the detected targets.
How can I read this field from the UDP packet please???
Thank you in advance
Ok im going to try an explain what is going on here... I have a network of multiple same type devices. I have a program that runs on any pc on the network that discovers these individual devices and categorizes them by ip, name, mac, etc.. This program allows for configuration of each device. The devices broadcast a udp packet to "255.255.255.255" with the information for discovery. I can run wireshark and intercept the packets broadcasted from the devices. I have a python program that will broadcast udp packets with data of my choosing.. Now.. This stems from me learning python and my project oriented approach.. I learn better this way :). Ok that being said.. My idea is to broadcast the exact udp packet that another device broadcasts, which in turn should land me on the discovery software as a particular network device.. By following udp stream in wireshark i can copy the data and enter it in my python program and broadcast it on the network. I can broadcast to any destination ip and see it in wireshark but when i try and send it to 255.255.255.255 it never shows up. Now i understand that routers will not forward 255x4 broadcasts pass the local network. When i run the discovery program i can see all the devices broacasting their packets to 255x4 but not the packet originating from my pc. Any ideas would be greatly appreciated.
Python Code:
import udp
import socket #for sockets
import sys #for exit
# create dgram udp socket
try:
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error:
print 'Failed to create socket'
sys.exit()
host = '255.255.255.255';
port = 55558;
while(1) :
msg = '''...z..
hrQT.b.......hrQT.b
.....w...NanoStation M2...N2N
..Test......"XM.ar7240.v5.6.2.27929.150716.1201........NanoStation M2'''
try :
#Set the whole string
s.sendto(msg, (host, port))
# receive data from client (data, addr)
d = s.recvfrom(1024)
reply = d[0]
addr = d[1]
print 'Server reply : ' + reply
except socket.error, msg:
print 'Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
To receive UDP packets, you need to bind the socket to the IP address and UDP port that you want to receive packets on.
1 import socket
2
3 UDP_IP = "127.0.0.1"
4 UDP_PORT = 5005
5
6 sock = socket.socket(socket.AF_INET, # Internet
7 socket.SOCK_DGRAM) # UDP
8 sock.bind((UDP_IP, UDP_PORT))
9
10 while True:
11 data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
12 print "received message:", data
I would recommend using different UDP sockets for sending and receiving packets.
I am sending this custom packet over a TCP socket
pkt = IP(len=16384, src='192.168.240.243', dst=ip,
id=RandShort(), ttl=64)/TCP(sport=5000,
dport=5000, flags="S", window=200,
options=[('MSS', 1460), ('WScale', 2)])/CustomLayer(type=1, update=2)/"SENT"
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, 5000))
except socket.error:
print 'User not connected'
spkt = str(pkt)
s.send(spkt)
The packet is correctly sent and received, but I can't send more than one. If I try for example to include this in a while(i<10), only one packet is received, the others are marked as [TCP Retransmission] looking in Wireshark.
How can I send the packet more than once within the same socket?
Your packets have the same ID, so they are the same packets, and therefore discarded as duplicates.
I am able to send and receive UDP messages in separate programs, but I'm not able to do the same task in one program.
import socket
UDP_IP = "192.168.1.178"
UDP_PORT = 8888
msg = 'test'
print "UDP target IP: ", UDP_IP
print "UDP target PORT: ", UDP_PORT
print "Message: ", msg
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.sendto(msg, (UDP_IP, UDP_PORT))
UDP_IP2 = "192.168.1.198"
sock.bind((UDP_IP2, UDP_PORT))
while True:
data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
print "received message:", data
With this program, I am able to send UDP messages, however, I am not able to receive any messages from the other machine.
What am I doing wrong?
Thanks in advance,
Mikkel
In your example you try to bind socket addr after sending, what's wrong.
Address can be bound to socket only before any data transfer.
If there is no explicit bind OS sets any free (unused) port number in range [1024, 65535] on first .send()/.recv() call.
Next, socket can be bound only to single IP (except special case '0.0.0.0' which means "all host's interfaces").