2013-09-26 115 views
0

我試圖做一個程序,從遠程服務器(我沒有訪問服務器代碼)讀取數據並打印它。 它崩潰時,我使用的方法InputStream.read(),我拋出這個異常:java.net.SocketException異常:連接復位Socket編程:連接重置異常 - Java

這裏是我的代碼:

import java.io.IOException; 
import java.io.InputStream; 
import java.net.InetSocketAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 


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 = 56668; 

    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, 10000000); 
     } 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 
    } 

    private void decode(){ 

    } 

    private void encode(){ 
     //TODO: função que codifica 
    } 

    public static void main(String[] args) throws UnknownHostException, IOException { 
     ProtocoloX protocolo = new ProtocoloX(); 
     if(protocolo.connect()){ 
      protocolo.receive(); 
      /*protocolo.decode(); 
      protocolo.encode(); 
      protocolo.send();*/ 
     } 

    } 
} 
+0

看看這兩個線程 - http://stackoverflow.com/questions/585599/whats-causing-my-java-net-socketexception-connection-reset和http://stackoverflow.com/questions/62929/java的淨socketexception連接復位 – Ankit

回答

0

服務器具有重置連接。也許你應該在收到之前發送一些東西?