我还在努力学习socket服务器和客户机编程。所以我根据我收到的教程做了这个编码。我设法为多客户端交互创建了线程。但是,我无法停止客户机处理程序中的循环,该循环会一直显示我发出的欢迎消息,即使在我为它辩护之后。
如何阻止已经发出的欢迎信息的循环?
服务器端
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();
}
}
}
1条答案
按热度按时间tcomlyy61#
只需将客户机和服务器中的欢迎消息从循环中删除,如下所示。
服务器.java
客户端.java