mardi 5 juillet 2016

How to add field or information to UDP socket in Python?

I'm sending an image using UDP sockets and want to add information to the package, such as checksum and sequence number.

Here's the code:

client.py

import socket
import sys

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))

buf = 1024
f = open("image2.png",'wb')

data , addr = sock.recvfrom(buf)
while True:
    print "PACKAGE RECEIVED..."
    f.write(data)
    data, addr = sock.recvfrom(buf)

and server.py

import socket
import sys
UDP_IP = "127.0.0.1"
UDP_PORT = 5005

print "UDP target IP:", UDP_IP
print "UDP target port:", UDP_PORT

sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

f=open ("image.png", "rb")
print sys.getsizeof(f)
buf = 1024
data = f.read(buf)
while (data):
    if(sock.sendto(data,(UDP_IP,UDP_PORT))):
        print "sending ..."
        data = f.read(buf)

How can I add a field to the UDP header or add additional information to UDP socket?

Aucun commentaire:

Enregistrer un commentaire