Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
vinit mav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

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

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
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49466
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
vinit mav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Kacper....
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.
 
vinit mav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vinit mav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried port forwarding but it still isn't working?
is there a solution for this?
 
vinit mav
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
does the server/client chat application only work on lan and not on internet???
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic