Search code examples
javasocketsclientserverserversocket

Keep communication open between server and client - Java


How do you make a client which is able to send a server multiple messages at anytime, and therefore a server listening for a message all the time.

Right now I have wrote some code which only allows me to send a message once. I thought this was due to me closing the input/output streams and the sockets. So I have been playing around for a while now and I can't seem to do it!

Client:

public class Client {
    private Socket socket;
    private OutputStream os;

    public Client() {}

    public void connectToServer(String host, int port) {
        try {
            socket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sendMessage();
    }

    public void sendMessage() {
        try {
            os = socket.getOutputStream();

            String string = "Anthony";
            byte[] b = string.getBytes(Charset.forName("UTF-8"));

            os.write(b);
            os.flush();

            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void STOP() {
        stopOutput();
        stopServer();
    }

    public void stopServer() {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopOutput() {
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Server:

public class ConnectionHandler implements Runnable {
    private Socket clientSocket;
    private BufferedReader in;

    public ConnectionHandler(Socket clientSocket) {
        this.clientSocket = clientSocket;
        String clientAddress = clientSocket.getInetAddress().toString()
            .substring(1);
        System.out.println("Connected to " + clientAddress);

        try {
            in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                ArrayList<String> data = new ArrayList<String>();
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    data.add(inputLine);
                }

                if (data.size() > 0) {
                    System.out.println(data.toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void STOP() {
        stopInput();
        stopConnection();
    }

    public void stopInput() {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopConnection() {
        try {
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

At the moment on the client side, I send a message as soon as the socket is opened but after when I call the send function from another class it does not send...

How should I do this? Or what am I doing wrong?

Thanks in advance.

p.s. I am guessing client-server is the same as server-client, so if I know how to do one way I can easily switch it around... right?


Solution

  • Turns outs it was a simple error.

    I as writing (sending-client) as an OutputStream however I was then reading (receiving-server) as BufferedReader! ha

    So quick tip for anyone, make sure you receive messages the same way you send them!

    Thanks for everyone who tried helping.