2014-02-10 33 views
0

你好,我是用這個方法來讀取信息:Java的閱讀從插座消息沒有發送完整的郵件

public String readMessage() { 
    int read = -1; 
    byte[] buffer = new byte[5*1024]; 
    byte[] redData; 
    try { 
     while ((read = this.session.getInputStream().read(buffer)) > -1) { 
      redData = new byte[read]; 
      System.arraycopy(buffer, 0, redData, 0, read); 
      return new String(redData,"UTF-8"); 

     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

當我寫的東西,如「你好你今天怎麼」

響應(具體格式,包括這些新線):

[/127.0.0.1:54930]:  
[/127.0.0.1:54930]: are 

[/127.0.0.1:54930]: you 

[/127.0.0.1:54930]: today? 

那我怎麼看聊天信息,首先我檢查請求該數據包,如果包類型爲0,然後我得到packethandler實例,並通過客戶對象聊天處理數據包,這將在這裏閱讀信息,像這樣:

public void startClientService() throws IOException { 
    while(true) { 
     int packetType = this.in.read(); 
     packets.getPacket(packetType); 
    } 
} 
public void getPacket(int packetType) { 
    switch (packetType) { 
    case 0: 
     chat.processPacket(this.client); 
     break; 
    } 
} 

而且聊天包:

@Override 
public void processPacket(Session c) { 
    String clientMessage = c.readMessage(); 
    System.out.println("[" + c.getStream().getRemoteSocketAddress() + "]: " + clientMessage.toString()); 
} 

然後發生打印信息。

爲什麼它會以新行打印部分消息?甚至沒有完整的信息。

這是我的客戶:

public static void main(String[] args) throws UnknownHostException, IOException { 
    Socket socket = new Socket("127.0.0.1", 43594); 
    Scanner r = new Scanner(System.in); 
    PrintWriter out = new PrintWriter(socket.getOutputStream()); 
    String input; 
    while(true) { 
     input = r.next(); 
     if (input != null) { 
      sendMessage(input, out); 
     } 
    } 

} 

public static void sendMessage(String message, PrintWriter out) { 
    out.write(0); 
    out.flush(); 
    out.write(message + "\n"); 
    out.flush(); 
} 

感謝。

會議:

public class Session extends Thread implements Runnable { 

    private Socket session; 
    private Client client; 
    private PrintWriter out; 
    private BufferedReader in; 
    private PacketHandler packets; 

    public Session(Socket session) { 
     this.session = session; 
     this.client = new Client(this); 
     try { 
      this.setStream(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     this.packets = new PacketHandler(this); 
     System.out.println("[New session created]: " + session.getRemoteSocketAddress()); 
    } 

    public void run() { 
     try { 
      this.startClientService(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

    public Socket getStream() { 
     return this.session; 
    } 

    public void setStream() throws IOException { 
     this.out = new PrintWriter(this.session.getOutputStream()); 
     this.in = new BufferedReader(new InputStreamReader(this.session.getInputStream())); 
    } 

    public Client getClient() { 
     return this.client; 
    } 

    public String readMessage() { 
     int read = -1; 
     byte[] buffer = new byte[5*1024]; 
     byte[] redData; 
     try { 
      while ((read = this.session.getInputStream().read(buffer)) > -1) { 
       redData = new byte[read]; 
       System.arraycopy(buffer, 0, redData, 0, read); 
       return new String(redData,"UTF-8"); 

      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    public void startClientService() throws IOException { 
     while(true) { 
      int packetType = this.in.read(); 
      packets.getPacket(packetType); 
     } 
    } 

    public void destruct() throws IOException { 
     this.session.close(); 
     System.out.println("Session killed"); 
    } 

} 
+0

什麼是你的會話對象? – UVM

+0

@UVM爲會話添加了類。 – Artemkller545

回答

1

看起來像你的,只要你從流得到一些數據返回。

while ((read = this.session.getInputStream().read(buffer)) > -1) { 
      redData = new byte[read]; 
      System.arraycopy(buffer, 0, redData, 0, read); 
      return new String(redData,"UTF-8"); 

     } 

完全讀取數據,並進行字符串對象了出來,並返回它

BufferedReader br = new BufferedReader(new InputStreamReader(this.session.getInputStream())); 
String msg = br.readLine(); 
br.close(); 
    return msg; 

嘗試這種方式。這將給你一個緩衝區的整個數據,並可以返回作爲一行字符串。無需循環

+0

現在它甚至沒有收到消息(不會打印任何東西) – Artemkller545

+0

我讀了你的編輯,現在它逐行發送它。 – Artemkller545

+0

不錯!祝你好運.... – UVM

0

從一次調用讀取返回的數據量與 數據發送時如何劃分沒有關係。一次發送可導致任意次數的讀取,並且可將多次發送組合成一次讀取。