Solved

Python - creating byte arrays help

Posted on 2014-09-23
3
378 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Which Eclipse should I install now, Java, Python? 1 120
Python multiple IF statements 4 76
copyEndy  challenge 15 86
Path to Python 9 110
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now