0

I have a list contains hex values. I want to send this list over UDP socket and in the other end C program will receive it. So the list should be converted to bytes and send over socket.

I tried the below code:

frames_to_send = [0x00, 0x01, 0x02, 0x03, 0x05]
UDPServerSocket.sendto(bytearray(frames_to_send), ("127.0.0.1", 5555))

it gives me the following error:

Exception has occurred: TypeError
'bytes' object cannot be interpreted as an integer

If I send string over socket, the client application is receiving, so my server client program is working fine... I need guidance on how to send binary data which is list as hex.

6
  • What are the arguments you're trying to pass? Where does the exception occur? We can't know if you don't show us. Please add a minimal reproducible example. Commented Aug 2, 2021 at 11:32
  • Variable 'key' is a bytes object just as the error message indicates Commented Aug 2, 2021 at 11:51
  • aalways put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot, not link to external portal). There are other useful information. Commented Aug 2, 2021 at 12:47
  • I don't have all your code but I can't reproduce this error in sendto. Maybe you have problem in different place - i.e. in recvfrom or when you try to use received data as integer. Full error message (which you didn't show) should show where exactly you have problem. Commented Aug 2, 2021 at 12:55
  • i have solved it. since frames_to_send is list, we have to iterate through the list and send the byte one by one or append all the hex data to a single variable and send it. usage of bytearray in this place is wrong. Commented Aug 2, 2021 at 12:59

1 Answer 1

1

I see you are saying that you can send one byte at a time or append everything to a byte string and send it. However, there's a simpler option, which is just:

UDPServerSocket.sendto(bytes(frames_to_send), ("127.0.0.1", 5555))

In any case, you're not sending anything "as hex". The fact that you use an hexadecimal notation to represent the bytes doesn't mean much, at the end of the day what you send over the UDP socket are raw bytes of data, which could be represented in hexadecimal, decimal, binary, octal, or whichever other base you like.

Sign up to request clarification or add additional context in comments.

4 Comments

still i am getting an error because frames_to_send is list. looks like for sending anything over socket, the datatype of the message should be <class bytes>. so i have iterated over the list and merged the list item to a single variable and send it. it works
@deepanmuthusamy well, bytes([0x00, 0x01, 0x02, 0x03, 0x05]) gives me b'\x00\x01\x02\x03\x05'... are you perhaps using Python 2 and forgot to mention it?
you are right @Marco. it is working. sorry i did not check it correct. Thanks for the solution.
@deepanmuthusamy Note that bytearray(frame_to_send) works as well. You should edit your question to provide a a client/server example the reproduces the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.