Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > confused: Socket InputStream != ServerSocker InputStream

Reply
Thread Tools

confused: Socket InputStream != ServerSocker InputStream

 
 
R
Guest
Posts: n/a
 
      03-12-2005
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[i]); /* '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[i]);
}
}

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
 
Reply With Quote
 
 
 
 
klynn47@comcast.net
Guest
Posts: n/a
 
      03-12-2005
What happens if you wrap the InputStream in a BufferedInputStream and
then use the read method of BufferedInputStream?

 
Reply With Quote
 
 
 
 
Kevin McMurtrie
Guest
Posts: n/a
 
      03-12-2005
In article <(E-Mail Removed) >,
http://www.velocityreviews.com/forums/(E-Mail Removed) (R) wrote:

> 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[i]); /* '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[i]);
> }
> }
>
> 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.
 
Reply With Quote
 
HK
Guest
Posts: n/a
 
      03-12-2005
R wrote:
> 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.

 
Reply With Quote
 
R
Guest
Posts: n/a
 
      03-12-2005
> 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
 
Reply With Quote
 
Kevin McMurtrie
Guest
Posts: n/a
 
      03-13-2005
In article <(E-Mail Removed) .com>,
(E-Mail Removed) wrote:

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments