• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot read more than twice on BufferedReader/Inputstream  RSS feed

 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the title says i cannot read more than twice.
I can read the first line and than on the second time and on its giving me an exception.

Server:




Client:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exception?
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Cannot read message"
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the exception. That's the String you print out when you catch the IOException.

Trying printing the actual stacktrace. It should at least help you figure out which line is causing hte issue.
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Socket is closed
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really know nothing about sockets and this kind of stuff, but if the problem is that the socket is closed, maybe you should not close it?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your server code looks a bit complicated. Why create an InputStreamReader (line 18), then wrap that in a BufferedReader (line 20), then read a line from that into a ByteArrayInputStream (encoding it using UTF-8), and then reading that again with a different BufferedReader and InputStreamReader (line 23; using the default character encoding which is not necessarily UTF-8)? It looks much more complicated than necessary.

Look at what your server code does. It accepts a client connection, then reads one line, prints it back, and then closes the connection. There's no loop anywhere to handle multiple lines.

Can you explain more clearly what exactly you expected both the server and client programs to do, what they actually do, and what the difference is between what you expected and what actually happens?
 
Amit Shef
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I managed to work it
When i set up a loop on client the client always connected to the server with a new port or ip so it did not recognize the ip or port , Sorry if i can't explain it right.
Thanks anyway.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!