我之前没说清楚 怪我...
我想让客户端给服务器发送CLOSE,然后服务器关闭和这个客户端的连接。 但是只要一输入CLOSE 客户端那边都运行不了 更不要说发送给服务器 感觉是一个很简单的问题 但是一直调不通
只要有一个客户端退出 剩下的程序整个崩 有没有办法加几条语句让剩下的客户端和服务器正常运行
服务器
@RestController
@RequestMapping("/Socket")
@Api("服务器")
@Service
public class SocketService {
@Autowired
SetDao setDao;
// SendData send=new SendData();
static Scanner input = new Scanner(System.in);
static List<Socket> list = new ArrayList();
static Integer count =0;
public static final Integer PORT=8888;
@RequestMapping(value = "/photo", method = RequestMethod.POST)
public String photocontrol(@RequestBody Integer e) throws IOException{
SendData send=new SendData();
if(e%2==0)
send.msg="OPENPHOTO";
else if(e%2==1)
send.msg="CLOSEPHOTO";
return "SUCCESS_PHOTO";
}
@RequestMapping(value = "/create", method = RequestMethod.GET)
public void createServerSocket(){
// {
try {
//创建服务器
ServerSocket server = new ServerSocket(PORT);
System.out.println("***服务器即将启动***");
while (true) {
Socket s = server.accept();
list.add(s);
System.out.println("当前客户端的数量为:" + (count + 1));
//发信息(有多个客户端)
if (count == 0) {
new SendData().start();
}
//收信息
new GetData(new DataInputStream(list.get(count).getInputStream())).start();
count++;
}
} catch (IOException e) {
e.printStackTrace();
}
// }
}
//给客户端发送信息
public static class SendData extends Thread {
private DataOutputStream dOutput;
public static String msg;
@Override
public void run() {
while (true) {
try {
// String msg =input.next();
if (msg!=null) {
for (Socket socket : list) {
dOutput = new DataOutputStream(socket.getOutputStream());
dOutput.writeUTF(msg);
}
msg=null;
}
} catch (IOException e) {
// break;
e.printStackTrace();
}
}
}
}
//读取客户端发来的信息
public static class GetData extends Thread {
private DataInputStream dInput;
public GetData(DataInputStream _dInput) {
dInput=_dInput;
}
@Override
public void run() {
while (true) {
try {
String msg = dInput.readUTF();
if (msg != null) {
System.out.println("客户端:"+ msg);
// if(msg.equals("CLOSE")) {
// dInput.close();
// list.remove(count);
// count--;
// System.out.println("当前客户端的数量为:"+count);
// }
// if(socket.getKeepAlive()==false){
// ((Reader)in).close();
// temp="客户端"+socket.getPort()+"退出";
// ss.appendMessage(temp);
// socket.close();
// this.stop();
}
} catch (IOException e) {
try {
dInput.close();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}
}
}
}
客户端
@RestController
@RequestMapping("/TestSocket")
@Api("客户端")
@Service
public class TestSocketService {
static Scanner input = new Scanner(System.in);
public static final Integer PORT=8888;
@RequestMapping(value = "/create", method = RequestMethod.GET)
public void createSocket() throws IOException {
Socket socket = new Socket("localhost",PORT);
System.out.println("已连接服务器");
new GetData(new DataInputStream(socket.getInputStream())).start();
new SendData(new DataOutputStream(socket.getOutputStream())).start();
}
//读取服务器发来的信息
public static class GetData extends Thread {
private DataInputStream dInput;
public GetData(DataInputStream _dInput) {
dInput = _dInput;
}
@Override
public void run() {
while (true) {
try {
String msg = dInput.readUTF();
if (msg != null) {
System.out.println("服务器:" + msg);
}
} catch (IOException e) {
try {
dInput.close();
} catch (IOException e1) {
e1.printStackTrace();
}
break;
}
}
}
}
//给服务器发送信息
public static class SendData extends Thread {
private DataOutputStream dOutput;
public SendData(DataOutputStream _dOutput) {
dOutput = _dOutput;
}
@Override
public void run() {
while (true) {
try {
System.out.println("给服务器发消息:");
String msg =input.next();
if (msg != null) {
dOutput.writeUTF(msg);
}
} catch (IOException e) {
break;
// e.printStackTrace();
}
}
}
}
}