Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Unable to read inputstream

Thread view: 
sconeek@gmail.com - 06 Jun 2006 00:47 GMT
hi all,
i am implementing socket tcp/ip comm within my app.
now i am able to read data from the inputstream, however in my reads i
am sometimes missing the first character of my string, so if the
message send is Java i am receiving ava, without the first character.
also each message is separated by a carriage return. so i am reading
till i get a carriage return. any help.
Matt Humphrey - 06 Jun 2006 01:12 GMT
> hi all,
> i am implementing socket tcp/ip comm within my app.
[quoted text clipped - 3 lines]
> also each message is separated by a carriage return. so i am reading
> till i get a carriage return. any help.

Most likely there is something improper with how you are reading.   There
are quite a number of things that you can get wrong, including stream
encoding, flushing, buffer length, etc.  You'll have to show your code for
anyone to say what may be wrong.

Cheers,
Matt Humphrey matth@ivizNOSPAM.com  http://www.iviz.com/
sconeek@gmail.com - 06 Jun 2006 01:19 GMT
ok here we go, i hope somebody can help me now,

StringBuffer line;
int byyyte;
byte buffer1[] = new byte[1];

while (sin.read() > 0)
{
line = new StringBuffer();
do    {
if((byyyte = sin.read()) != -1 && ((byyyte != 13) || (byyyte != 10))) {
buffer1[0] = (byte)byyyte;
line.append(new String(buffer1));       
}
}
while(byyyte != 13);
EJP - 06 Jun 2006 01:59 GMT
> ok here we go, i hope somebody can help me now,
>
> while (sin.read() > 0)

You just threw away 8 bits.
sconeek@gmail.com - 06 Jun 2006 01:57 GMT
ok so should i change that to != -1

> > ok here we go, i hope somebody can help me now,
> >
> > while (sin.read() > 0)
>
> You just threw away 8 bits.
Matt Humphrey - 06 Jun 2006 02:53 GMT
> ok so should i change that to != -1

No.  sin.read() reads the first byte of the stream.  You then test for EOF
and discard the character, which is why you're missing one.

Aside from that, however, your code badly mixes bytes and characters.  If
you move to different platforms or places with different encodings this can
cause you problems. If you're providing both the sender and receiver there
are much easier ways to do this, particularly for characters. All that
line-reading business is taken care of for you, as well as ensuring that the
characters on both sides match.

BufferedReader br = new BufferedReader (
  new InputStreamReader (sin, "UTF-8"));
String s = br.readline ()
while (s != null) {
   // Process the line
   s = br.readline ()
}

The sender simply has to make a corresponding PrintWriter
PrintWriter pw = new PrintWriter (
   new OutputStreamWriter (outstream, "UTF-8"),
   true /* autoflush */);

pw.println ("Here is the message");

Then you can just send text strings back and forth.  A common technique is
for the client to send to the server, who reads a line and then sends a line
in reply.  That is, the client goes write-read and the server goes
read-write.

Cheers,
Matt Humphrey matth@ivizNOSPAM.com http://www.iviz.com/ 
sconeek@gmail.com - 06 Jun 2006 03:32 GMT
i have tried to implement your approach, but its not working for me. i
am unable to even read any messages.

and my old approach picked up the first characters in some messages and
in some it missed, so its working but why is it working only on a few
and not others. will keep on trying in the meantime.
sconeek@gmail.com - 06 Jun 2006 03:38 GMT
sorry mate, modified my approach and it works well now. thanks a lot
for your help.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.