Reputation: 1
I am receiving the data stream(46 Bytes) from external hardware(Zigbee receiver) to PC on a TCP/IP socket. Then by using Python programming,iam trying to extract the message from the received data stream from TCP/IP socket.
my python source code and extracted data from the data stream, given in the bellow link
https://docs.google.com/document/pub?id=1pYASqImWm4HkKrDbeeal8fTBsh_GojBQLXixxbMQBlU
As we know "TCP/IP operate on stream of data, never by packets"
i could able extract the data, if i received the complete packet in a single stream. but some times, the single packet is received in two data streams(broken), that time i couldn't extract my message from the stream.
i am not more familier with python programming, so can anyone help me to slove this problem.
I am looking for any good example(programming) to manage the breaking of incomming data stream on a TCP/IP socket, by storing those stream in a buffer as complete packet and then extract the message by using delimiters.
Thanks in advance
Upvotes: 0
Views: 2394
Reputation: 3650
This should work:
import socket
def connect(addr):
con = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
con.connect(addr)
return con
addr = ('127.0.0.1', 7777)
client_socket = connect(addr)
packet_size = 46
data = ""
while True:
while len(data) < packet_size:
d = client_socket.recv(1024)
if not d:
client_socket.close()
client_socket = connect(addr)
else:
data += d
packet, data = data[:packet_size], data[packet_size:]
lqi = ord(packet[27])
...
Upvotes: 1