java - Limiting the number of connections to a server -


i have multi-threaded server in java, want limit number of connected clients 2. it's basic application, being used testing.

on server have int userno attribute assigns clients value of either 0 or 1.

my question is, there better way of handling this. want 2 clients connect, , want application ignore further requests.

pseduo code:

    if(userno == 0) {     player 1;     }     if (userno == 1) {     player 2;     }     else {     nothing     } 

i this:

int connectedclientcount = 0; // ... while(true) {     serversocket ss = ...     socket s = ss.accept();     if(connectedclientcount == 2) {         // stuff tell connected client rejected because of max clients...     } else {         connectedclientcount++;         // cool stuff...     } } 

and somewhere else in code (which gets executed on client disconnect)

public void clientdisconnected() {     connectedclientcount--; } 

because of sake of simplicity don't use thread synchronization in example..


Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -