aspose file tools*
The moose likes Sockets and Internet Protocols and the fly likes IO Streams and sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "IO Streams and sockets" Watch "IO Streams and sockets" New topic
Author

IO Streams and sockets

Mat Cauthon
Greenhorn

Joined: Mar 29, 2006
Posts: 10
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
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8895
    
    8

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).


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Mat Cauthon
Greenhorn

Joined: Mar 29, 2006
Posts: 10
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

Joined: Oct 29, 2001
Posts: 8895
    
    8

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

Joined: Mar 29, 2006
Posts: 10
Did you change both instances of write.print? You had one on the server and one on the client.


Yes I did
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Mat Cauthon
Greenhorn

Joined: Mar 29, 2006
Posts: 10
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 ]
Martin Simons
Ranch Hand

Joined: Mar 02, 2006
Posts: 196
As a side note, could you possibly be a robert jordan fan matrim.
Mat Cauthon
Greenhorn

Joined: Mar 29, 2006
Posts: 10
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

Joined: Mar 02, 2006
Posts: 196
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

Joined: Oct 29, 2001
Posts: 8895
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO Streams and sockets