Sockets Question

I

Ike

If I succesfully create a Socket as follows:

Socket sock = new Socket("192.168.1.55", 1000);

Then, go to send data, and listen in on it as follows:

BufferedWriter os = new BufferedWriter(new
OutputStreamWriter(sock.getOutputStream()));
BufferedReader is = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
try{
os.write("D0");
os.flush();
returnedString =is.readLine();
System.out.println(returnedString);
}catch (java.io.IOException e) {


I get hung up on --> is.readLine();

I don't understand why the system would simply hang here. Am I missing
something? Am I doing something really stupid here that I am oblivious to?
Thanks, Ike
 
L

Lee

You need a while loop in there:

try{
os.write("D0");
os.flush();
String returnedString = "";
while((returnedString =is.readLine())!=null){
System.out.println(returnedString);
}
}catch (java.io.IOException e) {
 
S

Sudsy

Ike wrote:
I get hung up on --> is.readLine();

I don't understand why the system would simply hang here. Am I missing
something? Am I doing something really stupid here that I am oblivious to?
Thanks, Ike

Possibly the other end isn't sending line-termination character(s).
 
C

Chris Smith

Ike said:
BufferedWriter os = new BufferedWriter(new
OutputStreamWriter(sock.getOutputStream()));
BufferedReader is = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
try{
os.write("D0");
os.flush();
returnedString =is.readLine();
System.out.println(returnedString);
}catch (java.io.IOException e) {


I get hung up on --> is.readLine();

I don't understand why the system would simply hang here. Am I missing
something? Am I doing something really stupid here that I am oblivious to?

A couple things:

1. You are using the platform's default encoding to talk over a socket.
By definition, the platform default encoding is intended to be used as a
default encoding for local text. It's nearly always a mistake to use it
for network communication. Find out what the proper encoding is for
your network protocol (it's quite likely to be ASCII or ISO 8859-1) and
specify that by name when you create the InputStreamReader and
OutputStreamWriter.

2. The reason your code hangs on is.readLine() is probably that it's
waiting for the server to finish sending a line of text. If the server
doesn't send newlines at the end of its text, you'll need to use a
different method instead. Alternative, if newlines are used in this
protocol, then you might need to send one after "D0". I have trouble
imagining a protocol where a client would send "D0" and the server would
immediately respond with something terminated by a newline. Check your
assumptions in the documentation for this protocol.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top