print multiple lines using sockets in java

时间:2016-10-20 18:40:36

标签: java sockets newline

I'm creating a simple client-server to use with my Raspberry Pi.

What I'm trying to accomplish is to send "ACKTEMP" for example to the Server using my Client. The servers then calls the serial port (which is my STM32 Nucleo Board btw) with this message I get the temperature back using the serial communication and after that it sends it back to the Client.

My question is, the Nucleo board returns some strings like (TEMP: xx) this works fine until I start sending multiple strings back at once ex. if I send ACKTEMP and want to receive (TEMP: xx ) and "Temperature OK", doing this I only get the first line which is (TEMP: xx ).

So it seems that somewhere in my code I need to change something so it prints out all the lines instead of just one and then stop. Please don't get angry at me if my programming isn't that great I'm a student and trying to understand everything.

    public class Client {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        String sentence;
        String messageFromServer;
        while(true)
        {
        BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in));
        try (Socket clientSocket = new Socket("192.168.0.135", 6789)) {
            PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
            BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            sentence = inFromUser.readLine();
            outToServer.println(sentence + '\n');
            messageFromServer = inFromServer.readLine();
            System.out.println("FROM SERVER: " + messageFromServer);
            clientSocket.close();
        }
        }
    }

}

    public class Server {
        private static SerialPort serialPort;

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
         String clientSentence;
         String capitalizedSentence;
         ServerSocket welcomeSocket = new ServerSocket(6789);
         serialPort = new SerialPort("/dev/ttyACM0");


         while(true)
         {
            Socket connectionSocket = welcomeSocket.accept();
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(connectionSocket.getInputStream()));
            PrintWriter outToClient = new PrintWriter(connectionSocket.getOutputStream(), true);
            clientSentence = inFromClient.readLine();
            try{
             //opening port
            serialPort.openPort();

            serialPort.setParams(
                    SerialPort.BAUDRATE_115200,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            //Write string to port
            serialPort.writeString(clientSentence + "\n");

            System.out.println("String wrote to port, waiting for response.."); 

            try {
                     Thread.sleep(10);                 //1000 milliseconds is one second.
                } catch(InterruptedException ex) {
                   Thread.currentThread().interrupt();
                }

            String buffer = serialPort.readString();
            outToClient.println(buffer + '\n');

            serialPort.closePort();//Close serial port

            }
            catch(SerialPortException ex){
            System.out.println("Error writing data to port: " + ex);
            } 
         }
        // TODO code application logic here
    }

}

1 个答案:

答案 0 :(得分:0)

我使用以下内容来解决这个问题。但如果有人有更好的解决方案,那就非常受欢迎了!

我所做的是每一根字符串,我想要一条新线,我放了一个"〜"在我的Nucleo STM32程序中签署字符串前面。

       String strArray[] = messageFromServer.split("~");

       System.out.println("FROM SERVER: " + strArray[0]);

       for(int i = 1; i < strArray.length; ++i)
       {
           if(messageFromServer.indexOf('~') >= 0)
           {
               System.out.println("FROM SERVER: " + strArray[i]);
           }
       }