• 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

IO Streams and sockets

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a question on the use of IO streams and socks.

Here is a simple echo client:


The echo server:



This works exactly as I would expect. Then I tried it using the example in Head First Java, using BufferedReader and PrintWriter, because it looks easier to just send/recv all data as a string.

client:



server:



The problem is that nothing happens, it just blocks. What am I doing wrong?

Another question, are either of these methods language independant? Meaning, could a client written in C be able to connect to this server?

Thanks in advance
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You write a stream of text:

but you are trying to read a line:

The above code blocks waiting for the line to be terminated.
You could use write.println() to terminate the line, but that is not recommended.
Yes, sockets can be used to communicate between programs written in different languages, but you have to be careful to use the correct data (i.e. Strings in Java are Unicode, C code usually is ASCII).
 
Mat Cauthon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok, thanks. It looks like using the first example will actually be easier and have less potential issues. Would you agree? Eventually I would like to write a full-featured, non-HTTP chat client/server, and am trying to get all the communication issues hammered out now. I wonder why HF Java uses BufferedReader and Printwriter and doesn't mention these issues.

edit: I tried fixing the second example, but it still blocks. Could you show me a small amount of code that will not block, please?

Lets say I am writing an FTP server in Java, should there be any major issues I need to know up front when I don't know the platform that clients will be using? Integral values are used as codes to communicate what each should do next and then needing to wrap a binary or hex file into something that, say, a C FTP client can deal with. I guess I will have to read up more on converting unicode to ascii.
[ March 29, 2006: Message edited by: Mat Cauthon ]
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mat Cauthon:
I wonder why HF Java uses BufferedReader and Printwriter and doesn't mention these issues.



Lots of "instructive examples" are not "best practices". They just show a simple way to do things and leave a lot of the details to the reader.



edit: I tried fixing the second example, but it still blocks. Could you show me a small amount of code that will not block, please?



Did you change both instances of write.print? You had one on the server and one on the client.



Lets say I am writing an FTP server in Java, should there be any major issues I need to know up front when I don't know the platform that clients will be using?



Off the top of my head, I can't say. I've never done it so it's hard to predict the pitfalls
 
Mat Cauthon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Did you change both instances of write.print? You had one on the server and one on the client.



Yes I did
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you flush()? Re PrintWriter:

Unlike the PrintStream class, if automatic flushing is enabled it will be done only when one of the println() methods is invoked, rather than whenever a newline character happens to be output...

 
Mat Cauthon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, that did the trick. It is frusterating that HF java didn't mention it at all, not even in their code examples. I have two other HF books and are generally fantastic, that I thought getting HF Java to learn a bit more of the language and the new 1.5 stuff was a good idea. So far it has proved to be a poor book, especially the GUI sections. Although it does have some great sections, but is too often frusterating, and I am not a newbie, although obviously not an expert.

So everytime print(), from PrintWriter, using Sockets I need to do this or set autoflush in the constructor? I thought that PrintWriter was not supposed to care, or really know what the underlying stream really is. I use printwriter all the time for writing to files, and have never had this issue.

It seemed like using these higher level IO classes would make life easier, but it turned out that DataInputStream and DataOutputStream is more straight forward, less likely to have problems. Being able to send everything as a string has its charms, but having to flush alot seems that it is not worth it.

What is your opinion about DataOutputstream vs PrintWriter? Does one have any specific advantages or disadvantages, other then my frusterations already vented here?

Like I said, I am working up to writing a rather complex(at least for me) Chat client/server, with everything you would expect from a chat room and then some. I have most of what I need, other then trying to figure out the insanely convoluted Swing Layouts, and would like your opinion on which IO route I should go, based on the 2 client/server examples. For the most part only integers and strings will be sent back and forth, at least until I am ready to write the autoupdater.

The reason I asked about a C program communicating with a java server, is because I am also working on Simple client/server games that can be started from the java client, but is actually a C/C++ wxwidgets project, and the server I want to write in java since I want to keep everything platform independant, and I think writing a PI server in C would be more hassles then it is worth. No reason for the langauge/GUI library switch other then getting practice doing lots of other stuff.

Maybe these other questions should go into seperate threads, so I apologize if this is a bit much for a single thread. I realize that these are very general questions, and am not looking for specific answers, just a few basic ideas of yours and/or links to good information.

Thanks again for the help! Both of you.
[ March 30, 2006: Message edited by: Mat Cauthon ]
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As a side note, could you possibly be a robert jordan fan matrim.
 
Mat Cauthon
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually no. I have read that convoluted series three times and have learned to hate it, yet still somehow like it.

I wondered how long it would take for someone to catch it.
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Through book 7 I liked it, but after that the story line just does not
continue, which makes the whole thing a drag, but, that's probably enough
of that.
[ March 30, 2006: Message edited by: Martin Simons ]
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mat Cauthon:
I use printwriter all the time for writing to files, and have never had this issue.



Sure, because you don't look at the file contents until you are done writing. Flush once at the end and you're fine. Here you are literally trying to read a file in mid-write so you have to take a little more care to synchronize your producer and consumer.


What is your opinion about DataOutputstream vs PrintWriter? Does one have any specific advantages or disadvantages, other then my frusterations already vented here?


Each IO class has a specific purpose. Using the right tool for the job is what makes a task easier, not just using a higher-level class. DataOutputStream, for example, is used to write primitive data in a portable form. The output must be read with a DataInputStream, so it is probably not an option for language agnostic IO. PrintStream on the other hand is specifically for text. We've already talked about Java and Unicode, but if you are trying to make an FTP program, text streams are a bad choice for transmitting binary data.
Have a look at the Java Tutorial IO chapter for the different IO classes and their use.
Maybe your first step should be to duplicate the above code using a C client and Java server then vice-versa. I haven't written any substantial C code in 10 years, so I'm not sure what the state-of-the-art as far as IO is.
 
expectation is the root of all heartache - shakespeare. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic