Send and Receive data from socket simultaneously
Hello,
I am working with an in-line barcode reader that receives commands and sends ASCII data to the client using the socket functionality in Java. I have no problems using PrintStream sending commands or using BufferedReader to receive data from the barcode reader separately. The problem I am currently having is receiving data from the barcode reader immeditately after I send it a command string. For example there is a command string that I can send to the barcode reader and by doing so the barcode reader will return settings in ASCII data.
Here is my code:
Code :
try
{
//Create a connection to server
Socket s = new Socket(ipAddress, portNumber);
//Create input and output streams to socket
PrintStream out = new PrintStream(s.getOutputStream());
//Create input streams to socket
BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
//send the command string to the barcode reader
out.print("sRN LocationName");
//receive data from barcode reader that the command string initiated
String line = in.readLine();
System.out.println("Text received: " + line);
//close the stream
out.close();
//close the stream
in.close();
//close the socket
s.close();
}catch (SocketException e )
{
//for debugging purposes
System.err.println ("Socket error : " + e);
}catch (UnknownHostException e )
{
//for debugging purposes
System.err.println ("Invalid host!");
}catch (IOException e )
{
//for debugging purposes
System.err.println ("I/O error : " + e);
}
Everytime I try it I get no return. I know it works because I have tested in Hyperterminal and it works everytime.
Any help is appreciated.
Thanks,
Tim