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: