1

I'm developing a simple application to read data from a remote server (I don't have access to the server code). It's going through the connection process, but when I try to read some data from the server, the conection resets.

Here's my code:

import java.io.IOException;
import java.io.InputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class ProtocoloX {
    private byte[] bytes = new byte[1024];
    // private byte[] bytes = new byte[] { (byte) 0xC6, 0x57, 0x54, (byte) 0x95,
    // 0x5E, (byte) 0x9E, 0x6B, (byte) 0xC6, 0x55, 0x17, 0x55, 0x52,
    // (byte) 0x9E, 0x21 };
    private Socket cliente;
    private final String HOST = "177.71.195.77";
    private final int PORT = 52246;
    private Map<Byte, Byte> table4x5 = new HashMap<Byte, Byte>();
    private Map<Byte, Byte> table5x4 = new HashMap<Byte, Byte>();
    private String msgReceived = new String();
    private byte[] packetsToSend;
    private int packetsToSendLength = 0;

    public ProtocoloX() {
        table4x5.put((byte) 0x0, (byte) 0x1e);
        table4x5.put((byte) 0x1, (byte) 0x09);
        table4x5.put((byte) 0x2, (byte) 0x14);
        table4x5.put((byte) 0x3, (byte) 0x15);
        table4x5.put((byte) 0x4, (byte) 0x0a);
        table4x5.put((byte) 0x5, (byte) 0x0b);
        table4x5.put((byte) 0x6, (byte) 0x0e);
        table4x5.put((byte) 0x7, (byte) 0x0f);
        table4x5.put((byte) 0x8, (byte) 0x12);
        table4x5.put((byte) 0x9, (byte) 0x13);
        table4x5.put((byte) 0xa, (byte) 0x16);
        table4x5.put((byte) 0xb, (byte) 0x17);
        table4x5.put((byte) 0xc, (byte) 0x1a);
        table4x5.put((byte) 0xd, (byte) 0x1b);
        table4x5.put((byte) 0xe, (byte) 0x1c);
        table4x5.put((byte) 0xf, (byte) 0x1d);

        table5x4 = invert(table4x5);

    }

    private static Map<Byte, Byte> invert(Map<Byte, Byte> map) {

        Map<Byte, Byte> inv = new HashMap<Byte, Byte>();

        for (Entry<Byte, Byte> entry : map.entrySet())
            inv.put(entry.getValue(), entry.getKey());

        return inv;
    }

    public boolean connect() {
        this.cliente = new Socket();
        System.out.println("-- Trying to connect: " + HOST + ":" + PORT);
        InetSocketAddress socketAddress = new InetSocketAddress(HOST, PORT);
        try {
            this.cliente.connect(socketAddress);
        } catch (IOException e) {
            System.out.println(e);
            System.out.println("-- CONNECTION PROBLEM ");
            return false;
        }

        System.out.println("-- Connection successful");
        return true;
    }

    private void receive() {
        InputStream stream = null;
        System.out.println("-- Reading data...");
        try {
            stream = this.cliente.getInputStream();
            try {
                int count = stream.read();
                System.out.println((char) count);
            } catch (IOException e) {
                System.out.println("-- DATA READING PROBLEM");
                e.printStackTrace();
            }
        } catch (IOException e) {
            System.out.println("-- DATA READING PROBLEM");
            e.printStackTrace();
        }
        System.out.println("-- Data read successful");
    }

    private void send() {
        // TODO: função que envia sinal
    }

    public static void main(String[] args) throws UnknownHostException,
            IOException {

        ProtocoloX protocolo = new ProtocoloX();
        if (protocolo.connect()) {
            protocolo.receive();
        }

    }
}
4

1 回答 1

0

这通常发生在您写入已被对等方关闭的连接之后。换句话说,应用程序协议错误。

于 2013-10-18T19:58:45.550 回答