2017-02-01 62 views
0

我正在學習有關服務器服務器的多個消息 - 客戶溝通,我做了一個簡單的溝通,它的工作原理,但我只能發送一個消息給服務器。我不知道如何發送和接收來自客戶端的更多消息。我嘗試了很多選擇,但都沒有成功。爪哇 - 客戶 - 從客戶

這是我的代碼: 客戶端: import java.io. ; import java.net。;

public class Klient 
    { 
     public static final int PORT=50007; 
     public static final String HOST = "127.0.0.1"; 

    public static void main(String[] args) throws IOException        
    {                     

     Socket sock;                  
     sock=new Socket(HOST,PORT);              
     System.out.println("communication works: "+sock);        


     BufferedReader klaw;                
     klaw=new BufferedReader(new InputStreamReader(System.in));      
     PrintWriter outp;                 
     outp=new PrintWriter(sock.getOutputStream());          


     System.out.print("<Sending:> ");             
     String str=klaw.readLine();              
     outp.println(str);                
     outp.flush();                  


     klaw.close();                  
     outp.close();                  
     sock.close();                  
    }                     
} 

和服務器:

import java.io.*; 
import java.net.*; 

public class Serwer 
{ 
    public static final int PORT=50007; 

    public static void main(String args[]) throws IOException     
    {                   

     ServerSocket serv;              
     serv=new ServerSocket(PORT);           


     System.out.println("listen for: "+serv);        
     Socket sock;               
     sock=serv.accept();              
     System.out.println("communication: "+sock);       


     BufferedReader inp;              
     inp=new BufferedReader(new InputStreamReader(sock.getInputStream())); 

     String str;                
     str=inp.readLine();              
     System.out.println("<it comes:> " + str);        


     inp.close();               
     sock.close();               
     serv.close();               
    }                   
} 

回答

0

您需要添加一個主循環爲您的代碼和特殊命令退出

例如:

// infinite loop 
while (true) { 

    // ..receive or send commands here.. 

    if (command.equals("exit") { 
    // exit from loop 
    } 

} 

同時添加異常處理(的try-catch-finally程序),或者你的應用程序將是非常脆弱的

0

TCP套接字流中的發送數據。 TCP不支持在「消息」或「塊」中發送數據。你在代碼中做什麼是發送和接收流。

要發送「消息」使用TCP,應用協議必須在TCP的頂部限定。這個協議應該有能力發送「消息」。 (如果你不明白這部分你應該閱讀有關的協議層,7層OSI模型,和5層TCP/IP套件)

甲方式做到這一點是定義一個消息終止字符。該流將如下所示:

<message><termination-character><message><termination-character> 

終止符是來自消息字符集的字符或其外部的字符。在後一種情況下,消息中終止字符的任何出現都應該用換碼序列替換。

假設我們使用「\ n」作爲終止字符,我們假設「\ n」是不是在消息的字符集。您的客戶端應該是這樣的:

import java.io.*; 
import java.net.*; 

    public class Klient 
    { 
    public static final int PORT=50007; 
    public static final String HOST = "127.0.0.1"; 
    public static final char TERMINATIONCHAR = '\n'; 

    public static void main(String[] args) throws IOException        
    {                     

     Socket sock;                  
     sock=new Socket(HOST,PORT);              
     System.out.println("communication works: "+sock);        


     BufferedReader klaw;                
     klaw=new BufferedReader(new InputStreamReader(System.in));      
     PrintWriter outp;                 
     outp=new PrintWriter(sock.getOutputStream());          

     //define the loop 
     while(true){ 
      System.out.print("<Sending:> ");             
      String str=klaw.readLine(); 
      outp.print(str+TERMINATIONCHAR);                
      outp.flush(); 
     } 

     /* uncomment if the loop can be exited 
     klaw.close();                  
     outp.close();                  
     sock.close();*/ 
    }                     
} 

,你的服務器應該是這樣的:

import java.io.*; 
import java.net.*; 

public class Server 
{ 
    public static final int PORT=50007; 
    public static final char TERMINATIONCHAR = '\n'; 

    public static void main(String args[]) throws IOException     
    {                   

     ServerSocket serv;              
     serv=new ServerSocket(PORT);           


     System.out.println("listen for: "+serv);        
     Socket sock;               
     sock=serv.accept();              
     System.out.println("communication: "+sock);       


     BufferedReader inp;              
     inp=new BufferedReader(new InputStreamReader(sock.getInputStream())); 

     //define the loop 
     while(true){ 
      String str;                
      str=inp.readLine();            
      System.out.println("<it comes:> " + str); 
     } 

     /* uncomment if the loop can be exited 
     inp.close();               
     sock.close();               
     serv.close();*/                 
    }                   
}