Server端代碼實(shí)現(xiàn)
Server端只實(shí)現(xiàn)簡(jiǎn)單的消息轉(zhuǎn)發(fā)功能晴叨,為每一個(gè)客戶端建立一個(gè)Thread,處理該客戶端的消息轉(zhuǎn)發(fā)逞敷。
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
/**
* Created by sandy on 10/03/2017.
*/
public class MultiChatServer {
ArrayList<ClientThread> clients = new ArrayList<ClientThread>();
public static void main(String args []) {
new MultiChatServer().start();
}
public void start() {
try {
boolean iConnect = false;
ServerSocket serverSocket = new ServerSocket(20013);
iConnect = true;
while (iConnect) {
Socket socket = serverSocket.accept();
ClientThread currentClient = new ClientThread(socket);
clients.add(currentClient);
new Thread(currentClient).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
class ClientThread implements Runnable{
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
private String str;
private Boolean iConnect = false;
ClientThread(Socket socket) {
this.socket = socket;
iConnect = true;
}
public void run() {
try {
while (iConnect) {
dis = new DataInputStream(socket.getInputStream());
str = dis.readUTF();
int currentIndex = 0;
for (int i=0; i<clients.size(); i++) {
if (clients.get(i).equals(this)) {
currentIndex = i;
break;
}
}
for (int i = 0; i < clients.size(); i++) {
ClientThread clientThread = clients.get(i);
if (!clientThread.equals(this)) {
clientThread.sendMsg("Client-" + currentIndex + ": " + str);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void sendMsg(String str) {
try {
dos = new DataOutputStream(this.socket.getOutputStream());
dos.writeUTF(str);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Client端代碼實(shí)現(xiàn)
Client端需要啟動(dòng)兩個(gè)線程,一個(gè)負(fù)責(zé)發(fā)送消息,一個(gè)負(fù)責(zé)接收消息逢享,互不干擾。
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;
/**
* Created by sandy on 10/03/2017.
*/
public class MultiChatClient {
private Socket socket;
private DataInputStream dis;
private DataOutputStream dos;
private Scanner scanner;
public static void main(String args[]) {
new MultiChatClient().start();
}
public void start() {
try {
socket = new Socket("localhost", 20013);
dis = new DataInputStream(this.socket.getInputStream());
dos = new DataOutputStream(this.socket.getOutputStream());
scanner = new Scanner(System.in);
new Thread(new SendThread()).start();
new Thread(new RecThread()).start();
} catch (Exception e) {
e.printStackTrace();
}
}
class SendThread implements Runnable {
private String str;
private Boolean iConnect = false;
public void run() {
iConnect = true;
try {
while (iConnect) {
str = scanner.nextLine();
if (null == str || "".equals(str.trim()) || str.isEmpty() || str.length() == 0) {
continue;
} else {
dos.writeUTF(str);
System.out.println("I: " + str);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
class RecThread implements Runnable {
private String str;
private Boolean iConnect = false;
public void run() {
iConnect = true;
try {
while (iConnect) {
str = dis.readUTF();
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}