confused: Socket InputStream != ServerSocker InputStream

R

R

Hello everybody.

I've got very stange situation.

I have a client using Socket and server applicaton using ServerSocket.

Server reads sth from client with this code:
while (-1 != (read = is.read(buffer))) { /* 'is' is InputStream */
for (int i = 0; i < read; i++) {
sb.append((char)buffer); /* 'sb' is StringBuffer */
}
}

read method NEVER returns -1, when client sends eg a basic message:
os.write("why oh why? don't You see the end of this
text?".getBytes());

so I have to do a trick and I hate this solution - all messages are
terminated with fullstop eg. client sends:
os.write("why oh why? don't You see the end of this
text?.".getBytes());

and the server:
while (46 != (read = is.read())) { /* 46 stands for ASCI '.' */
sb.append((char)read);
}
it works now but it's so lame solution...

but on the other hand when server sends a message to client:
os.write("roger that and over ;-)".getBytes());

client can read it without any prblem using exactly the same code as
the first I show You:
while (-1 != (read = is.read(buffer))) {
for (int i = 0; i < read; i++) {
sb.append((char)buffer);
}
}

and that's why I am so confused

why does Socket's and ServerSocket's Streams differs?

Am I doing sth wrong?

I'm using Linux but it's not a case...

can You advise me sth?

thanks in advance

best regards
R
 
K

klynn47

What happens if you wrap the InputStream in a BufferedInputStream and
then use the read method of BufferedInputStream?
 
K

Kevin McMurtrie

Hello everybody.

I've got very stange situation.

I have a client using Socket and server applicaton using ServerSocket.

Server reads sth from client with this code:
while (-1 != (read = is.read(buffer))) { /* 'is' is InputStream */
for (int i = 0; i < read; i++) {
sb.append((char)buffer); /* 'sb' is StringBuffer */
}
}

read method NEVER returns -1, when client sends eg a basic message:
os.write("why oh why? don't You see the end of this
text?".getBytes());

so I have to do a trick and I hate this solution - all messages are
terminated with fullstop eg. client sends:
os.write("why oh why? don't You see the end of this
text?.".getBytes());

and the server:
while (46 != (read = is.read())) { /* 46 stands for ASCI '.' */
sb.append((char)read);
}
it works now but it's so lame solution...

but on the other hand when server sends a message to client:
os.write("roger that and over ;-)".getBytes());

client can read it without any prblem using exactly the same code as
the first I show You:
while (-1 != (read = is.read(buffer))) {
for (int i = 0; i < read; i++) {
sb.append((char)buffer);
}
}

and that's why I am so confused

why does Socket's and ServerSocket's Streams differs?

Am I doing sth wrong?

I'm using Linux but it's not a case...

can You advise me sth?

thanks in advance

best regards
R


Read the documentation. A length of -1 indicates that the stream is
closed. If you don't close the stream on the server side, the client
will never see the -1.

Implementing record terminators is up to you. Object serialization can
do some of the work for you.
 
H

HK

R said:
Hello everybody.

I've got very stange situation.

I have a client using Socket and server applicaton using ServerSocket.
[...]
read method NEVER returns -1, when client sends eg a basic message: [...]
client can read it without any prblem using exactly the same code as

I doubt you show all the code in your original post. The -1 shows
up as soon as the sender --- whether client or server makes
no difference --- closes its OuputStream.

Your problem is that the client cannot close the OutputStream
because this would close the socket too. But there is

Socket.shutdownOutput()

for exactly this purpose. It signals eof but leaves the
Socket open to be able to read the response.

If you need to exchange multiple request/response pairs,
then indeed you need to do your own marking up of the
end of a message and pray that nothing gets stuck in
any buffering scheme, whether in Java or in the OS.

Harald.
 
R

R

Read the documentation. A length of -1 indicates that the stream is
closed. If you don't close the stream on the server side, the client
will never see the -1.

thanks shutdownOutputStream() helped ;-)
Implementing record terminators is up to you. Object serialization can
do some of the work for you.

no, using terminators wasn't the best idea


thanks once again
best regards
R
 
K

Kevin McMurtrie

What happens if you wrap the InputStream in a BufferedInputStream and
then use the read method of BufferedInputStream?

No functional difference.

A BufferedInputStream improves performance when you need to read many
small quantities of data from an InputStream having a high call
overhead. Streams to native sockets and files are expensive to interact
with in some JVMs. A parser that reads one byte at a time can benefit
greatly from a buffer. Worst case performance from a buffer usually
comes from alternating between very small and very large transfers,
which alternates the stream between buffering and pass-through modes.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top