• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with DataInputStream.readInt() method

 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everyone,

I have a slight problem.Im working with a client server pair on my own computer and just runing the server on this computer as well.On the client side i am trying to send an updated file to the server, then save that file on the servers drive (in this case my drive just another folder then where the client is sending the file from ).

Iv already learned how to send a file from a server to a client and save it on the client local drive, now im just trying to do the oposite and figured it would be quite easy.I think i am runing into a timing issue when im caling the methods for sending the file on the client side, and recieveing it on the server side, but im not sure, because i just leanred how to write client servers last week.

I've traced through the code tons of times and again have it narrowed down to the readInt() on the server side...im kind of wondering if the server "waits" at this line until a writeInt(int val) on the client side occurs.If it does wait for the writeInt() to occur on the client side, then im not quite sure whats wrong with my code.In fact i ran the code one time and it worked, i have no idea why it owuld work then not work the next time...anyways..it hasnt worked since so im here.. .

I'm just kind of confused too because on my client side iv put numerous println's to see what was goin down, and the method where i send the file to the server...i have a writeInt() at this point i think the server should be waiting for a writeInt() to come through the stream...but yah..anyways after that writeInt() i also have a write(byte[] buff), and the printlns show these are both being written out on the stream, but its geting an IOError on the server side when it tryes to do the readInt(), so im not quite sure how it does the write(byte[] buff) if the server couldnt even readInt().

Since this post has kinda goten prety long and ppl dont like reading long things...ill post the code for the two methods in new post.

Hopefully i didnt confuse everyone in all that...
If you have any ideas what im doing wrong before or after u look at the code feel free to critsize me!
thnks
-luc
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
allrighty,heres the code. not very long or intense..
This is the method thats caleed on the client side, not that updateDout is a DataOutputStream.



and this is the method called on the server side, note that updateDis is a DataInputStream



ahh yes in my GUI application im first sending a command to the server over a PrintWriter and then the server side has an if statment that says something like...if(getCommand().equals("update"))--->then writePreferencesFileFromClient()

this is where i think i hit the timing issue because on the client side GUI once it sends the "update" command it goes and does the sendPreferencesFileToServer() method but im not sure if the writePref....() had time to get to the readInt() before the client sent the writeInt()...or im not even sure if that matters...I know very little about it all..anyways long post agian ill shutup until someone has an idea!thanks a bunch!
-luc
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
actually im wondering something wich just occured to me.

Im sneding strings with the printWriter from the client to the server, so does this means that i cannot also have an output stream that is something else....like i used DataOutputStream as well, i didnt think it would be a problem as long as a made a DataInputStream on the server side as well...and when it came time to use the dataoutputStream(when im about to send the file,or first...the file size)i would use the DataInputStream on the server side after it read the coommand from a bufferedReader(new inputStreamReader(socket.getInputStream)).

i switched then to the DataInputStream on the server side wich was also decalred like this DataInputStream updateDis= new ...(socket.getInputStream());

still if you tell me this is a problem then how th heck did it work once?
Just me thinking outloud.
-luc
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't have time right now to scan the code, but I'll try tonight. One thing caught my eye, however. Be aware that buffered input streams read more than you ask for initially, and then feed it to you from the buffer as you ask.

Say on the sending side you write out 10 lines 10 characters long each (110 characters total counting linebreaks). If you use a BufferedReader on the receiving side (say with a 2000 character buffer) to read one line, the reader will try to fill its buffer immediately. In this case, all 110 characters will be read from the stream into the buffer.

Now, if you throw out the BufferedReader and attach a DataInputStream to the same InputStream the reader read from and call a read method on it, the buffer still holds the 9 other lines from before, so they are "lost" to the other code. You need to be extra careful with mixing buffering and streams.
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey david

This is the same app i was working on from my last thread, so you kinda know whats going on.But yah i can see what ur talking about with the streams, i wonder....actually, i duno im not really sure how else to do this without sending the server a command, because i have two buttons on my GUI ok and cancel, when the user clicks cancel i wasnt the connection to be severed and the application to end, but when they click ok i want thier file to be updated on the server.

I supose i could just assume on the server that the next read it does will be the size of the file they are about to recieve then go from there, and as for the cancel button....just not send a command over at all, just end the connection right there on the client side.If you cant find an easy fix for this dont rack your brain allright, its probably not a problem to do it the way i just suggested.None the less thanks again for the help!

-luc
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My first recommendation is to stick to one type of stream per direction if you can. Since the file is going both directions, you'd like to use DataIOStreams, right? Try to get rid of the Writer/Reader. For your commands you can simply use integer constants instead of Strings:Now you can use readInt() instead of readLine() which is only in Reader.

Let me know if this won't be possible or if you have trouble changing the code. If the latter, please post the updated code and maybe start a new thread and subject.
[ February 03, 2005: Message edited by: David Harkness ]
 
luc comeau
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your exactly right! i infact just got that idea from a java example from sun.com where it was a client server situation where the protocol was that of a knock knock joke, and the "states" that the process was in were represented by integers.

I havnt yet done this, because i was working on adding other things like a login and all that jazz.But ill try it today, im prety sure it will work.Another quality idea from mr.Harkness.Great job thanks, ill try it and if im having problems ill post thanks guy!
 
reply
    Bookmark Topic Watch Topic
  • New Topic