Solved

Python - creating byte arrays help

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
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 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…

839 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