您好,登錄后才能下訂單哦!
今天就跟大家聊聊有關java Socket多線程服務器源代碼是怎樣的,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
java Socket多線程需要我們不斷的學習,在學習的時候我們就會發現在服務器的配置上有不少的問題。下面我們就來看看服務器源代碼是如何編寫的,以便大家更好的使用java Socket多線程。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ChatServer {
private static final int PORT = 1234; // 端口號
private static List<Socket> list = new ArrayList<Socket>();
// 保存連接對象private ExecutorService exec;
private ServerSocket server;
public static void main(String[] args) {
new ChatServer();
}
public ChatServer() {
try {
server = new ServerSocket(PORT);
exec = Executors.newCachedThreadPool();
System.out.println(“服務器已啟動!”);
Socket client = null;
while (true) {
client = server.accept(); // 接收客戶連接
list.add(client);
exec.execute(new ChatTask(client));
}
} catch (IOException e) {
e.printStackTrace();
}
}
static class ChatTask implements Runnable {
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
private String msg;
public ChatTask(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
msg = “【” + this.socket.getInetAddress() + “】
進入聊天室!當前聊天室有【”+ list.size() + “】人”;
sendMessage();
}
public void run() {
try {
while ((msg = br.readLine()) != null) {
if (msg.trim().equals(“bye”)) {
list.remove(socket);
br.close();
pw.close();
msg = “【” + socket.getInetAddress() + “】
離開聊天室!當前聊天室有【”+ list.size() + “】人”;
socket.close();
sendMessage();
break;
} else {
msg = “【” + socket.getInetAddress() + “】說:” + msg;
sendMessage();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 群發消息給聊天室的所有人
*/
private void sendMessage() throws IOException {
System.out.println(msg);
for (Socket client : list) {
pw = new PrintWriter(client.getOutputStream(), true);
pw.println(msg);
}
}
}
}
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ChatClient {
private static final int PORT = 7891;
private static ExecutorService exec =
Executors.newCachedThreadPool();public static void main(String[] args) throws Exception {
new ChatClient();
}
public ChatClient() {
try {
Socket socket = new Socket(“221.130.165.41″, PORT);
exec.execute(new Sender(socket));
System.out.println(“【” + socket.getInetAddress() + “】
您好,歡迎來到簡易聊天室!”);BufferedReader br = new BufferedReader(new
InputStreamReader(socket.getInputStream()));String msg;
while ((msg = br.readLine()) != null) {
System.out.println(“收到—->”+msg);
}
} catch (Exception e) {
}
}
看完上述內容,你們對java Socket多線程服務器源代碼是怎樣的有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注億速云行業資訊頻道,感謝大家的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。