Solved

Python - creating byte arrays help

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

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…
Surprisingly, there is a lot to Gym battles, and I thought it would be helpful to share knowledge about all the ins and outs of this feature!
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

747 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

12 Experts available now in Live!

Get 1:1 Help Now