简体   繁体   English

套接字中的Java堆内存错误

[英]Java Heap Memory error from socket

I am trying to open a socket and listen. 我正在尝试打开一个套接字并听取。 Clients written in PHP will then send XML requests. 用PHP编写的客户端将发送XML请求。 At the moment I am just send the string "test" to it and I am getting a Memory Heap Error. 目前我只是发送字符串“test”到它,我得到一个内存堆错误。

Here is my java code for the server: 这是我的服务器的Java代码:

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. 我已经做了一个搜索,这里有很多关于这个错误的解释,但是我无法弄清楚为什么当我发送一个4个字母时,String len是1952805748。

Well you are getting the out of memory error because the len is so huge. 好吧,你得到了内存不足的错误,因为len是如此巨大。 If you are sending the data as characters and then doing a readInt() on it, then that's what's causing your problem. 如果您将数据作为字符发送,然后对其执行readInt() ,那么这就是导致问题的原因。 You need to read the data as characters. 您需要将数据作为字符读取。

Your numeric valid is probably the binary for the string "test". 您的数字有效可能是字符串“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. 你应该只是从InputStream读取一个字符串,不知道为什么你需要一个DataInputStream ,因为它支持读取二进制等,这不是你正在做的事情。 Just use a BufferedInputStream and then do a normal read on it. 只需使用BufferedInputStream然后对其进行正常读取即可。

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. 为了扩展Francis Upton的答案,你得到一个堆异常,因为你试图从传入的套接字流中读取n个字节,其中n代表你在处理循环开始时读取的完全任意整数。 And the reason I call it totally arbitrary is because you never actually sent a separate int in your client code. 我称之为完全随意的原因是因为你从未在客户端代码中实际发送过单独的int。 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. 因此,您的代码只是从输入流的前4个字节中的任何内容读取一个int,这可能是任何东西。

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). 看看Apache Commons IO中的IOUtils ,它包含一次性读取整个数据流的好方法( toByteArraytoString等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM