2017-02-01 9 views
0

私はサーバーとクライアントの通信について学んでいます。私は単純なコミュニケータを作っていますが、動作しますが、サーバーには1つのメッセージしか送信できません。私は、クライアントからもっと多くのメッセージを送受信する可能性をどのようにするかを知らない。私はたくさんのオプションを試しましたが、何もしません。java - client - serverクライアントからの複数のメッセージ

ここに私のコードです: クライアント: インポート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();                  
    }                     
} 

とサーバー:

// infinite loop 
while (true) { 

    // ..receive or send commands here.. 

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

} 

はまた、例外処理を追加します。あなたは例の

を終了するために、あなたのコードや特殊コマンドのメインループを追加する必要が

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

(try-catch-finally)するか、アプリが非常に壊れやすい

0

TCPソケットはストリーム内のデータを送信します。 TCPは "メッセージ"または "ブロック"でデータを送信することをサポートしていません。あなたがコード内でやっていることは、ストリームの送受信です。

TCPを使用して「メッセージ」を送信するには、アプリケーションプロトコルをTCPの上に定義する必要があります。このプロトコルは "メッセージ"を送信する能力を持つべきです。 (この部分が理解できない場合は、プロトコル層、OSIモデル7層、TCP/IP群5層について読むべきです)

これを行う方法は、メッセージ終了文字を定義することです。ストリームは次のようになります。

<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();*/                 
    }                   
} 
関連する問題