Solved

Python - creating byte arrays help

Posted on 2014-09-23
3
396 Views
Last Modified: 2014-09-24
Hi

I have my TCP connection, server listen code fleshed out.
On the client, though,
I'd like to create a byte array of a certain length, with a tag number.
How do I do this?
basically Java,

byte[] data = new byte[500] // is that the correct size for a TCP network game HELO message? smaller? can be 3 . . .
data[0] = HELO_MSG byte
data[1] = client number byte #1
data[2] = client number byte #2
That's all? no need for more than 256 clients..so, one byte.
The client port, address, etc is gathered from this message.
I can send and listen, but I don't know how to create a blank byte[] of x size and fill it with certain numbers?
byte[] data = new byte[3]
What about in game messages, that can be hundreds of bytes?
0
Comment
Question by:beavoid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:beavoid
ID: 40340797
I found this, that worked over TCP.
s.sendall(b'Hello, world')

Open in new window


Is that right? It didn't explode.
How do I make a bigger byte array for in-game messages? Or are they not big. I'm thinking big because of movements of large numbers of units. Must I keep groupings of units registered in the server?
How can I set the size of each type of message byte array?
Incidentally, when I hardcoded the byte array, my other socket explosions stopped.
0
 
LVL 29

Accepted Solution

by:
pepr earned 500 total points
ID: 40340948
Yes. The b'whatever' (with the b prefix is a literal of the bytes type. It looks like a string literal but the difference is that no encoding is considered. Which is fine if you use ASCII of if you want to use the same encoding for sending the messages as used in the Python source code.

In Python 2, the bytes was the same as the str type. In Python 3, the two types are separated. Python strings are more abstract, and you have to tell the encoding to convert them to bytes (https://docs.python.org/3.3/library/functions.html?highlight=bytes#bytes)

The bytes type is immutable (as also strings are). If you need some extra manipulation -- such as appending other bytes to the message before sending it -- use the bytearray type (https://docs.python.org/3.3/library/functions.html?highlight=bytes#bytearray):
msg = bytearray(b'Hello, world ')
msg.append(0x34)   # just coincicence -- ordinal number of '4'
msg.append(0x32)   # ordinal number of '2'
print(msg)
s.sendall(msg)

Open in new window

0
 

Author Comment

by:beavoid
ID: 40342145
Thanks

bytearray is the answer

am doing

data=bytearray(5)
data[0]= MSG_HELO

and it sends this over TCP the same.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question