Solved

Python - how do I - array of IPAddresses?

Posted on 2014-09-25
3
363 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 16

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Game w 3D galaxy - How should I do vertical distancing? 3 73
java continue statement 10 112
How Complex Is This Java Course ? 9 92
linux date/time setting via python 6 54
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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…

930 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

9 Experts available now in Live!

Get 1:1 Help Now