ilCrosta
ilCrosta

Reputation: 95

Java Socket - send many message

I have two simple classes:

Client:

public static void main(String[] args) throws IOException {
        InetAddress addr = InetAddress.getByName(null);
        Socket socket = null;

        try {
            socket = new Socket(addr, 1050);

            InputStreamReader isr = new InputStreamReader(socket.getInputStream());
            in = new BufferedReader(isr);

            OutputStreamWriter osw = new OutputStreamWriter( socket.getOutputStream());
            BufferedWriter bw = new BufferedWriter(osw);
            out = new PrintWriter(bw, false);

            stdIn = new BufferedReader(new InputStreamReader(System.in));
            String userInput;

            // read user input
            while (true) {
                userInput = stdIn.readLine();

                System.out.println("Send: " + userInput);
                out.println(userInput);
                out.flush();

                String line = in.readLine();
                while(line != null){
                    System.out.println(line);
                    line = in.readLine();
                }

                System.out.println("END");
            }
        }
        catch (UnknownHostException e) {
            // ...
        } catch (IOException e) {
            // ...
        }

        // close
        out.close();
        stdIn.close();
        socket.close();
    }

Server:

OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
                BufferedWriter bw = new BufferedWriter(osw);
                PrintWriter out = new PrintWriter(bw, /*autoflush*/true);
private void sendMessage(String msg1, String msg2) {
        out.println(msg1);

        // empy row
        out.println("");

        out.println(msg2);
    }

The user enters a message, and this is sent to the server. Then, the server responds with N messages. After the first request, the client stops and is never printed the word "END". How do I send multiple messages at different times, with only one socket connection?

Upvotes: 1

Views: 4192

Answers (1)

Stu Whyte
Stu Whyte

Reputation: 768

Firstly, you don't need to send an empty row, because you are sending by "line" and recieving by "line".

out.println(msg1);
out.println(msg2);

and

userInput = stdIn.readLine();

Here, userInput will only equal msg1

What I would recommend, would be not to loop on stdIn.readLine() = null, but have the client send, for example, "END_MSG", to notify the server that it will not send anymore messages. Perhaps something like...

SERVER:

userInput =stdIn.readLine();
if(userInput.Equals("START_MSG");
    boolean reading=true;
    while(reading)
    {
         userInput=stdIn.readLine();
         if(userInput.Equals("END_MSG")
         {
             //END LOOP!
             reading = false;
         }
         else
         {
             //You have received a msg - do what you want here
         }
    }

EDIT:CLIENT:

private void sendMessage(String msg1, String msg2) {
        out.println("START_MSG");

        out.println(msg1);

        out.println(msg2);

        out.println("END_MSG");
    }

(It also looks like in your question to have mixed up the client and the server?)

Upvotes: 2

Related Questions