Forum Stats

  • 3,757,530 Users
  • 2,251,242 Discussions
  • 7,869,855 Comments

Discussions

Problem when send more messagge to socket

webgest
webgest Member Posts: 1

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