您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“如何使用Java多線程來實現多人聊天室”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“如何使用Java多線程來實現多人聊天室”這篇文章吧。
客戶端代碼:
package netProgram;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketAddress;
public class Client implements ScreenInputInterface{
private SocketHandler socketHandler;
public void start(){
new Thread(new ScreenInputRunIns(this)).start();
try {
socketHandler = new SocketHandler(new Socket("127.0.0.1",666),null);
new Thread(socketHandler).start();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void send(String msg) {
socketHandler.sendMsg(msg);
}
/* @Override
public void send(String msg, SocketAddress address) {
socketHandler.sendMsg(msg);
}*/
}
服務端代碼:
package netProgram;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.List;
public class Server implements ScreenInputInterface{
private List<SocketHandler> clients = new ArrayList<>();
private ServerSocket serverSocket;
public void start(){
// new Thread(new ScreenInputRunIns(this)).start();
try {
serverSocket = new ServerSocket(666);
while (true){
SocketHandler socketHandler = new SocketHandler(serverSocket.accept(),this);
clients.add(socketHandler);
System.out.println(socketHandler.getSocket().getRemoteSocketAddress()+"進入聊天系統");
new Thread(socketHandler).start();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void send(String msg) {
for (SocketHandler s:clients) {
s.sendMsg(msg);
}
}
/* @Override
public void send(String msg, SocketAddress address) {
for (SocketHandler s:clients) {
if (s.getSocket().getRemoteSocketAddress().equals(address)){
continue;
}
s.sendMsg(msg);
}
}*/
public static void main(String[] args) {
new Server().start();
}
}
屏幕輸入信息接口:
package netProgram;
import java.net.SocketAddress;
public interface ScreenInputInterface {
void send(String msg);
// void send(String msg, SocketAddress address);
}
用于各個端口輸入信息的創建的線程對象:
package netProgram;
import java.util.Scanner;
public class ScreenInputRunIns implements Runnable{
private ScreenInputInterface screenInputInterface;
private Scanner input = new Scanner(System.in);
public ScreenInputRunIns(ScreenInputInterface screenInputInterface) {
this.screenInputInterface = screenInputInterface;
}
@Override
public void run() {//屏幕實時輸入
while (true){
String msg = input.nextLine();
// screenInputInterface.send(msg,null);
screenInputInterface.send(msg);
}
}
}
套接字處理器(用于傳輸數據:實際發送信息到輸出流并實時監聽各個端口的輸入且負責顯示):
package netProgram;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
public class SocketHandler implements Runnable{
private String clientName;
private Socket socket;
private PrintWriter writer;
private Server server;
public SocketHandler(Socket socket,Server server) throws IOException {
this.socket = socket;
this.server = server;
clientName = "<"+socket.getLocalSocketAddress()+">";
writer = new PrintWriter(this.socket.getOutputStream());
}
public Socket getSocket() {
return socket;
}
public void sendMsg(String msg){
if (server!=null) writer.println(msg);
else writer.println(clientName+":"+msg);
writer.flush();
}
@Override
public void run() {
while(true){//監聽輸入流的輸入
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String receiveMsg = null;
System.out.println();
if ((receiveMsg = reader.readLine())!=null&&!(receiveMsg.equals(""))){
if (server!=null){
// server.send(receiveMsg,socket.getRemoteSocketAddress());
server.send(receiveMsg);
}else {
System.out.println(receiveMsg);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
負責啟動的各個端口的客戶端代碼:
package netProgram;
public class ClientLinked {
public static void main(String[] args) {
new Client().start();
}
}
package netProgram;
public class ClientLinked1 {
public static void main(String[] args) {
new Client().start();
}
}
package netProgram;
public class ClientLinked2 {
public static void main(String[] args) {
new Client().start();
}
}
服務端啟動代碼在服務端類的main方法中。
以上是“如何使用Java多線程來實現多人聊天室”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。