使用Socket,並利用多線程,實現羣聊通訊。
客戶端和服務端都應該使用多線程,服務端多線程負責多個客戶端的連接和信息轉發,客戶端的多線程負責每個客戶端的信息接受與發送。
用一個list保存每個客戶端的socket信息,服務端建立實例化對象讀取所有的socket信息並進行通訊。
class allClient { //保存socket
List list = new ArrayList();
public void addList(Socket socket) {
list.add(socket);
}
public void showList() {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public Socket getList(int n) {
return (Socket) list.get(n);
}
public int getSize() {
return list.size();
}
}
服務端
/**
* Created by hades on 2017/3/16.
* 服務端
*/
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
public class EchoServer {
private int port = 8000; //定義服務器連接端口
private ServerSocket serverSocket;
public EchoServer() throws IOException {
serverSocket = new ServerSocket(port); //新建serverSocket對象,綁定連接端口port
System.out.println("服務器啓動...");
}
public void service() throws IOException {
Socket socket = null;
try {
int count = 0;
while (true) {
socket = serverSocket.accept();
//創建一個新的線程
ServerThread serverThread = new ServerThread(socket, count);
//啓動線程
serverThread.start();
System.out.println("客戶端" + count + "已連接");
System.out.println("套接字信息:" + socket);
System.out.println("當前客戶端的IP:" + socket.getInetAddress() + ":" + socket.getPort());
count++;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null)
socket.close(); //斷開連接
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException {
new EchoServer().service();
}
}
服務端多線程類
/**
* Created by hades on 2017/3/17.
* 服務端建立多線程實現多個客戶端的連接
*/
import java.io.*;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
class allClient { //保存socket
List list = new ArrayList();
public void addList(Socket socket) {
list.add(socket);
}
public void showList() {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
public Socket getList(int n) {
return (Socket) list.get(n);
}
public int getSize() {
return list.size();
}
}
public class ServerThread extends Thread {
Socket socket = null;
int count = 0;
Socket socket1 = null;
private static allClient list = new allClient();
public ServerThread(Socket socket, int count) {
this.socket = socket;
this.count = count;
list.addList(socket);
list.showList();
}
public String echo(String msg, int count) {
return "客戶端" + count + "說:" + msg;
}
private PrintWriter getWriter(Socket socket) throws IOException { //輸出流的建立
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
private BufferedReader getReader(Socket socket) throws IOException { //輸入流的建立
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void run() {
try {
BufferedReader br = getReader(socket);
String msg = null;
while ((msg = br.readLine()) != null) {
for (int i = 0; i < list.getSize(); i++) {
//System.out.println(getName());
socket1 = list.getList(i);
PrintWriter pw = getWriter(socket1);
pw.println(echo(msg, count));
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) socket.close(); //斷開連接
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
客戶端
/**
* Created by hades on 2017/3/16.
* 客戶端
*/
import java.io.*;
import java.net.Socket;
public class EchoClient {
private String host = "localhost";
private int port = 8000;
private Socket socket;
public EchoClient() throws IOException {
socket = new Socket(host, port); //建立socket對象,用於與服務器的通信,需要綁定服務器端口。
//ClientThread clientThread=new ClientThread(socket);
//clientThread.start();
System.out.println("客戶端套接字信息:" + socket);
}
private PrintWriter getWriter(Socket socket) throws IOException { //輸出流
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
private BufferedReader getReader(Socket socket) throws IOException { //輸入流
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void talk() throws IOException {
try {
ClientThread clientThread = new ClientThread(socket);
//啓動線程
clientThread.start();
BufferedReader br = getReader(socket);
PrintWriter pw = getWriter(socket);
BufferedReader localReader = new BufferedReader(new InputStreamReader(System.in));
String msg = null;
while ((msg = localReader.readLine()) != null) {
pw.println(msg);
System.out.println(br.readLine());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String args[]) throws IOException {
new EchoClient().talk();
}
}
客戶端多線程類
/**
* Created by hades on 2017/3/17.
* 客戶端多線程,實現每個客戶端的信息接收
*/
import java.io.*;
import java.net.Socket;
public class ClientThread extends Thread {
Socket socket = null;
public ClientThread(Socket socket) {
this.socket = socket;
}
public String echo(String msg) {
return "返回的消息:" + msg;
}
private PrintWriter getWriter(Socket socket) throws IOException { //輸出流的建立
OutputStream socketOut = socket.getOutputStream();
return new PrintWriter(socketOut, true);
}
private BufferedReader getReader(Socket socket) throws IOException { //輸入流的建立
InputStream socketIn = socket.getInputStream();
return new BufferedReader(new InputStreamReader(socketIn));
}
public void run() {
try {
BufferedReader br = getReader(socket);
//PrintWriter pw = getWriter(socket);
String msg = null;
while ((msg = br.readLine()) != null) {
System.out.println(msg);
//pw.println(echo(msg));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}