1

I am trying to open a socket and listen. Clients written in PHP will then send XML requests. At the moment I am just send the string "test" to it and I am getting a Memory Heap Error.

Here is my java code for the server:

import java.io.DataInputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class main {

    /**
     * @param args
     */
    public static void main(String[] args) {
        server();

    }

    public static void server() {
        ServerSocket MyService = null;
        try {
            MyService = new ServerSocket(3030);
        }
        catch (IOException e) {
            System.out.println(e);
        }

        Socket serviceSocket = null;
        try {
            serviceSocket = MyService.accept();
        }
        catch (IOException e) {
            System.out.println(e);
        }

        DataInputStream in;
        try {
            in = new DataInputStream(serviceSocket.getInputStream());
            System.out.println("DEV STEP 1");
            int len = in.readInt();
            System.out.println(len);
            byte[] xml = new byte[len];
            in.read(xml, 0, len);
            //System.out.print(xml.toString());
            //Document doc = builder.parse(new ByteArrayInputStream(xml));

        }
        catch (IOException e) {
            System.out.println(e);
        }

    }

}

The error I am getting is:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at main.server(main.java:39)
    at main.main(main.java:12)

I have done a search and there are plenty of explanations of this error on here, however I can not work out why when I am sending a 4 letter String len is 1952805748.

1
  • Are you using a DataOutputStream to connect to port 3030? Commented Jan 3, 2012 at 0:53

2 Answers 2

3

Well you are getting the out of memory error because the len is so huge. If you are sending the data as characters and then doing a readInt() on it, then that's what's causing your problem. You need to read the data as characters.

Your numeric valid is probably the binary for the string "test". You should just read a string from the InputStream, not sure why you need a DataInputStream as that's something that supports reading binary, etc, which is not what you are doing. Just use a BufferedInputStream and then do a normal read on it.

Sign up to request clarification or add additional context in comments.

Comments

1

To expand on Francis Upton's answer, you are getting a heap exception because you are trying to read n bytes from the incoming socket stream, where n represents the totally arbitrary integer you read at the beginning of your processing loop. And the reason I call it totally arbitrary is because you never actually sent a separate int in your client code. So your code is simply reading an int from whatever is in the first 4 bytes of the input stream, which could be anything at all.

Take a look at IOUtils in Apache Commons IO, it contains nice methods for reading an entire data stream in one shot (toByteArray, toString, etc).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.