java Socket多线程服务器源代码是怎样的
今天就跟大家聊聊有关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多线程服务器源代码是怎样的有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注编程网行业资讯频道,感谢大家的支持。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341