Python Forum
Python:How to read a recived message that idinfies the client Byte by Byte using UDP
Thread Rating:
  • 2 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python:How to read a recived message that idinfies the client Byte by Byte using UDP
#1
I am trying to implement a simple scenario in python:

The server waits for the client to send a fixed size(8 Bytes) message that identifies the client over UDP and interprets it as follows :

Byte1:represents message ID can takes values (0-255)

Byte2: Reserved value always 0

Byte3&4: integer data values (0-6000)

last 4 Bytes: another integer data

so my question is what is the best way to design such "conventional protocol" between the server and the client?

I think it is called key/value pair but I do not think it is needed in my scenario plus I do not want to use a dictionary. I want to construct the packet on the client side and the server should know how to extract the bytes and stores them for further processing. .

Note: based on the info in the message(ex the ID and timedata) the server should close the UDP connection and starts a TCP connection with the client for further communication.

Below is my code so far.It is working but, sure this is not the "professional" way to do that.

server side:
import socket
import pickle
serversocket=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
serversocket.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
serversocket.bind((socket.gethostname(),4449))
serversocket.settimeout(1)
Datasend=('connection accepted')
#msg = bytearray()

def TCPconnection ():
    print "trying to start a tcp session"
    serversocket2=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    serversocket2.bind((socket.gethostname(),5000))
    serversocket2.listen(1)
    while True:
        clientsocket,adrr=serversocket2.accept()
        print "got connection from :",adrr
        clientsocket.send("confirmed TCP connection")
        data=clientsocket.recv(1024)
        print "Recived back from  TCP client",data
        clientsocket.close()


while True:
    try:
        rmesg,addr=serversocket.recvfrom(1024)
        if rmesg:
            rmesg=pickle.loads(rmesg)
            print rmesg[0],rmesg[3]
            if rmesg[0]==100 and rmesg[3]==101:
                serversocket.sendto("connection accepted", addr)
                print "sent connection accpted"
                TCPconnection()

            else:
                serversocket.sendto("connection refused", addr)

            serversocket.close()
            break
    except Exception as e:
        print ("wating for client to connect..Reason is :",e)
client side:

import socket
import time
import pickle

Type=100 #message type can be up to 255 =1byte
ignore=0#the seconnecbyte has no significan represntation
time=4 #specify a time data can be up to 6000 sec =2bytes
ID=101 #ID  4bytes
Datasend=[Type,ignore,time,ID]
Datasend = pickle.dumps(Datasend)
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def TCPconnection():
    print "trying to set a Tcp sesion"
    clientsocket2=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    while True:
        try:
            clientsocket2.connect((socket.gethostname(),5000))
            data=clientsocket2.recv(1024)
            print "Recived a TCP response from server",data
            clientsocket2.send("thank you server")
            clientsocket2.close()
            break
        except Exception as e:
            print ("try again...reason",e)

while True:
    clientsocket.sendto(Datasend,(socket.gethostname(),4449))
    clientsocket.settimeout(1)
    try:
        DATA,addr=clientsocket.recvfrom(1024)
        if DATA:
            print"Recived response from server:",DATA
            if DATA=="connection accepted":
                clientsocket.close()
                TCPconnection()
                break
            else:
                print"connection refused closing"

        clientsocket.close()
        break
    except Exception as e:
        print ("try again ..reason:",e)
Any solution ideas,resources or notes are highly appreciated.
Reply
#2
Why not follow PEP8?
Reply
#3
import msgpack

id_ = 44
byte2 = 0
data_values = 6000
the_last_4_bytes = 42

package = [id_, byte2, data_values, the_last_4_bytes]
masgpack.packb(package)

# send the data
On the other side:

import msgpack

# receive the UDP datagram
data = msgpack.unpackb(package)
# process the data
msgpack
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  asyncio byte data transfer gary 5 2,070 Nov-18-2022, 02:45 AM
Last Post: Skaperen
  How can i create a server for already existing client using Python? Chapanson 21 7,437 Aug-19-2020, 09:12 AM
Last Post: DeaD_EyE
  Python script multi client server sonra 1 2,456 Mar-24-2020, 03:49 PM
Last Post: Larz60+
  Python server(Django web site)/client(Python app) connection Junior_Pythoneer 5 3,792 Jul-05-2019, 05:41 PM
Last Post: noisefloor
  recive post request from client(browser) python sockets kunz 1 2,787 Dec-13-2018, 12:53 AM
Last Post: kunz
  Sending/Receiving Multiple Message from Server or Client Lyperion 0 10,453 Jul-30-2018, 07:52 AM
Last Post: Lyperion

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020