0

I'm writing a game server in Java. As common in game servers, I must take data received by one client and distribute it over the client's room (other clients). Currently, each client spawns its own thread and works on top of that. I'm having trouble, however, to define the inter-player relations. How do I control something like chat, where a message must be echo'ed to all the other players in the same room? I'm thinking in something like a message queue, where all threads would often look for messages and send them to its own client.

I'm also open to suggestions that would ditch multithreading at once, but I'm not familiar with NIO and the server is already written using plain Sockets and Threads.

Summary: How do I make my clients (in threads) talk to each other? Or is there a better non-threaded alternative to this?

Délisson Junio
  • 1,296
  • 4
  • 21
  • 43
  • Using some framework like Netty would make your life much easier and your application more scalable. I'm sure broadcasting data to connected clients becomes a simple task. There might be a slight learning curve, but it will be worth it. – Jurgen Camilleri Dec 11 '14 at 14:25
  • @JurgenCamilleri I gasped when I readed the getting started manual for netty! So many `builder`s and `factory`s that makes me cry! do you know of a better resource for learning it, or is that the true-and-tested-way? – Délisson Junio Dec 11 '14 at 14:27
  • Well in my experience with Netty I just started writing out the examples and trying them myself; then started building on top of them what I needed, and discarded what I didn't. This YouTube channel contains a series which looks promising: https://www.youtube.com/channel/UCIA0yteJXa5JgRqxFJYQbEQ – Jurgen Camilleri Dec 11 '14 at 14:36

3 Answers3

1

One approach I have used is to design a Server class and a class which extends Thread.

The Server class will spawn all of your threads, and keep a list of all currently running threads it has created.

In your Thread class, use the Socket object to create your input / output streams. When one of these threads has something to say, it will talk to the server through the stream, and then it is up to the server to deliver that message to all of its currently running threads.

This really simple java tutorial actually helped me out a lot.

https://docs.oracle.com/javase/tutorial/networking/sockets/readingWriting.html

Jakeway
  • 241
  • 1
  • 2
  • 10
  • Oh, so the server, on a separate thread, listens for the clients each on their own threads? Wouldn't that introduce a delay between the client writing and the server receiving it? – Délisson Junio Dec 11 '14 at 14:40
  • sure, there is always some kind of delay. but how much of a delay are you expecting it to have? – Jakeway Dec 11 '14 at 15:04
  • Hm actually it sounds performant now. The master thread's only job is to watch for the client threads to ask for broadcasting and stuff. Sounds neat, I might try it if netty doesn't works to me. Thank you! – Délisson Junio Dec 11 '14 at 15:11
  • No problem! When I get home I can show you the code I wrote which might help out way more than plain English. – Jakeway Dec 11 '14 at 15:22
  • I was reading some material and found an answer in this question http://programmers.stackexchange.com/questions/185385/should-i-use-simple-socket-programming-to-communicate-between-2-threads which states that using sockets to communicate between threads is a no-no. Am i missing something here? – Délisson Junio Dec 11 '14 at 16:35
1

My proposed solution too is similar to Jakeway. I have exactly implemented the same feature in this way.

Server will create one thread and create Server Socket in that thread and waits for connection

Client will connect to Server. Server will create ClinetSocket and pass this socket to thread. This thread is responsible for client/server communication from server

Form the client side, on creation of Socket, one thread will start and that thread is responsible for client/server communication from client

Have a look some of ready-made code from below links.

chat example 1

chat example 2

Regarding Pros & Cons of socket usage, visit below link : rmi vs servlets vs sockets

Regarding clients communication between them ? Not feasible. Client will send message to server & server should send message to other client. If you look at Yahoo kind of chat

1) You will send message to server in a chat room

2) Server have list of client subscribed clients to the chat room

3) Server will send message to all the clients subscribed to room

Community
  • 1
  • 1
Ravindra babu
  • 37,698
  • 11
  • 250
  • 211
-1

Hey i made a library just for that! https://www.dropbox.com/s/xcy1uyyyc610lb5/JSock.rar?dl=0 Just download it and import it into your project. Read ReadMe.txt it will explain most of the stuff. If you need me to explain more just ask!

  • 1
    Include the relevant parts of the link into your answer to demonstrate examples of code. Currently this answer is very spammy, especially linking to a DropBox .rar... Why not use GitHub to share? Seems more legitimate. – CubeJockey Aug 12 '15 at 17:41