2013-12-12 87 views
0

我想要的只是從wunderground打印到控制檯響應插座保持打開狀態:打印的BufferedReader而

public class Weather { 

    public static void main(String[] args) { 
     String host = "rainmaker.wunderground.com"; 
     int port = 3000; 
     int c; 
     { 
      try (Socket socket = new Socket(host, port); 
        PrintWriter printWriter = new PrintWriter(socket.getOutputStream(), true); 
        final BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in))) { 
       while (true) { 
        System.out.println(socket.toString()); 
        c = bufferedReader.read(); 
        System.out.print((char) c); 
       } 
      } catch (IOException ex) { 
       System.out.println(ex + host + port); 
       System.exit(1); 
      } finally { 
       System.exit(1); 

      } 
     } 
    } 
} 

然而,沒有太多的輸出去:

[email protected]:~/NetBeansProjects/MudSocketClient$ 
[email protected]:~/NetBeansProjects/MudSocketClient$ java -jar dist/MudSocketClient.jar 
Socket[addr=rainmaker.wunderground.com/38.102.137.140,port=3000,localport=53550] 
^[email protected]:~/NetBeansProjects/MudSocketClient$ 
[email protected]:~/NetBeansProjects/MudSocketClient$ 

從運行Telnet CLI,連接工作正常。

+0

爲了澄清,我想在不關閉套接字的情況下打印輸出。請參閱http://stackoverflow.com/questions/6399557/java-simple-telnet-client-using-sockets – Thufir

回答

0

我發現了一些舊代碼:

public class InputOutput extends Observable { 

    private static final Logger log = Logger.getLogger(InputOutput.class.getName()); 
    private Alias alias = new Alias(); 

    public InputOutput() { 
    } 

    private void readFromConsole(final OutputStream outputStream) { 
     Thread read = new Thread() { 

      @Override 
      public void run() { 
       String line; 
       byte[] bytes; 
       Scanner scanner; 
       while (true) { 
        GameDataBean gameData = null; 
        scanner = new Scanner(System.in); 
        line = scanner.nextLine(); 
        try { 
         gameData = alias.parseUserInput(line); 
        } catch (StringIndexOutOfBoundsException e) { 
         log.fine(e.toString()); 
        } 
        if (gameData != null) { 
         setChanged(); 
         notifyObservers(gameData); 
        } else { 
         bytes = line.getBytes(); 
         try { 
          outputStream.write(bytes); 
          outputStream.write(10); 
          outputStream.flush(); 
         } catch (IOException ex) { 
          log.fine(ex.toString()); 
         } 
        } 
       } 
      } 
     }; 
     read.start(); 
    } 

    private void readInput(final InputStream inputStream) throws FileNotFoundException, IOException { 
     Thread readInput = new Thread() { 

      @Override 
      public void run() { 
       char ch = 0; 
       int intVal = 0; 
       StringBuilder sb = new StringBuilder(); 
       try { 
        while ((intVal = inputStream.read()) != -1) { 
         ch = (char) intVal; 
         printToConsole(ch); 
         //logToFile(ch); 
         sb.append(ch); 
         if (intVal == 13) { 
          setChanged(); 
          notifyObservers(sb.toString()); 
          sb = new StringBuilder(); 
         } 
        } 
       } catch (IOException ex) { 
        Logger.getLogger(InputOutput.class.getName()).log(Level.SEVERE, null, ex); 
       } 
      } 

      private void logToFile(char c) throws IOException { 
       String fname = "weather.log"; 
       File f = new File(fname); 
       f.createNewFile(); 
       try (PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fname, true)))) { 
        out.print(c); 
        out.flush(); 
       } 
      } 

      private void printToConsole(char c) { 
       System.out.print(c); 
      } 
     }; 
     readInput.start(); 
    } 

    public void readWriteParse(final InputStream inputStream, final OutputStream outputStream) throws FileNotFoundException, IOException { 
     readFromConsole(outputStream); 
     readInput(inputStream); 
    } 
} 

我認爲這是,當插座仍然是開放的,它必須是多線程的,我記得。

相關問題