reading socket

T

Timo

Whats wrong with my socket? I can't seem to get read anything from it.

System.out.println(str) and System.out.println("closed") are not
reached/printed.

socket.toString() shows right address, and socket.isConnected() shows true


try {
Socket socket = new Socket(hubAddress, hubPort);

//out = new PrintWriter(socket.getOutputStream());
BufferedReader bout = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

hubChat.showMessage(socket.toString() + "\n");
hubChat.showMessage(socket.isConnected() + " <- socket \n");

String str;
while ((str = in.readLine()) != null) {
hubChat.showMessage(str);
System.out.println(str);
}
in.close();
System.out.println("closed");

}

catch (IOException e)
{ e.printStackTrace();
}
}


Here is the connection with telnet:

telnet xxx.xxx.xxx.xxx 411
Trying xxx.xxx.xxx.xxx...
Connected to xxxxxxxxxxxxxxxx (xxx.xxx.xxx.xxx).
Escape character is '^]'.
$Lock 4[7<UR[xcL23Z&;7QS@H(n'Xit(1am^Fh>D=?FK;&XyDj.)Q94xw?'PAU
Pk=qUO<ulaI3R+<.O,k|$HubName FIN / ENG Sauna Hub |<Hub-Security> This hub
is running version 1.0.25 of the NeoModus Direct Connect Hub Software.|
 
S

Steve Horsley

Whats wrong with my socket? I can't seem to get read anything from it.

System.out.println(str) and System.out.println("closed") are not
reached/printed.

socket.toString() shows right address, and socket.isConnected() shows true


try {
Socket socket = new Socket(hubAddress, hubPort);

//out = new PrintWriter(socket.getOutputStream());
BufferedReader bout = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

hubChat.showMessage(socket.toString() + "\n");
hubChat.showMessage(socket.isConnected() + " <- socket \n");

String str;
while ((str = in.readLine()) != null) {
hubChat.showMessage(str);
System.out.println(str);
}
in.close();
System.out.println("closed");

}

catch (IOException e)
{ e.printStackTrace();
}
}


Here is the connection with telnet:

telnet xxx.xxx.xxx.xxx 411
Trying xxx.xxx.xxx.xxx...
Connected to xxxxxxxxxxxxxxxx (xxx.xxx.xxx.xxx).
Escape character is '^]'.
$Lock 4[7<UR[xcL23Z&;7QS@H(n'Xit(1am^Fh>D=?FK;&XyDj.)Q94xw?'PAU
Pk=qUO<ulaI3R+<.O,k|$HubName FIN / ENG Sauna Hub |<Hub-Security> This hub
is running version 1.0.25 of the NeoModus Direct Connect Hub Software.|

Looks to me as though you are reading a lot from it. Why do you say you
can't read anything? Can you be mre detailed?

A few comments:

* Character set: You are using the machine default characterset over the
network, which may not be right and isn't very portable. I suggest that
you use the Reader and Writer constructors that take and encoding string,
such as "ASCII" of "ISO8859-1".

* Line endings: Using a BufferedReader will not give you text that is not
finished with a line-feed. I suspect that the hub is using direct cursor
positioning with ANSI escape sequences rather than sending plain text.
This could explain your not seeing some text.

Steve
 
S

Steve Horsley

I found the problem - server doesn't send "end of line" mark, so readline()
didn't work, read() does.

Good. Beware that read() might not return the entire message in one hit -
you probably have to have to read with a timeout of a few seconds before
you continue, or you might go prematurely with only part of the message.

Steve
 
J

John C. Bollinger

Timo said:
Whats wrong with my socket? I can't seem to get read anything from it.

System.out.println(str) and System.out.println("closed") are not
reached/printed.

socket.toString() shows right address, and socket.isConnected() shows true


try {
Socket socket = new Socket(hubAddress, hubPort);

//out = new PrintWriter(socket.getOutputStream());
BufferedReader bout = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
in = new BufferedReader(new
InputStreamReader(socket.getInputStream()));

hubChat.showMessage(socket.toString() + "\n");
hubChat.showMessage(socket.isConnected() + " <- socket \n");

String str;
while ((str = in.readLine()) != null) {
hubChat.showMessage(str);
System.out.println(str);
}
in.close();
System.out.println("closed");

}

catch (IOException e)
{ e.printStackTrace();
}
}

[telnet session log elided]

Probably nothing is wrong with your socket, although I might be able to
judge better if you had provided your real code -- aside from being a
fragment, the above contains errors. It might also help if you told us
what the code _does_ do.

Have you considered that your telnet client may be doing something that
your application / applet is not? For all its apparent simplicity (if
any) telnet is a non-trivial protocol. Do you have any documentation
for how the hub software / firmware is supposed to behave?

My best bet is that after you connect to the server you have to send it
something to tell it you're there before it sends anything back to you.
It might be as simple as sending a linefeed, or it might require use
of a handshaking protocol of some sort.


John Bollinger
(e-mail address removed)
 

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,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top