Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Problem when send more messagge to socket

webgestFeb 7 2020 — edited Feb 7 2020

I develop an application in java for communicate with a ethernet printer. I use this code

Socket socket = new Socket(ip,port);

                  InputStream is = socket.getInputStream();

      InputStreamReader isr = new InputStreamReader(is);

  BufferedReader br = new BufferedReader(isr);

      OutputStream outStream = socket.getOutputStream();

   String messageString = "message1";

  outStream.write(messageString.getBytes());

  outStream.flush();

  outStream.close();

  br.close();

          socket.close();

For "message 1 " all it s ok but when i try to send "message2" the printer doesn t print the message. I need to close the connection and re-open the socket like this

socket = new Socket(ip,port);

                  InputStream is = socket.getInputStream();

      InputStreamReader isr = new InputStreamReader(is);

  BufferedReader br = new BufferedReader(isr);

      OutputStream outStream = socket.getOutputStream();

  String messageString = "message2";

  outStream.write(messageString.getBytes());

  outStream.flush();

  outStream.close();

  br.close();

          socket.close();

In this way work fine but if i send ten message i have to wait 10 second to have the ticket probably due to the time for open a new socket Thank you

Comments

Post Details

Added on Feb 7 2020
0 comments
185 views