[Networking] (Client) Unique User Identification Number

Hello JGO before I dig in on this I was wondering how I should go about giving each client that connects a Unique ID.
(Abstract / importable networking kit)

Not like:
Client #0 ID: 937
Client #1 ID: 238

But rather in 1 + 1 order
:
Client #0 ID: 0
Client #1 ID: 1

The part that I think will confuzzle me is when a client disconnects.
Before I post any code that will probably make no sense, I’d like some feedback on a good approach to take on achieving this :slight_smile:

Thanks JGO, hope somebody can help ~_^

My only idea so far 0_0

Pseudo-Code:


			// clients[0] = 0
			// clients[1] = 1
			// clients[2] = 2
			// clients[3] = 3
			// clients[4] = 4
			// clients[5] = 5
			
			// clients # 3 disconnects:
			// 100 clients online
			for (int i = 0; i < clients.length; i++) { 
				final Client c = clients[i];
				if (c.getID() >= 3) {
					for (int j = 3; j < 100; j++) {
						clients[j].ID -= 1;						
						clients[j].update();
					}
				}
			}

If you were to use an ArrayList instead of array, couldn’t you easily reorder the IDs without any extra work on your side? The remove method will easily do this. Like:


ArrayList<Client> clients = new ArrayList<Client>(100); //initial capacity of 100 but I prefer 0 and just let it increase and decrease capacity as necessary
//client #3 disconnects
clients.remove(3); //removes client 3

clients.get(3).getID() would now equal 4.

That’s what I was thinking too. You could remove a client and then all of the other id’s would automatically drop one.

Just thought of a neat solution: when a client disconnects, if there are ID’s created after it, add that client’s number to an ArrayList in sorted order. If the ArrayList is not empty, grab and remove the first number to hand out. Otherwise, assign the next value in order.

Wow thanks haha, I didn’t think it would be self sustaining. (As in automatically decreasing the “ID”)

-Implementing / Testing it now.

Thanks mate, never knew you could set the initial capacity of a List :slight_smile:

Great, thanks I got it guys.

The pseudo-code I posted above earlier worked :slight_smile:


	/* Method implemented for calling other functions besides disconnected */
	void clientDisconnected2(ClientModel clientModel) {
		clientDisconnected(clientModel); // abstract method
		System.out.println("Client " + clientModel.test_name + " disconnected");
		for (ClientModel c : getClients()) {
			if (c.getID() > clientModel.getID()) {
				c.setID(c.getID() - 1);
				c.sendMessage("A Client has dc'd your new ID is " + c.getID() + ".");
			}
		}
		getClients().remove(clientModel);
	}

I didn’t think it would be that easy 0_0!

It might work, but switching object identifiers in a running program is not a good idea. Bad for consistency, understanding, debugging, etc.
Uniqe identifiers and list indexes are two things and should not be mixed up.

This. If you want unique IDs then don’t change them in the middle of execution :stuck_out_tongue: I’d go with ra4king suggestion of assigning each client an ID on connecting (0, 1, 2, 3, etc.) then when someone leaves their ID gets added to a sorted list that you can pull from the next time somebody connects.