Solved

Python - how do I - array of IPAddresses?

Posted on 2014-09-25
3
391 Views
Last Modified: 2014-09-30
Hi
Thanks for getting me this far in my server code.

My clients are all joining, but I need the best way of storing client information for later use.
I basically need an array of structs for the clients, like
struct client {
ipaddress address;
int PORT;
}
Their index in the array of client information would correspond with their client number.
I'm considering having an array of addresses and an array of PORTS, - should that be enough info for the clients?
maybe, also a boolean if they are still connected?

What is the code for declaring an array of ipaddresses?
or, how do I declare an array of tuples? structs?

Thanks
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: 40345239
I can make an array of bytes

bytearray(100)
will give you 100 zero bytes.

How does it work for complex data?
0
 
LVL 17

Accepted Solution

by:
gelonida earned 500 total points
ID: 40345599
If you just want to keep track of this information and if you don't have to store the data in a memory optimized way:


Why don't you just make a list of objects.
or a dict of objects (acting as a sparse array)

Is there any erason you want to declare that array.
WOuldn't it be enough to add new client info whenever you have new data.

class ClientInfo():
    def __init__(self, ip=None, port=None):
        self.ip = ip
        self.port=port

clients = []




to create a new client you would just write
clients.append(ClientInfo('127.0.0.1', 8080)

# next line of you want to keep the index of the client
client_idx = len(clients)-1

please give me more context if I misunderstood your question
0
 

Author Comment

by:beavoid
ID: 40346626
Thanks
I would just like to store the client info once the clients in the game arrive.

If the server expects n clients, the clients may, will arrive out of order.

Last time I did an RTS, in Java, I declared an array of clientInfo objects, because I knew how many to expect.
- And I assign a client a clientNumber corresponding to the order in which they joined?
So the ClientInfo code above goes in the server .py  - even tho it is a class? and the server class code after it?

Wouldn't I also need a ClientNumber parameter? clientnumber?
ClientNumber = clientnumber
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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…

632 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