尝试从Socket读取时连接重置

时间:2013-10-18 14:42:06

标签: java sockets tcp

我正在开发一个简单的应用程序来从远程服务器读取数据(我无法访问服务器代码)。 它正在进行连接过程,但是当我尝试从服务器读取一些数据时,连接将重置。

这是我的代码:

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

    }
}

1 个答案:

答案 0 :(得分:0)

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