0

I tried to get the image from my pc so that I wrote C# and android code to do that.

But when I start running, I got fatal exception from my android. I can't figure out what it's meaning...

18469-18469/roman10.tutorial.tcpcommserver E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{roman10.tutorial.tcpcommserver/roman10.tutorial.tcpcommserver.TcpServer}: java.lang.NegativeArraySizeException: -1991225785
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$600(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NegativeArraySizeException: -1991225785
            at roman10.tutorial.tcpcommserver.TcpServer.readBytes(TcpServer.java:72)
            at roman10.tutorial.tcpcommserver.TcpServer.runTcpServer(TcpServer.java:54)
            at roman10.tutorial.tcpcommserver.TcpServer.onCreate(TcpServer.java:27)
            at android.app.Activity.performCreate(Activity.java)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
            at android.app.ActivityThread.access$600(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
            at android.os.Handler.dispatchMessage(Handler.java)
            at android.os.Looper.loop(Looper.java)
            at android.app.ActivityThread.main(ActivityThread.java)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
            at dalvik.system.NativeStart.main(Native Method)

And this is my android code

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        imageView = (ImageView) this.findViewById(R.id.imageView);
        try{
            runTcpServer();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final int TCP_SERVER_PORT = 13233;
    private void runTcpServer() throws IOException {

        ServerSocket serverSocket = null;

        try {
            serverSocket = new ServerSocket(TCP_SERVER_PORT);
        } catch (IOException ex) {
            System.out.println("Can't setup server on this port number. ");
        }

        Socket socket = null;
        InputStream in = null;
        try {
            socket = serverSocket.accept();
        } catch (IOException ex) {
            System.out.println("Can't accept client connection. ");
        }

        try {
            in = socket.getInputStream();
            byte[] imageData = readBytes(in);
            Bitmap bmp = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
            imageView.setImageBitmap(bmp);
        } catch (IOException ex) {
            System.out.println("Can't get socket input stream. ");
        }

        in.close();
        socket.close();
        serverSocket.close();

    }

    public byte[] readBytes(InputStream in) throws IOException {
        DataInputStream dis = new DataInputStream(in);

        int len = dis.readInt();
        byte[] data = new byte[len];
        if (len > 0) {
            dis.readFully(data);
        }
        return data;
    }

1 Answer 1

1
int len = dis.readInt();

Here len is negative for some reason

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

1 Comment

@user5121761 Here len is negative for exactly one reason: you've reached end of stream. Similarly, there is exactly one solution. The test should be before the allocation.

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.