tags:

views:

57

answers:

5

I have a tcp socket sending three lines like this

        out2.println("message1\n");
        out2.println("message2\n");
        out2.println("message3\n");

and another tco socket receiving and displaying these messages like this

        System.out.println(in.readLine());
        System.out.println(in.readLine());
        System.out.println(in.readLine());

but only the first message is recieved and displayed, anything I send after that is not.

edit: here is the code

    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {                                       
    try {
        // TODO add your handling code here:
        String ipAddress = ipTextArea.getText();
        sourceSocket = new Socket(ipAddress,32323);
        out = new PrintWriter(sourceSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(sourceSocket.getInputStream()));
        System.out.println(in.readLine());
        System.out.println(in.readLine());
        System.out.println(in.readLine());
    } catch (UnknownHostException ex) {
        Logger.getLogger(DESWashView.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(DESWashView.class.getName()).log(Level.SEVERE, null, ex);
    }


}       

cWashStations is also called from a button event:

public void cWashStations(){
Thread washThread = new Thread(){
    @Override
    public void run(){
        try {
            sSocket2 = new ServerSocket(32323);
            Thread stationThread = new Thread(){
                @Override
                public void run(){
                    try {
                        washSocket = sSocket2.accept();
                        out2 = new PrintWriter(washSocket.getOutputStream(), true);
                        in2 = new BufferedReader(new InputStreamReader(washSocket.getInputStream()));
                        out2.println("hello from attendant3423\n\n");
                        out2.flush();
                        out2.println("hello from attendant3423\n\n");
                        out2.println("1");
                        while(running){
                            }
                        } catch (IOException ex) {
                            Logger.getLogger(DESAttendantView.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                 };
                 stationThread.start();

            } catch (IOException ex) {
                Logger.getLogger(DESAttendantView.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    };
    washThread.start();
}
A: 

It sounds like you are buffering somewhere. Either remove the buffer for testing or flush the buffer.

Pyrolistical
+1  A: 

If you use println() do not add \n at the end of string.

Ha
A: 

Call out2.flush() in stationThread

Anon
A: 

There are couple of things I don't understand in your code, but I think you should flush the buffer once you are done with it (any reason why you flush after the first message and not after the last?) and probably you also want to close the connection at the end?

fikovnik
A: 

Instead of doing three System.out.println(in.readLine()) in a row, try doing it in a loop. I have a feeling the extra two '\n' you put in are being read as lines.

Rob Heiser