Oct-26-2017, 11:31 AM
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:
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.