EsotericSoftware / kryonet

TCP/UDP client/server library for Java, based on Kryo
BSD 3-Clause "New" or "Revised" License
1.82k stars 419 forks source link

Get Server Ports #88

Closed Rolleander closed 8 years ago

Rolleander commented 9 years ago

Hi,

I started a Server with:

Server server= new Server(); server.bind(0,0); server.start();

(tpc and udp port 0, so it searchs free ports) Where do I find now the tcp and upd port, the server uses? I can only read inet addresses from client connections to the server, but not the server itself.

Thanks!

NathanSweet commented 9 years ago

How would you get the port the server is running on to the client so it can connect?

On Tue, Mar 31, 2015 at 4:01 PM, Rolleander notifications@github.com wrote:

Hi,

I started a Server with:

Server server= new Server(); server.bind(0,0); server.start();

(tpc and udp port 0, so it searchs free ports) Where do I find now the tcp and upd port, the server uses? I can only read inet addresses from client connections to the server, but not the server itself.

Thanks!

— Reply to this email directly or view it on GitHub https://github.com/EsotericSoftware/kryonet/issues/88.

Rolleander commented 9 years ago

Thats the problem, I have a main server with constant IP + port. The main server opens new sub-server (therefore generated ports that work), the main server has to send the clients connecting to the sub server the ports, before they can connect to the sub server. My problem is, I dont know how to read the used ports from a running server instance. (The main server knows all sub server instances)

NathanSweet commented 9 years ago

Why would you use multiple servers?

You can try getting the Server's serverChannel and using the NIO API to get the port. You'll need to add Server#getServerChannel.

On Tue, Mar 31, 2015 at 6:56 PM, Rolleander notifications@github.com wrote:

Thats the problem, I have a main server with constant IP + port. The main server opens new sub-server (therefore generated ports that work), the main server has to send the clients connecting to the sub server the ports, before they can connect to the sub server. My problem is, I dont know how to read the used ports from a running server instance. (The main server knows all sub server instances)

— Reply to this email directly or view it on GitHub https://github.com/EsotericSoftware/kryonet/issues/88#issuecomment-88170970 .

Rolleander commented 9 years ago

Okay, I understand know how to get the port information. I have my main server who is the management basis for db and login/logout. Then I have many sub servers representing current game containers. I didn't want to send all information to my main server, instead if a game starts all participating players will be connected to a special game server, only handling game relevant information and objects to be sent. So I dont have to check on my main server on every receive "Is my player in-game, and in which game I must send the information?". With 100 players, I would have to loop over every registered player to check in which game he is (and that for all received objects!). If I have sub servers for games, there are less clients connected => only the clients in the game. I dont know if its bad opening too many servers, but at least the players in game shouldnt be sending to the main server their udp game updates.

Edit:

For everyone interested, this is how to read the ports:

Tcp (in Server): serverChannel.socket().getLocalPort()

Udp(in UdpConnection): datagramChannel.socket().getLocalPort()

NathanSweet commented 9 years ago

On Tue, Mar 31, 2015 at 7:37 PM, Rolleander notifications@github.com wrote:

Okay, I understand know how to get the port information. I have my main server who is the management basis for db and login/logout. Then I have many sub servers representing current game containers. I didn't want to send all information to my main server, instead if a game starts all participating players will be connected to a special game server, only handling game relevant information and objects to be sent. So I dont have to check on my main server on every receive "Is my player in-game, and in which game I must send the information?". With 100 players, I would have to loop over every registered player to check in which game he is (and that for all received objects!).

You can store state on the connection. When you receive a message from a connection, you already know what game it is (or whatever other state). Eg: https://github.com/EsotericSoftware/kryonet/blob/master/examples/com/esotericsoftware/kryonet/examples/chat/ChatServer.java#L119

If I have sub servers for games, there are less clients connected => only the clients in the game. I dont know if its bad opening too many servers, but at least the players in game shouldnt be sending to the main server their udp game updates.

I think you ​are optimizing prematurely. A single server with a single networking thread should be able to service thousands of connections. You can hand off objects to game threads (or a thread pool) so you don't block the server's network thread. I don't see a benefit to using multiple servers, unless you were going to run them in separate processes so if one goes down the rest are unaffected.

Rolleander commented 9 years ago

Thanks for the information! Thats right, the server knows the player state and in which game he is (every game is in an own thread with the game logic). I think ill try both ways, with multiple servers and only one! Thanks for your support !