Python Forum
First Byte of a string is missing while receiving data over TCP Socket
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
First Byte of a string is missing while receiving data over TCP Socket
#1
Dear All Friends.
I am trying to communicate with an IEC-60870-5-104 server through TCP sockets. The IEC server is located over the local host 127.0.0.1: 2404 and I am trying to send a HEX message with the help of send() function. Please look at my code
#import the socket library 
import socket
  
# create a socket object 
s = socket.socket()          
print("Socket successfully created")
  
# connect to the server on local computer, 2404 default port for IEC-104 Server
s.connect(('127.0.0.1', 2404))
# send a hex message in bytes
s.send(b'\x68\x04\x07\x00\x00\x00')

data = s.recv(1024)

s.close()

print( "received data:", data)
Now when i run the code, it connects successfully with the server, Sends the HEX message which is
0x68 0x04 0x07 0x00 0x00 0x00

In response it receives the hex message 0x68 0x04 0x0B 0x00 0x00 0x00
I confirmed this with Wireshark but what I find on my shell is
======== RESTART: C:/Users/Shahrukh/Desktop/Python Coding/TCP Server.py ========
Socket successfully created
received data: b'h\x04\x0b\x00\x00\x00'
>>>
Only 5 bytes are shown, where is the 1st byte 0x68? I must receive and show 0x68 0x04 0x0B 0x00 0x00 0x00, whats wrong? Please help
Reply
#2
Not sure, but this could be related to big endian, little endian conversion.
Here's a blog on the subject: https://pythontic.com/modules/socket/byt...-functions
Reply
#3
I count 6 bytes. The first byte is a h.
You have a computer and you're using a programming language.
Just use len(data) to get the length.

In [2]: hex(ord('h'))                                                                                                       
Out[2]: '0x68'
The first letter is a h, which is 104 in ascii and unicode, which is 0x68 in hex.

All printable ASCII characters in bytes are represented as printable version and not with the hex value.
If you want to represent the bytes as hex-string, you could use binascii.hexlify()

import binascii

data = b'\x68\x04\x07\x00\x00\x00'
hexstr = binascii.hexlify(data, ' ', 1).decode()
# yes, hexlify return bytes
# the decode step is ugly
print(hexstr)
Output:
68 04 07 00 00 00
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Oh My God, Now I got it, its basically the ascii codes which I am receiving. Thank you very very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help Sending Socket Data snippyro 0 150 Sep-23-2021, 01:52 AM
Last Post: snippyro
  QR code data missing in some of my QR codes Pedroski55 6 1,058 Jan-26-2021, 04:38 AM
Last Post: Pedroski55
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 3,820 Sep-18-2020, 10:10 PM
Last Post: tienttt
  [Selenium]Timed out receiving message from renderer: 10.000 wood_6636 0 1,284 Jun-26-2020, 08:59 AM
Last Post: wood_6636
  'utf-8' codec can't decode byte 0xda in position 184: invalid continuation byte karkas 8 25,132 Feb-08-2020, 06:58 PM
Last Post: karkas
  Receiving XML exception from nmap.scan() results. PythonNmap 4 1,818 Jan-21-2020, 04:41 AM
Last Post: PythonNmap
  Trying to use python-nmap but receiving however python2 or 3 can't find PortScanner. PythonNmap 21 4,137 Jan-19-2020, 07:54 PM
Last Post: PythonNmap
  txt-file: read and append missing data sufi 1 1,244 Dec-07-2019, 08:12 AM
Last Post: Gribouillis
  Unable to do the proper split using re.sub incase of missing data. Karz 1 747 Nov-17-2019, 05:58 PM
Last Post: buran
  Byte string catenation inefficient in 3.7? RMJFlack 13 2,625 Aug-18-2019, 05:19 AM
Last Post: RMJFlack

Forum Jump:

User Panel Messages

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