Solved

Python - creating byte arrays help

Posted on 2014-09-23
3
386 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
  • 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 28

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…

777 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