服务器端,建立监听(方法更新,可以同时连接多个Client)
客户端,发送消息
public class Server extends ServerSocket {
private static ArrayList<String> mUserList = new ArrayList<String>();
private static ArrayList<Thread> mThreader = new ArrayList<Thread>();
private static LinkedList<String> mMessageArray = new LinkedList<String>();
private static int mThreadCounter = 0;
private static boolean isClear = true;
public Server(int port) throws IOException {
super(port);
new Broadcast();
try {
while (true) {
Socket socket = accept();
new CreateServerThread(socket);
}
} catch (IOException e) {
} finally {
close();
}
}
// --- CreateServerThread
class CreateServerThread extends Thread {
private Socket client;
private BufferedReader in;
private PrintWriter out;
private String username;
public CreateServerThread(Socket s) throws IOException {
client = s;
in = new BufferedReader(new InputStreamReader(
client.getInputStream(), "UTF8"));
out = new PrintWriter(client.getOutputStream(), true);
sendMessage("--- Welcome ---");
sendMessage("Input your nickname:");
start();
}
public void sendMessage(String msg) {
out.println(msg);
System.out.println(msg);
}
public void run() {
try {
int flag = 0;
mThreadCounter++;
String line = in.readLine();
while (line == null || !line.equals("bye")) {
if (line == null) {
// sendMessage("null");
line = in.readLine();
continue;
}
if (line.equals("l")) {
sendMessage(listOnlineUsers());
line = in.readLine();
continue;
}
if (flag++ == 0) {
username = line;
mUserList.add(username);
sendMessage(listOnlineUsers());
mThreader.add(this);
pushMessage("[< " + username + " come on in >]");
} else {
pushMessage("<" + username + ">" + line);
}
line = in.readLine();
}
sendMessage("--- See you, bye! ---");
client.close();
} catch (IOException e) {
} finally {
try {
client.close();
} catch (IOException e) {
}
mThreadCounter--;
mThreader.remove(this);
mUserList.remove(username);
pushMessage("[< " + username + " left>]");
}
}
private String listOnlineUsers() {
String s = "-+- Online list -+-1512";
for (int i = 0; i < mUserList.size(); i++) {
s += "[" + mUserList.get(i) + "]1512";
}
s += "-+---------------------+-";
return s;
}
private void pushMessage(String msg) {
mMessageArray.addLast(msg);
isClear = false;
}
}
// --- Broadcast
class Broadcast extends Thread {
public Broadcast() {
start();
}
public void run() {
while (true) {
if (!isClear) {
String tmp = mMessageArray.getFirst();
for (int i = 0; i < mThreader.size(); i++) {
CreateServerThread client = (CreateServerThread) mThreader
.get(i);
client.sendMessage(tmp);
}
mMessageArray.removeFirst();
isClear = mMessageArray.size() > 0 ? false : true;
}
}
}
}
}
客户端,发送消息
public class Client {
private Socket socket;
private BufferedReader in;
private PrintWriter out;
public Client(String domain, int port) {
connect(domain, port);
get(in);
}
private void connect(String domain, int port) {
try {
socket = new Socket(domain, port);
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void get(final BufferedReader in) {
new Thread(new Runnable() {
public void run() {
try {
String line = in.readLine();
while (true) {
line = in.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
public void send(String msg) {
out.println(msg);
}
public void disconnect() {
try {
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}