2 Replies Latest reply on Dec 12, 2012 1:14 PM by smigh

    Newbie question about the arquitecture of a client-server application

      I've been doing some stuff in Java for a while now but I never delved into the networking part of it. I've followed Oracle's tutorial and I've been searching a few other resources on the web. I don't have a very specific question but I'd appreciate if someone could help me understand the whole thing. My aim in this learning project is to make a server application that will accept an undetermined number of independent connections from a client application. I think I get the fundamentals of setting up a ServerSocket on the server, setting up the Socket on the client, then open input and output streams on both and start a transfer of data.

      Now, I think I'm stuck in a wrong understanding about that transfer. All the examples I see seem to deal with transfers of data where the order that each side speaks is already known, so you send something right after connecting and you set the other side to stand by for receiving something after connecting, and everything works fine. If the order isn't known though, I can't seem to figure out the logic flow of the app.

      Let's say the server gets 3 connections from 3 clients and I save those Socket objects in an ArrayList. Then, client B asks for something and the server responds accordingly. Later, the server needs to tell something to client B, so it sends some information through its socket and client B responds accordingly. How should I go about setting something like this up? I'm thinking I should set listeners but I can't find examples like these, or should I set infinite loops for each socket, attempting to read data?

      Any help to guide me in the right direction would be very appreciated.
        • 1. Re: Newbie question about the arquitecture of a client-server application
          Think about a very basic chat client-server application. Once a client connects, he receives all the messages entered by other clients. He can also send messages that will go to all the other clients. This is very much like your uncertainty about who will talk first, the server or the client. If the user/client waits a bit after connecting, it's likely the server will talk first (because another user sent a message). If the user/client is fast, he will speak first ("Hello everybody" or something like this). There are examples of such simple chat applications on the web, just google for them. And yes, this is typically achieved with endless loops. Each client will have such a loop attempting to read data from the server and the server will have as many endless loops concurrently (think multi-threading) active as there are clients connected.
          • 2. Re: Newbie question about the arquitecture of a client-server application
            Thanks for the pointers. I'm in the right track now.