如何解决如何停止客户端与套接字服务器交互中的循环
我仍在尝试研究套接字服务器和客户端编程。因此,我根据收到的教程进行了编码。我设法为多客户端交互创建线程。但是,我无法停止客户端处理程序中的循环,即使我对此进行了说明,该循环仍继续显示我发出的欢迎消息。
如何停止已发出的欢迎消息的循环?
服务器端
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();
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。