Search code examples
javamultithreadingtcpclient

Multi-threaded Java TCP Client


I am writing a Java client application(Basic Java Net package with TCP/IP). The client must take input from the system.in and at the same time must listen to any messages coming from server through socket inputstream. Once an input from the system.in is received, the client will get that input, do some processing and send it to the server as a request. So basically 2 processes run,

-listening to client request

-listning to server responses.

I implemented 2 threads for this and ran the processing of messages in the main thread. Is this good enough design.?

And is there a way to return the message received from the system.in to the main thread. The threads run() method returns void. I used a volatile variable to return the string received but its said that volatile is very costly since it doesn't use cpu cache to store the variable.


Solution

  • You can review these two projects I've written for an example of java sockets and multithreading.

    I guess the ClientExample is the one you are searcing for but you can take a look at the server part too.

    Basically the idea is to start two separate threads that listen for the different inputs - socket and console.

    final Thread outThread = new Thread() {
        @Override
        public void run() {
            System.out.println("Started...");
            PrintWriter out = null;
            Scanner sysIn = new Scanner(System.in);
            try {
                out = new PrintWriter(socket.getOutputStream());
                out.println(name);
                out.flush();
    
                while (sysIn.hasNext() && !isFinished.get()) {
                    String line = sysIn.nextLine();
                    if ("exit".equals(line)) {
                        synchronized (isFinished) {
                            isFinished.set(true);
                        }
                    }
                    out.println(line);
                    out.flush();
                    disconnect();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (out != null) {
                    out.close();
                }
            }
        };
    };
    outThread.start();
    

    and another thread for the socket input:

            final Thread inThread = new Thread() {
                @Override
                public void run() {
                    // Use a Scanner to read from the remote server
    
                    Scanner in = null;
                    try {
                        in = new Scanner(socket.getInputStream());
                        String line = in.nextLine();
                        while (!isFinished.get()) {
                            System.out.println(line);
                            line = in.nextLine();
                        }
                    } catch (Exception e) {
    //                  e.printStackTrace();
                    } finally {
                        if (in != null) {
                            in.close();
                        }
                    }
                };
            };
            inThread.start();
    

    I hope this will help you :)