2015-04-05 57 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