Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes use of bufferedwriter.newline() in server/client code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "use of bufferedwriter.newline() in server/client code" Watch "use of bufferedwriter.newline() in server/client code" New topic
Author

use of bufferedwriter.newline() in server/client code

vinit mav
Greenhorn

Joined: May 14, 2011
Posts: 7

--------------------------------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------

on the server side,if i don't write newline() ,why is nothing printed on client side even though i use flush()?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
Well, the client is probably executing the readLine(), which means it will keep waiting until it sees something that signals the end of the line.

Incidentally it's a very bad idea to have those empty catch blocks. If something goes wrong and you do get an exception, the empty catch block will silently catch the exception, without giving you any useful information about what happened. Which makes it very difficult to detect the problem, much less fix it. I suggest replacing the catch blocks with something like:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39791
    
  28
. . . and welcome to the Ranch
vinit mav
Greenhorn

Joined: May 14, 2011
Posts: 7
but why is it so that if i write bufferedwriter.close() instead of bufferedwriter.newline() and bufferedwriter.flush() , then the client successfully prints even though the line is not ending?
thank you very much........
Kacper Jonczyk
Greenhorn

Joined: Aug 25, 2010
Posts: 9
Hi Vinit,

readline() behaves like Mike have specified. It blocks until it runs into "\n", "\r", "\r\n", or EOF.
However it still keeps data already read in. It just won't be returned until function comes across one of previously specified delimiters.

You might want to check out:
readLine()
and
Source which says:

464: // We only want to return null when no characters were read before
465: // EOF.


HTH

Welcome to JavaRanch!
vinit mav
Greenhorn

Joined: May 14, 2011
Posts: 7
thanks Kacper....
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3018
    
  10
You're welcome.
vinit mav
Greenhorn

Joined: May 14, 2011
Posts: 7
i visited a site whatismyipaddress.com and it showed my ip ,but when i put this for the ip address in this program,it didn't work.
why is this program unable to run on internet?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

Maybe because your computer is behind a firewall or a router which does NAT (network address translation), which causes your computer to have a different IP address in the local network than on the outside Internet.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
vinit mav
Greenhorn

Joined: May 14, 2011
Posts: 7
i tried port forwarding but it still isn't working?
is there a solution for this?
vinit mav
Greenhorn

Joined: May 14, 2011
Posts: 7
does the server/client chat application only work on lan and not on internet???
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use of bufferedwriter.newline() in server/client code