如何停止客户端-套接字服务器交互中的循环

How to stop looping in client -socket server interaction

我仍在努力研究套接字服务器和客户端编程。所以我根据收到的教程进行了编码。我设法为多客户端交互创建线程。但是,我无法停止客户端处理程序中的循环,即使在我提出理由后,它仍然显示我发出的欢迎消息。

如何停止循环播放已制作好的欢迎信息?

服务器端

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

        //server listen on port 4999
        ServerSocket ss = new ServerSocket(4999);

        //running loop to get client request
        while(true){

            Socket s = null;

            try
            {
                //socket object receive incoming client requests
                s = ss.accept();

                System.out.println("New Client is connected :" + s);

                //Obtaining input and out streams
                DataInputStream dis = new DataInputStream(s.getInputStream());
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());

                System.out.println("Assigning new thread for this client");

                //create new thread object
                Thread t = new ClientHandler(s, dis, dos);

                //Invoking start() method
                t.start();
            }
            catch (Exception e){
                s.close();
                e.printStackTrace();
            }
        }
    }
}

    class ClientHandler extends Thread{
    DateFormat fordate= new SimpleDateFormat("yyyy/MM/dd");
    DateFormat fortime = new SimpleDateFormat("yyyy/MM/dd");
    final DataInputStream dis;
    final DataOutputStream dos;
    final Socket s;

    //Constructor
    public ClientHandler(Socket s, DataInputStream dis,DataOutputStream dos){
        this.s = s;
        this.dis = dis;
        this.dos = dos;

    }

    @Override
    public void run() {
       String received;
       String toreturn;
       while(true){
           try{

               //ask user his position
               dos.writeUTF("WELCOME TO CREWCUTS SOCKET SERVER. \n" +
                            "Select either [Customer | BarberShop] \n" +
                                    "Type Exit to terminate connection");

               //get client's answer
               received = dis.readUTF();

               if(received.equals("Exit")){
                   System.out.println("Client " + this.s + "send exit");
                   System.out.println("Closing connection");
                   this.s.close();
                   System.out.println("Connection closed");
                   break;
               }

               //creating Date object
               Date date = new Date();

               //write on output stream based on the answer from client
               switch (received){

                   case "Customer" :
                       toreturn = fordate.format(date);
                       dos.writeUTF(toreturn + "\n Welcome to Customer service of CREWCUTS Socket Server");
                       break;

                   case "BarberShop" :
                       toreturn = fordate.format(date);
                       dos.writeUTF(toreturn +"\n Welcome to BarberShop service of CREWCUTS Socket Server");
                       break;

                   default:
                       dos.writeUTF("Invalid input");
                       break;
               }

           }
           catch (IOException e) {
               e.printStackTrace();
           }
       }

       try
       {
           //closing resource
           this.dis.close();
           this.dos.close();
       }
       catch(IOException e){
           e.printStackTrace();
       }
    }
}

客户端

public class client {
    public static void main(String[] args) throws IOException{
        try{
            Scanner scn = new Scanner(System.in);

            //establish connection to server port 4999 in localhost
            Socket s = new Socket("localhost" ,4999);

            //obtaining input and out streams
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());

            //loop for exchange of information between client and client handler
            while(true)
            {
                System.out.println(dis.readUTF());
                String tosend = scn.nextLine();
                dos.writeUTF(tosend);

                //if client send Exit, connection closed and break from loop
                if(tosend.equals("Exit")){
                    System.out.println("Closing connection : " + s);
                    s.close();
                    System.out.println("Connection closed");
                    break;
                }

                //printing info as requested by client
                String received = dis.readUTF();
                System.out.println(received);
            }

            //closing resources
            scn.close();
            dis.close();
            dos.close();

        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}

只需删除客户端和服务器中的欢迎消息,如下所示。

server.java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.net.*;
 public class server {
    public static void main(String[] args) throws IOException{

        //server listen on port 4999
        ServerSocket ss = new ServerSocket(4999);

        //running loop to get client request
        while(true){

            Socket s = null;

            try
            {
                //socket object receive incoming client requests
                s = ss.accept();

                System.out.println("New Client is connected :" + s);

                //Obtaining input and out streams
                DataInputStream dis = new DataInputStream(s.getInputStream());
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());

                System.out.println("Assigning new thread for this client");

                //create new thread object
                Thread t = new ClientHandler(s, dis, dos);

                //Invoking start() method
                t.start();
            }
            catch (Exception e){
                s.close();
                e.printStackTrace();
            }
        }
    }
}

    class ClientHandler extends Thread{
    DateFormat fordate= new SimpleDateFormat("yyyy/MM/dd");
    DateFormat fortime = new SimpleDateFormat("yyyy/MM/dd");
    final DataInputStream dis;
    final DataOutputStream dos;
    final Socket s;

    //Constructor
    public ClientHandler(Socket s, DataInputStream dis,DataOutputStream dos){
        this.s = s;
        this.dis = dis;
        this.dos = dos;

    }

    @Override
    public void run() {
       String received;
       String toreturn;
                      //ask user his position
                      try{
               dos.writeUTF("WELCOME TO CREWCUTS SOCKET SERVER. \n" +
                            "Select either [Customer | BarberShop] \n" +
                                    "Type Exit to terminate connection");
                      }
                      catch (IOException e) {
               e.printStackTrace();
           }
       while(true){
           try{

               //get client's answer
               received = dis.readUTF();

               if(received.equals("Exit")){
                   System.out.println("Client " + this.s + "send exit");
                   System.out.println("Closing connection");
                   this.s.close();
                   System.out.println("Connection closed");
                   break;
               }

               //creating Date object
               Date date = new Date();

               //write on output stream based on the answer from client
               switch (received){

                   case "Customer" :
                       toreturn = fordate.format(date);
                       dos.writeUTF(toreturn + "\n Welcome to Customer service of CREWCUTS Socket Server");
                       break;

                   case "BarberShop" :
                       toreturn = fordate.format(date);
                       dos.writeUTF(toreturn +"\n Welcome to BarberShop service of CREWCUTS Socket Server");
                       break;

                   default:
                       dos.writeUTF("Invalid input");
                       break;
               }

           }
           catch (IOException e) {
               e.printStackTrace();
           }
       }

       try
       {
           //closing resource
           this.dis.close();
           this.dos.close();
       }
       catch(IOException e){
           e.printStackTrace();
       }
    }
}

client.java

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.util.Scanner;
import java.net.*;
 public class client {
    public static void main(String[] args) throws IOException{
        try{
            Scanner scn = new Scanner(System.in);

            //establish connection to server port 4999 in localhost
            Socket s = new Socket("localhost" ,4999);

            //obtaining input and out streams
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());

            try{
            System.out.println(dis.readUTF());
            }
            catch (Exception e){
            e.printStackTrace();
        }
            //loop for exchange of information between client and client handler
            while(true)
            {
                String tosend = scn.nextLine();
                dos.writeUTF(tosend);

                //if client send Exit, connection closed and break from loop
                if(tosend.equals("Exit")){
                    System.out.println("Closing connection : " + s);
                    s.close();
                    System.out.println("Connection closed");
                    break;
                }

                //printing info as requested by client
                String received = dis.readUTF();
                System.out.println(received);
            }

            //closing resources
            scn.close();
            dis.close();
            dos.close();

        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}