简体   繁体   中英

Java Socket readInt EOFException

I've made a simple socket between my phone (Android) and my server.

The phone is able to connect to the serveur but I can send or read data for eachother.

Here is the code I use for my client (Phone side):

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;
}

And the one for my server:

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();
        }
    }
}

When I use the function sendToServer on my phone, I got the following log:

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)

The line 78 is int arrLength = inputstream.readInt(); .

Any idea why I get this error?

Thanks.


I've tried using UDP and I wasn't able to read content from the smartphone.

My code is the following:

Server side:

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) {}
}

Client side (smartphone):

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;
}

The smartphone successfully logs the two lines but nothing is received on my server. Any idea?

Problem solved.

The firewall was blocking the incoming connection. I've simply opened the port as UDP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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