繁体   English   中英

Java-客户端-服务器来自客户端的多个消息

[英]java - client - server multiple messages from client

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

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

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

例如:

// infinite loop
while (true) {

   // ..receive or send commands here..

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

}

同时添加异常处理(最终尝试捕获),否则您的应用程序将非常脆弱

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM