Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Python - how do I - array of IPAddresses?

Posted on 2014-09-25
3
Medium Priority
?
419 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
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
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…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…

926 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