2015-04-05 72 views
0

我測試出套接字,所以你將不得不原諒混亂的代碼,但我有一些問題只是讓套接字服務器拾取發送的消息套接字,或者可能發送消息。無論如何,我無法弄清楚什麼是錯的。套接字通過緩衝讀取器讀取,不接收線上

我的代碼得到了來自附加套接字點但不是停止收聽點的消息的偵聽,並且沒有錯誤,因此它正在從輸入流中讀取數據。但是,當我寫入內容並將其寫入OutputStreamWriter中的套接字時,套接字的緩衝讀取器(由套接字服務器接受)不會接受它。

任何幫助都會很棒。謝謝。

public class Main 
{ 
    private static final String _address = "127.0.0.1"; 
    private static final int _port = 8080; 

    private static ServerSocket  _listener; 

    private static Socket _socket; 
    private static OutputStreamWriter _socketWriter; 

    public static void main(String[] args) throws Exception { 
     System.out.println("Type something in to send it into the big wide world!\n\nType quit to exit."); 
     BufferedReader inputReader = new BufferedReader(new InputStreamReader(System.in)); 
     Main._startListening(); 
     Main._connectToListener(); 

     while(true) { 
      String msg = inputReader.readLine(); 

      if("".equals(msg)) { 
       System.out.println("You can't send nothing!"); 
       continue; 
      } 
      else if("quit".equals(msg)) { 
       Main._listener.close(); 
       break; 
      } 

      Main.sendMessage(msg); 
     } 
    } 

    public static void sendMessage(String message) throws Exception { 
     System.out.println("Sending " + message); 
     Main._socketWriter.write(message + System.getProperty("line.separator")); 
    } 

    private static void _connectToListener() throws Exception { 
     Main._socket = new Socket(Main._address, Main._port); 
     Main._socketWriter = new OutputStreamWriter(Main._socket.getOutputStream()); 
     System.out.println("Connected to listener"); 
    } 

    private static void _startListening() throws Exception { 
     Main._listener = new ServerSocket(Main._port); 

     new Thread(new Runnable() 
     { 
      @Override 
      public void run() { 
       System.out.println("Listening on " + Main._address + ":" + Main._port); 

       while (!Main._listener.isClosed()) { 
        try { 
         final Socket socket = Main._listener.accept(); 

         System.out.println("Attached new socket " + socket.getInetAddress().toString()); 

         new Thread(new Runnable() 
         { 
          @Override 
          public void run() { 
           System.out.println("Listening for messages from attached socket"); 

           try { 
            BufferedReader socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 

            while(!socket.isClosed()) { 
             String receivedLine = socketReader.readLine(); 
             System.out.println("Received " + receivedLine); 
            } 

            System.out.println("Stopped listening to socket " + socket.getInetAddress().toString()); 
           } 
           catch(Exception e) { 
            System.out.println(e.toString()); 
           } 
          } 
         }).start(); 
        } 
        catch(Exception e) { 
         System.out.println(e.toString()); 
        } 
       } 
      } 
     }).start(); 
    } 
} 

回答

0

我修復了它。我需要行終止符,但是我也需要在寫入OutputStreamWriter之後刷新它。

1

經典。您正在閱讀線路,但您並未發送線路。您需要在郵件中追加一個行結束符。

+0

這並沒有解決它,我已經更新了op中的代碼 – user1763295 2015-04-06 06:53:04