Note that there are some explanatory texts on larger screens.

plurals
  1. POConnection resets while trying to read from Socket
    primarykey
    data
    text
    <p>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.</p> <p>Here's my code:</p> <pre><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&lt;Byte, Byte&gt; table4x5 = new HashMap&lt;Byte, Byte&gt;(); private Map&lt;Byte, Byte&gt; table5x4 = new HashMap&lt;Byte, Byte&gt;(); 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&lt;Byte, Byte&gt; invert(Map&lt;Byte, Byte&gt; map) { Map&lt;Byte, Byte&gt; inv = new HashMap&lt;Byte, Byte&gt;(); for (Entry&lt;Byte, Byte&gt; 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(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload