0

For this school assignment, I need to simulate a client server type application using Java threads (no need for sockets etc). How might I go about doing it?

I need a way to server to start and wait for clients to call it then it should return a response. The "API" in my mind was something like:

server.start()
client1.connect(server)
client2.connect(server)

x = client1.getData()
y = client2.getData()

success1 = client1.sendData(1)
success2 = client2.sendData(2)

How might the server|client.run method look like? Assume I could hardcode the method calls for now.

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805

2 Answers2

1

I suggest to use the following approach:
1. Have "server" code that works with Blocking Queue -
A blocking queue is a data structure which is synchronized and let's the thread that reads data from it (the "consumer" thread) to wait until there is a data in the queue to be read.
The "producer" thread is a thread that "pushes" data on the queue.
I would recommend you use one of the blocking queue implementations.
I would also suggest you read more about "consumer producer" pattern.
Blocking queue also eliminates the need for "busy wait" which is not recommended in multi-threading programming.

Yair Zaslavsky
  • 4,091
  • 4
  • 20
  • 27
  • I thought of this too. How do the server determine which client/producer to return data to? Do I have a blocking queue that takes in an object like (producer, functionToCall, args)? – Jiew Meng Feb 23 '13 at 11:36
  • @JiewMeng - For that issue, you can have a blocking queue in which the client will be the consumer, and the server will be the producer. – Yair Zaslavsky Feb 24 '13 at 10:46
  • did u mean a pair of queues where 1 has the server as a consumer (when client requests data) and vice versa? Else how does the server knows when the client calls? – Jiew Meng Feb 26 '13 at 01:55
  • Hmm, ... I may have many clients ... so do I need 1 queue per client? Else I need to somehow check if the response is for me – Jiew Meng Feb 26 '13 at 02:21
0

From the description that you have provided What i can suggest is you should write some thing like

1) Have one queue where all the clients can put up messages.

2) server which is running in an infinite loop like while(true) waits for the new messages that has been put in the queue and if it finds one then processes it and marks it as processed.

3) The job of the client threads would be to create messages and put them in the queue. And notifying the server that new message has been added to the queue so that server can come to know that new message has been arrived for processing.

For this program to make it working i think you need to learn Thread's notify, notifyAll(), and wait() methods. So basically without sockets what you are looking for it "Inter thread communication". This link can help.

Hope this helps.

ATR
  • 2,160
  • 4
  • 22
  • 43
  • "busy wait" is not recommended in multi-threading. See my answer about blocking queue. There are other ways to handle this issue. A thread can wait on a synchronization object that will be signaled by the thread that sends the data. – Yair Zaslavsky Feb 23 '13 at 09:24
  • @zaske : So are you trying to say that both consumer and producer will be having synchronized lock on the collection and when there is a message consumer will consume and remove it from the queue ? It seems a good approach. And yeah if requirement is such simple then it can save overhead of using wait, notify and notifyall. – ATR Feb 23 '13 at 09:28
  • Yes, but I would suggest using "already written" classes. No need to "re-invent the wheel" unless of course he is not allowed to use java util concurrent. But even in this case, he will find blocking queue implementations over the internet he can use. – Yair Zaslavsky Feb 23 '13 at 09:38
  • @zaske : yes, that can be better. – ATR Feb 23 '13 at 09:45