java - client - 服务器来自客户端的多条消息

时间:2017-02-01 13:00:52

标签: java server client communication

我正在学习服务器 - 客户端通信,我做了一个简单的通信器,它工作,但我只能向服务器发送一条消息。我不知道如何发送和接收来自客户端的更多消息。我尝试了很多选择,但都没有。

这是我的代码: 客户:         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();                                                          
   }                                                                         
}

2 个答案:

答案 0 :(得分:0)

您需要为代码添加主循环,并为退出添加特殊命令

例如

// infinite loop
while (true) {

   // ..receive or send commands here..

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

}

同时添加异常处理(try-catch-finally)或者你的应用程序将非常脆弱

答案 1 :(得分: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();*/                                                                  
    }                                                                         
}
相关问题