📄 clientservice.java
字号:
package com.javahomework.Client.SWT;
import java.io.*;
import java.net.Socket;
import java.util.*;
public class ClientService implements Runnable {
String ip = "127.0.0.1";
int port = 4000;
private Socket socket;
private DataOutputStream dout;
private DataInputStream din;
private Timer timer;
private ClientWindow clientWindow=null;
public ClientService(ClientWindow clientWindow) {
try {
this.clientWindow = clientWindow;
socket = new Socket(ip, port);
dout = new DataOutputStream(socket.getOutputStream());
din = new DataInputStream(socket.getInputStream());
timer = new Timer();
} catch (Exception e) {
e.printStackTrace();
}
}
public void start() {
Thread _thread = new Thread(this);
_thread.setDaemon(false);
_thread.start();
timer.schedule(new TimerTask() {
public void run() {
try{
response("LIST");
} catch(IOException e){
}
}
}, 0, 6000);
}
public void run() {
try {
while (true) {
String message = din.readUTF();
parse(message);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
socket = null;
}
}
public void response(String msg) throws IOException {
synchronized(dout){
dout.writeUTF(msg);
dout.flush();
}
}
public String readUTF() throws IOException {
return din.readUTF();
}
private void parse(String msg) {
//System.out.println(msg);
// FROM
// RESPONSELIST
// NUMBER eg.200,500
if(msg.startsWith("RESPONSELIST")){
RESPONSELISTCommand(msg);
} else if (msg.startsWith("FROM")){
FROMCommand(msg);
} else {
}
}
private void RESPONSELISTCommand(String msg){
String[] users = msg.substring(13).split(" ");
clientWindow.refreshUserList(users);
}
private void FROMCommand(String msg){
msg = msg.substring(5);
int userlen = msg.indexOf(" ");
String username = msg.substring(0,userlen);
String content = msg.substring(userlen +1);
String ret = username +"对您说:" + content;
clientWindow.showMessage(ret);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -