求大佬解答,不知怎样修改代码,怎样让socket在确认对方输入"BYE"时,回复对方BYE后,正常关闭?
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
public class SocketThreadTest2 {
public static void main(String[] args) {
if (args.length>0){
server();
}else {
client();
}
}
public static void server() {
try {
System.out.println("server启动");
ServerSocket ss = new ServerSocket(6789);
while(true) {
Socket clients = ss.accept();
//需要传参
new ServerThread2(clients).start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void client() {
try {
System.out.println("client启动");
Socket s = new Socket("localhost",6789);
OutputStream os = s.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
InputStream is = s.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
new WriteThread1(br, dos).start();
new ReadThread1(dis).start();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//线程:接受每个客户端,并且建立一个线程
class ServerThread2 extends Thread{
Socket s;
public ServerThread2(Socket s) {
this.s=s;
}
@Override
public void run() {
try {
InputStream is = s.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
DataInputStream dis = new DataInputStream(bis);
OutputStream os = s.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(os);
DataOutputStream dos = new DataOutputStream(bos);
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
new ReadThread1(dis).start();
new WriteThread1(br, dos).start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class ReadThread1 extends Thread{
DataInputStream dis;
public ReadThread1(DataInputStream dis) {
this.dis = dis;
}
@Override
public void run() {
String str;
try {
while (true){
str = dis.readUTF();
System.out.println("对方说:"+str);
if (str.equals("BYE")){
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(dis!=null) {
dis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
//
class WriteThread1 extends Thread{
BufferedReader br;
DataOutputStream dos;
public WriteThread1(BufferedReader br, DataOutputStream dos) {
this.br = br;
this.dos = dos;
}
@Override
public void run() {
String str;
try {
while (true){
str=br.readLine();
dos.writeUTF(str);
dos.flush();
if(str.equals("BYE")) {
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(dos!=null) {
dos.close();
}
if(br!=null) {
br.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}