0

I have code which works with one client connection. What I need is ability for the server to handle multiple client requests using multithreaded approach.

I found some solutions, but it's not meet my requirements, like this, or this

Server.java

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

public class Server extends User {
    private Socket clientSocket;
    private ServerSocket serverSocket;


    public Server() {
        super();
    }

    private void createConnection() {
        try {
            InetAddress locIP = InetAddress.getByName("127.0.0.1");
            serverSocket = new ServerSocket(9999, 0, locIP);


            //      serverSocket = new ServerSocket(4444, 4444, InetAddress.getByName("192.168.0.101"));
        } catch (IOException e) {
            System.err.println("Could not listen on port: 9999 ." + e);
            System.exit(1);
        }
    }

    private void closeConnection() {
        try {
            serverSocket.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    @Override
    public void connect() {
        createConnection();

        //Socket clientSocket=null;
        try {
            clientSocket = serverSocket.accept();

            System.out.println("Client connected! "
                    + "IP: "
                    + clientSocket.getInetAddress()
                    + ", port: "
                    + clientSocket.getPort());


        } catch (IOException e) {
            System.err.println("Accept failed. " + e);
            System.exit(1);
        }
    }


    @Override
    public void disconnect() {
        try {
            clientSocket.close();
        } catch (IOException e) {
            System.err.println(e);
        }

        closeConnection();
    }


    @Override
    public Socket getSocket() {
        return clientSocket;
    }

    @Override
    public String toString() {
        return new String("Server");
    }

}

Client.java

import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class Client extends User {

    private Socket socket;


    public Client() {
        super();
    }

    @Override
    public Socket getSocket() {
        return socket;
    }

    @Override
    public void connect() {
        try {
            InetAddress locIP = InetAddress.getByName("127.0.0.1");
          //  socket = new Socket(9999, 0, locIP);
          //  socket = new Socket("localhost", 9999);       oryginalny
              socket = new Socket(locIP, 9999);

        } catch (UnknownHostException e) {
            System.err.println("The host not found! " + e);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Can't find connection! " + e);
            System.exit(1);
        }
    }


    @Override
    public void disconnect() {
        try {
            socket.close();
        } catch (IOException e) {
            System.err.println(e);
        }
    }

    @Override
    public String toString() {
        return new String("Client");
    }
}

SendButton.java

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintStream;


import javax.swing.JButton;
import javax.swing.JTextPane;


@SuppressWarnings("serial")
public class SendButton extends JButton {
    private JTextPane incomingMessages;
    private JTextPane messageToSend;
    private User user;

    public SendButton(User user, JTextPane incomingMessages, JTextPane messageToSend) {
        super("Send!");
        this.user = user;
        this.incomingMessages = incomingMessages;
        this.messageToSend = messageToSend;

        this.addActionListener(new SendListener());
    }

    public class Write {
        private PrintStream out;


        public Write() {
            try {
                out = new PrintStream(new BufferedOutputStream(
                        user.getSocket().getOutputStream(), 1024), false);
            } catch (IOException e) {
                System.err.println(e);
            }
        }


        public void send(String message) {
            if (message != null) {
                out.println(message);
                out.flush();


                incomingMessages.setText(new String(incomingMessages.getText() + "\nMe: " + message));
            }
        }
    }


    public class SendListener implements ActionListener {
        private Write write = new Write();
        private String toSend;


        @Override
        public void actionPerformed(ActionEvent event) {
            toSend = messageToSend.getText();


            if (toSend != null || event.getActionCommand() == "\n") {
                write.send(toSend);
            }


            messageToSend.setText(new String(""));
        }
    }
}
Community
  • 1
  • 1

2 Answers2

2

You need to create a new Runnable class, whose data members consist of a Socket and its input and output streams. This class is used on the server side. Its run() method is responsible for all I/O to that client. Then your accept() loop just looks like this:

while (true)
{
    new Thread(new ConnectionHandler(serverSocket.accept())).start();
}

where ConnectionHandler implements Runnable as above.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @DROY That belongs at the bottom of the `run()` method, in a `finally` block. – user207421 Apr 20 '16 at 03:23
  • @DROY Yes of course but the only way `accept()` can fail is by timeout or closure of the `ServerSocket`. Can we stop this now please? I'm not attempting to post a complete working solution here. I do expect readers to exert some initiative. – user207421 Apr 20 '16 at 03:43
0

simply what you need to do is after accepting the request from the client (Using main thread), then the request pass to a new thread with the client socket and process the request inside the new thread. So the main thread is free to accept new requests.

Lakmal Vithanage
  • 2,767
  • 7
  • 42
  • 58