Server端代码实现
Server端只实现简单的消息转发功能,为每一个客户端建立一个Thread,处理该客户端的消息转发。
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端代码实现
Client端需要启动两个线程,一个负责发送消息,一个负责接收消息,互不干扰。
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();
}
}
}
}
网友评论