【问题标题】:java - client - server multiple messages from clientjava - 客户端 - 服务器来自客户端的多条消息
【发布时间】:2017-06-18 05:46:55
【问题描述】:

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

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

【问题讨论】:

    标签: java server client communication


    【解决方案1】:

    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();*/                                                                  
        }                                                                         
    }
    

    【讨论】:

      【解决方案2】:

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

      例如:

      // infinite loop
      while (true) {
      
         // ..receive or send commands here..
      
         if (command.equals("exit") {
           // exit from loop
         }
      
      }
      

      还要添加异常处理(try-catch-finally),否则您的应用将非常脆弱

      【讨论】:

        猜你喜欢
        • 2016-04-09
        • 2021-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-28
        相关资源
        最近更新 更多