On 1/11/2016 5:43 PM, Wang Weijun wrote: > >>> >>>> You want to start a server on the same port on and on? >>>> >>> Not actually. Different port are used for different server socket. The >>> logic looks like: >>> // define four slots for the ports >>> int serverPorts[] = new int[]{0, 0, 0, 0}; >>> >>> // for each slot, create a server socket, >>> // and assign the actually used port for each slot. >>> for each slots run a thread { >>> SSLServerSocket sslServerSocket = ... >>> serverPorts[nextPort] = sslServerSocket.getLocalPort(); >>> ^^^^^^^^ >>> } >>> >>> // use the actual port for each slot in client side >>> if (server ready) { >>> connect to the server socket (host:port) >>> } > > Why do you need a serverPort parameter in doServerSide()? Can it just always > be 0? > It is always 0 before the server socket generated. But after the server socket generated, need to remember the actual port so that client can connect to the port.
>>> >>> This fix is trying to increase the createdPorts value properly. It is >>> not actually need to synchronize serverPorts. > > I see. Why not just synchronized on this and add a small comment? > Synchronized on any object is OK. I will add a comment like: // set the server port and increase the count synchronized Thanks, Xuelei