英文:
How to stop looping in client -socket server interaction
问题
以下是您提供的代码的翻译:
服务器端:
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(4999);
        while (true) {
            Socket s = null;
            try {
                s = ss.accept();
                System.out.println("新客户端已连接:" + s);
                DataInputStream dis = new DataInputStream(s.getInputStream());
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                System.out.println("为此客户端分配新线程");
                Thread t = new ClientHandler(s, dis, dos);
                t.start();
            } catch (Exception e) {
                if (s != null) {
                    s.close();
                }
                e.printStackTrace();
            }
        }
    }
}
class ClientHandler extends Thread {
    DateFormat fordate = new SimpleDateFormat("yyyy/MM/dd");
    DateFormat fortime = new SimpleDateFormat("HH:mm:ss");
    final DataInputStream dis;
    final DataOutputStream dos;
    final Socket s;
    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 {
                dos.writeUTF("欢迎访问 CREWCUTS SOCKET 服务器。\n" +
                            "请选择 [顾客 | 理发店]\n" +
                            "输入 Exit 以终止连接");
                received = dis.readUTF();
                if (received.equals("Exit")) {
                    System.out.println("客户端 " + this.s + " 发送了退出请求");
                    System.out.println("关闭连接");
                    this.s.close();
                    System.out.println("连接已关闭");
                    break;
                }
                Date date = new Date();
                switch (received) {
                    case "顾客":
                        toreturn = fordate.format(date);
                        dos.writeUTF(toreturn + "\n 欢迎使用 CREWCUTS Socket 服务器的顾客服务");
                        break;
                    case "理发店":
                        toreturn = fordate.format(date);
                        dos.writeUTF(toreturn + "\n 欢迎使用 CREWCUTS Socket 服务器的理发店服务");
                        break;
                    default:
                        dos.writeUTF("无效的输入");
                        break;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            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);
            Socket s = new Socket("localhost", 4999);
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            while (true) {
                System.out.println(dis.readUTF());
                String tosend = scn.nextLine();
                dos.writeUTF(tosend);
                if (tosend.equals("Exit")) {
                    System.out.println("关闭连接:" + s);
                    s.close();
                    System.out.println("连接已关闭");
                    break;
                }
                String received = dis.readUTF();
                System.out.println(received);
            }
            scn.close();
            dis.close();
            dos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
英文:
i'm still trying to study on socket server and client programming. So i did this coding based on the tutorial i received. I managed to create thread for multi client interaction. However, i could not stop the loop in the in the client handler that keep displaying welcoming message that i made even after i made case for it.
How to stop the looping of welcoming message that has been made?
Server side
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();
       }
    }
}
Client Side
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();
        }
    }
}
答案1
得分: 1
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{
        ServerSocket ss = new ServerSocket(4999);
        while(true){
            Socket s = null;
            try {
                s = ss.accept();
                System.out.println("New Client is connected: " + s);
                DataInputStream dis = new DataInputStream(s.getInputStream());
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                System.out.println("Assigning new thread for this client");
                Thread t = new ClientHandler(s, dis, dos);
                t.start();
            }
            catch (Exception e){
                s.close();
                e.printStackTrace();
            }
        }
    }
}
class ClientHandler extends Thread{
    DateFormat fordate = new SimpleDateFormat("yyyy/MM/dd");
    final DataInputStream dis;
    final DataOutputStream dos;
    final Socket s;
    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;
        try{
            dos.writeUTF("Select either [Customer | BarberShop]\nType Exit to terminate connection");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        while(true){
            try{
                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;
                }
                Date date = new Date();
                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 {
            this.dis.close();
            this.dos.close();
        }
        catch(IOException e){
            e.printStackTrace();
        }
    }
}
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);
            Socket s = new Socket("localhost" ,4999);
            DataInputStream dis = new DataInputStream(s.getInputStream());
            DataOutputStream dos = new DataOutputStream(s.getOutputStream());
            try{
                System.out.println(dis.readUTF());
            }
            catch (Exception e){
                e.printStackTrace();
            }
            while(true)
            {
                String tosend = scn.nextLine();
                dos.writeUTF(tosend);
                if(tosend.equals("Exit")){
                    System.out.println("Closing connection : " + s);
                    s.close();
                    System.out.println("Connection closed");
                    break;
                }
                String received = dis.readUTF();
                System.out.println(received);
            }
            scn.close();
            dis.close();
            dos.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
英文:
Just remove the welcome message out of loop both in client and server as below.
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();
        }
    }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论