Sign in to follow this  
fujitsu

[java] Game Networking

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this