1

I'm creating a Java program, based on the client-server model in which I need the server and the client(s) to communicate.

More specifically I need them to "give orders" to each other or request things, and transfer data inside those orders/requests, you can see what I'm trying to achieve looks something like a method, but instead of happening locally, those "methods" are called by one side and executed by the other.

For instance a possible scenario is:

  1. The server is listening for orders
  2. The client tries to authenticate and sends username and password to the server
  3. The server receives the authentication request, controls whether the username and password are correct and reports back to the client
  4. The client is now authenticated
  5. The server is once again listening for orders

Now, my first idea was to use DataInputStream and DataOutputStream, sending requests under the form of strings, for instance, if I wanted the client to request authentication I would do something like this:

DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF("requestAuth");
dos.writeUTF(username);
dos.writeUTF(password);

and the server:

DataInputStream dis = new DataInputStream(socket.getInputStream());
if(dis.readUTF().equals("requestAuth")){
    String username = dis.readUTF();
    String password = dis.readUTF();
    //Check whether they're correct or not bla bla bla
} 

This doesn't seem like the best option to me, I think there's better ways to do this but I just don't know how, I already searched for a better solution but found nothing.

Another problem that arised is that I need both the server and the client to be able to send those requests simultaneously and these requests can be sent anytime, asynchronously.

For instance: let's say the client is authenticating, it's sending the username, password, etc... but meanwhile the server wants to check if the client is still connected because a Thread is doing it every second, how can I make sure each information is delivered in the right place without the stream clogging up or threads receiving unwanted data? If the client is waiting to know whether the username and password are correct, I don't want to clog it up with the request of the other thread that's doing something completely different.

Basically: my client/server program can be multithreaded, can sockets too without things getting messy? Can I have thread ServerA communicating with thread ClientA, and thread ServerB communicating with thread ClientB without interferring with each other? Can I have multiple streams from the same socket and work on them separately?

Elia Perantoni
  • 581
  • 1
  • 6
  • 19
  • Have you looked at http://stackoverflow.com/questions/2828447/using-threads-to-handle-sockets?rq=1 ? – Ivan Pronin Apr 07 '17 at 18:13
  • Yes but in that case he's only reading and writing once. I need to be able to read from 2 different sources simultaniously, for example: the server needs to be able to listen for a string in one thread and for another string in another thread without those strings swapping and going in the wrong thread – Elia Perantoni Apr 07 '17 at 18:31
  • Look into the direction of ThreadLocal class to store thread-dependent values and operate on them - refer to [this discussion](http://stackoverflow.com/questions/817856/when-and-how-should-i-use-a-threadlocal-variable), for example – Ivan Pronin Apr 07 '17 at 18:49

0 Answers0