• 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

I/O client server excercise

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
this is the excercise I'm trying to do:

write to java classes client and server with these features:
Server: Accept messages from the client (sent as lines of text) and returns them to the client numbering them.
Client: sends messages in lines of text to the server.
The connection is closed with the string "bye".
eg:
<the client sends>"Hello server";
<the client receives> "Message 1: Hello server!"
<the client sends> "What a beautiful day!"
<the client receive> "Message 2: What a beautiful day!"

The problem is that after the second message sent the client ends with an exception:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:2367)
at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130)
at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114)
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:535)
at java.lang.StringBuffer.append(StringBuffer.java:322)
at java.io.BufferedReader.readLine(BufferedReader.java:363)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at clientserver.Client.receive(Client.java:80)
at clientserver.Client.send(Client.java:67)
at clientserver.Client.<init>(Client.java:39)
at clientserver.Client.main(Client.java:48)
Java Result: 1

I'm sure it's a issue of how I wrote the receive method in the client class or the send method in the server class.
If I comment these methods the server receives messages from the client and ends without any problems.

These are the classes:

Server:


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have nested loops:




So you get a message, and as long as that one single message that you got (which never changes) is not "bye", you're sending back a response. A single message produces infinitely many copies of the same resopnse.

The send() method does not need a loop. Your loop should be


but what you have is


Once you get into that inner loop, you can never get out.
 
Simone Sarigu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much
I wouldn't ever came out of that mess but I'm still doing something wrong

I'm proceeding by trial and error. I've rewritten the send method in different ways.
I think this is the closest at the result:


In this way the client receives back the first message and the server throws an IOException:
java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:150)
at java.net.SocketInputStream.read(SocketInputStream.java:121)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at clientserver.Server.receive(Server.java:76) ----------------------> messaggio = bfIn.readLine();
at clientserver.Server.<init>(Server.java:36)
at clientserver.Server.main(Server.java:53)

Why that line? I'd expected this pw.close();

 
Simone Sarigu
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
no one?
reply
    Bookmark Topic Watch Topic
  • New Topic