Java Socket readInt EOFException

时间:2012-06-29 11:28:48

标签: java android sockets eof

我在手机(Android)和服务器之间做了一个简单的插座。

手机可以连接到服务器,但我可以为彼此发送或读取数据。

以下是我用于客户端的代码(电话方):

private static void connect(String url, int port)
{
    do
    {
        try
        {
            socket = new Socket(url, port);
            input = socket.getInputStream();
            output = socket.getOutputStream();
            inputstream = new DataInputStream(input);
            outputstream = new DataOutputStream(output);
            isConnected = true;
        }
        catch (Exception e)
        {
            try
            {
                Thread.sleep(100);
            }
            catch (Exception ex) {}
        }
    }
    while (!isConnected);
}

public static String sendToServer(String json)
{       
    try
    {
        connect(Constants.SERVER_HOST, Constants.SERVER_PORT);

        if (isConnected)
        {
            Log.e("SOCKET MNGR", "[SOCKET] Sending " + json);
            outputstream.writeInt(json.getBytes().length);
            outputstream.write(json.getBytes());

            int arrLength = inputstream.readInt();
            if (arrLength > 0)
            {
                byte[] fromServer = new byte[arrLength];
                inputstream.read(fromServer, 0, fromServer.length);
                String content = new String( Base64.decode(new String(fromServer)) );
                Log.e("SOCKET MNGR", "[SOCKET] Reading " + content);
                return content;
            }
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }       

    return null;
}

我服务器的那个:

public Client(Socket socket)
{
    try
    {
        this.socket = socket;
        this.input = this.socket.getInputStream();
        this.output = this.socket.getOutputStream();
        this.inputstream = new DataInputStream(this.input);
        this.outputstream = new DataOutputStream(this.output);
    }
    catch (Exception e) {}
}

public void run()
{
    while (true)
    {
        try
        {
            int arrLength = inputstream.readInt();
            byte[] fromClient = new byte[arrLength];
            inputstream.read(fromClient, 0, fromClient.length);
            String request = new String(fromClient);
            String read = new String( Base64.decode(request) );
            String[] data = request.split("###");

            if (data.length == 2 && data[0].equals("commit"))
            {
                CommitInterface.commit(data[1], true);
                outputstream.writeInt(0);
                outputstream.write(new byte[] {});
            }
            else if (data.length == 3 && data[0].equals("evaluate"))
            {
                EvaluateInterface.eval(data[1], data[2].equals("true") ? true : false);
                outputstream.writeInt(0);
                outputstream.write(new byte[] {});
            }
            else if (data.length == 2 && data[0].equals("publish"))
            {
                String content = PublishInterface.publish(new Vector<String>(Arrays.asList(data[1].split("|"))));
                String encoded = Base64.encode(content.getBytes());
                outputstream.writeInt(content.getBytes().length);
                outputstream.write(content.getBytes());
            }
        }
        catch (Exception e)
        {
            this.disconnect();
        }
    }
}

当我在手机上使用功能sendToServer时,我收到了以下日志:

06-29 13:26:05.823: E/Kramer(1781): [SOCKET] Sending commit###{"user":"me"}
06-29 13:26:05.823: W/System.err(1781): java.io.EOFException
06-29 13:26:05.823: W/System.err(1781):     at java.io.DataInputStream.readInt(DataInputStream.java:287)
06-29 13:26:05.823: W/System.err(1781):     at com.vodemki.network.KramerCommunicator.sendToServer(ServerCommunicator.java:78)
06-29 13:26:05.823: W/System.err(1781):     at com.vodemki.activities.PhoneBookActivity$6.run(PhoneBookActivity.java:298)

第78行是int arrLength = inputstream.readInt();

知道我为什么会收到这个错误吗?

感谢。


我尝试使用UDP,但我无法从智能手机中读取内容。

我的代码如下:

服务器端:

public void run()
{
    byte[] inData = new byte[48];
    byte[] outData = new byte[48];
    String message;
    DatagramSocket socket;

    try
    {
        socket = new DatagramSocket(this.port);
        while (true)
        {
            DatagramPacket in = new DatagramPacket(inData, inData.length);
            socket.receive(in);
            InetAddress senderIP = in.getAddress();
            int senderPort = in.getPort();
            message = new String(in.getData(), 0, in.getLength());
            System.out.println("Got "+message+" from "+senderIP+":"+senderPort);
            outData = "Pong".getBytes();
            DatagramPacket out = new DatagramPacket(outData, outData.length, senderIP, senderPort);
            socket.send(out);
        }
    }
    catch (Exception e) {}
}

客户端(智能手机):

public static String sendToServer(String json)
{       
    try
    {
        DatagramSocket socket = new DatagramSocket();
        InetAddress serverIP = InetAddress.getByName(Constants.SERVER_HOST);
        byte[] outData = ("Ping").getBytes();
        DatagramPacket out = new DatagramPacket(outData,outData.length, serverIP, Constants.SERVER_PORT);
        Log.e("kramer", "SENDING SOCKET");
        socket.send(out);
        Log.e("kramer", "SOCKET SENT");
        socket.close();
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }       

    return null;
}

智能手机成功记录了两条线路,但我的服务器上没有收到任何信息。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

问题解决了。

防火墙阻止了传入连接。我只是将端口打开为UDP。