0

Have a problem I hope someone could solve for me.

I have made a server simulator and a Clint that can connect to that server simulator. My server simulator listen on some commands that my Client can send to it.

The following commands are: D, DN, S, T, B, Q

My problem is that I need to make 3 functions in the server, like u see in the menu down under: a user can enter T, B or Q in the server.

What my server should do:

So when you run the server, it asks for a connection, then you run the Client program and connect to the server via a port number.

After that the server Menu will display.

My server Menu so far.

        System.out.println(" ");
        System.out.println("*************************************************");
        System.out.println("Welcome to Scale Simulator");
        System.out.println("Netto on weight: " + (brutto-tare) + "kg");
        System.out.println("*************************************************");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Esablished connection to address: " + sock.getInetAddress() + "");
        System.out.println("Brutto on weight: " + brutto + "kg");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("This simulator listen on the following commands: ");
        System.out.println("D, DN, S, T, B, Q ");
        System.out.println("Response received from Client: "+ inputRecieved + "");
        System.out.println(" ");
        System.out.println(" ");
        System.out.println("Enter T <For Tare on weight> ");
        System.out.println("Enter B - New Brutto <Enter a new brutto on weight>");
        System.out.println("Enter Q <For exit the program and connection ");
        System.out.print("Enter here: ");

Here I have to code it so I can enter T, B or Q, but also if I don't enter something my Client should still work so I can send a command to it like S (That will show what is on the Scale for now)

For full server code can be found here:

Hope you understand what i just wrote ;) or else tell me, and i try to explain it a bit better.

UPDATE:

So right now i read something about threads, and yes, it works, but i don't know if its the right way to code. could you please take a look:

After my client connects to the server simulator, my code is HERE:

Could you please check if this is a ok way to make the threads?

For those who don't have time to look at the pastebin ;) i done it like this:

Thread threadServer = new Thread() {
      public run() {
        functionality for the server functions
   }
};

Thread threadClient = new Thread() {
      public run() {
        functionality for the Client command response functions
   }
};
Kara
  • 6,115
  • 16
  • 50
  • 57
Pixel
  • 349
  • 1
  • 8
  • 17
  • To simultaneously listen for input from both the client and the server, you probably need two threads. – Alex Wittig Mar 12 '14 at 15:50
  • ohh, i was sick that day we had about threads in School, i try read some about it.. thx so much for replay – Pixel Mar 12 '14 at 15:53
  • You might find answers to [this question](http://stackoverflow.com/questions/5419328/multiple-client-to-server-communication-program-in-java) useful. – Alex Wittig Mar 12 '14 at 16:17
  • Create a client and server object and make them implement [`Runnable`](http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html). Then pass the `Runnable` objects to your created thread, [like so](http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html). – MirroredFate Mar 12 '14 at 20:13

0 Answers0