Solved

Python - how do I - array of IPAddresses?

Posted on 2014-09-25
3
354 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
Comment Utility
I can make an array of bytes

bytearray(100)
will give you 100 zero bytes.

How does it work for complex data?
0
 
LVL 16

Accepted Solution

by:
gelonida earned 500 total points
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Installing Python 2.7.3 version on Windows operating system For installing Python first we need to download Python's latest version from URL" www.python.org " You can also get information on Python scripting language from the above mentioned we…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
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 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 …

743 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

8 Experts available now in Live!

Get 1:1 Help Now