android中的socket编程得到了Fatal Exception

时间:2015-09-19 07:08:56

标签: android sockets

我试图从我的电脑上获取图像,以便我编写C#和android代码来做到这一点。

但是当我开始跑步时,我从我的机器人那里得到了致命的异常。 我无法弄清楚这是什么意思......

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)

这是我的安卓代码

    @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 个答案:

答案 0 :(得分:1)

int len = dis.readInt();

这里len因某种原因是否定的