[java] Game Networking

Started by
4 comments, last by Antheus 16 years, 1 month ago
I'm a student currently working on a project with some guy from overseas. For the networking, I've used Java's knock knock client/server tutorial (the multi threaded one), and added my own Listener to reduce class coupling. My problem is that the server class is dumb. All it does is receive a string and notify that a string is received (ServerListener). But It doesn't let me know which client (player) had sent the message or let me reply to only an individual client. Two problems arise when I try to fix the problem: The first is security, how can I be sure the request is a player and not a hacker? The second is identity, how do I know which player the message is from? I'd like to have a list of unique usernames (or id's) and be able to send replies to individual clients. Please give me your solution if you have experienced this problem yourself.
Advertisement
You may want to look at Killer Game Programming in Java's networking chapters. They take on Java networking from the standpoint of a game programmer so it may be of use to you.
After much thought, I think simplicity is the key. What I can do is give each client handling thread an id upon its creation.

The business part of the server would look like this:

int id = 0;
while(true)
{
try
{
Socket clientSocket = serverSocket.accept();

ClientServiceThread cliThread = new ClientServiceThread(clientSocket, id++);

cliThread.start();
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}

Also should I give each client handler thread its own listener? Or just the one for the server?

Or am I doing this all wrong?
Quote:The first is security, how can I be sure the request is a player and not a hacker?


You don't. Anyone can talk to your sockets, unless you use IP filtering.

Quote:The second is identity, how do I know which player the message is from? I'd like to have a list of unique usernames (or id's) and be able to send replies to individual clients.


That would be your socket instance you obtain from accept. Each connection has unique socket which has remote IP.

Quote:After much thought, I think simplicity is the key. What I can do is give each client handling thread an id upon its creation


Your socket *is* the id. Since this is Java, there's no reason why you couldn't pass either handler or socket as your key.

Quote:Also should I give each client handler thread its own listener? Or just the one for the server?


You need one handler per thread due to blocking nature of sockets. If you have a single listener, you'll block on one socket, and others will not get handled.

In general, thread-per-socket doesn't scale. If you intend to have a handful of clients it should be fine. Later however you'll need to look into non-blocking sockets.
Quote:
Quote:The second is identity, how do I know which player the message is from? I'd like to have a list of unique usernames (or id's) and be able to send replies to individual clients.


That would be your socket instance you obtain from accept. Each connection has unique socket which has remote IP.

Quote:After much thought, I think simplicity is the key. What I can do is give each client handling thread an id upon its creation


Your socket *is* the id. Since this is Java, there's no reason why you couldn't pass either handler or socket as your key.


Why is the socket unique? I've read that remote IP's arn't always unique (two machines may share the one IP).

How do can I represent a socket as an Integer or String exactly?
And is it worth it? what will it achieve that my incrementing id cant?

Thanks for the reply, I Appreciate it.
Quote:Original post by fujitsu

Why is the socket unique? I've read that remote IP's arn't always unique (two machines may share the one IP).


Network identification is IP+port. If two "different" clients share the same IP+port, they are, by definition of internet networking, the same client.

Quote:How do can I represent a socket as an Integer or String exactly?
And is it worth it?


Socket you obtain from accept() is a unique instance of Socket object. If you do (socket1 == socket2), the result will be true only and only if they are in fact the very same instance.

Since accept() uses new Socket() for what it returns, the connection will be the same only and only if it's the same client.

Therefore, you can safely use socket instance to identify clients.

Quote:what will it achieve that my incrementing id cant?


It's unique. Your id isn't, since it'll roll over at some point, and then you'll need a way to determine which values are in use and which aren't.

It also relieves you of extra management. Your socket instances are unique by definition.

This topic is closed to new replies.

Advertisement