java实现一个简单TCPSocket聊天室功能分享
作者:小小小丑 时间:2022-06-11 20:04:36
本文实例为大家分享了java实现TCPSocket聊天室功能的相关代码,供大家参考,具体内容如下
1.TCPserver.java
import java.net.*;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;
public class TCPserver{
private static final int SERVERPORT = 8888;
private ServerSocket MyServer = null;
private List<Socket> Clients = new ArrayList<Socket>();
private ExecutorService mExecutorService;
public TCPserver(){
try{
MyServer = new ServerSocket(SERVERPORT);
mExecutorService = Executors.newCachedThreadPool();
System.out.println("start:");
Socket MySocket = null;
while(true){
MySocket = MyServer.accept();
Clients.add(MySocket);
mExecutorService.execute(new ThreadServer(MySocket));
}
}catch(Exception e){
e.printStackTrace();
System.exit(0);
}
}
class ThreadServer implements Runnable{
private Socket msocket=null;
private BufferedReader in= null;
private PrintWriter out = null;
private String mStrMSG = null;
public ThreadServer(Socket socket) {
try{
this.msocket=socket;
in = new BufferedReader(new InputStreamReader(msocket.getInputStream(), "GB2312"));
mStrMSG = "user:" + msocket.getInetAddress() + " come total:" + Clients.size();
SendMassage();
}catch(IOException e){
System.out.println("erorr");
System.exit(0);
}
}
private void SendMassage(){
try{
System.out.println(mStrMSG);
for(Socket MySocket:Clients)
{
out = new PrintWriter(new OutputStreamWriter(MySocket.getOutputStream(),"GB2312"),true);
out.println(mStrMSG);
}
}catch(IOException e){
System.out.println("erorr");
System.exit(0);
}
}
public void run(){
try{
while((mStrMSG = in.readLine())!=null){
if(mStrMSG.trim().equals("exit")){
Clients.remove(msocket);
in.close();
out.close();
mStrMSG = "user:" + msocket.getInetAddress() + " exit tatal:" + Clients.size(); ;
msocket.close();
SendMassage();
break;
}
else{
mStrMSG = msocket.getInetAddress()+":" + mStrMSG;
SendMassage();
}
}
}catch(IOException e){
System.out.println("erorr");
System.exit(0);
}
}
}
public static void main(String[] args){
new TCPserver();
}
}
2.TCPclient.java
import java.net.*;
import java.io.*;
import java.util.concurrent.*;
public class TCPclient {
private static final int PORT = 8888;
private Socket Client = null;
private BufferedReader sin = null;
private ExecutorService mExecutorService;
public TCPclient(){
try{
Client = new Socket("120.27.126.174",PORT);
sin = new BufferedReader(new InputStreamReader(Client.getInputStream(),"GB2312"));
mExecutorService = Executors.newCachedThreadPool();
mExecutorService.execute(new ThreadClient(Client));
String msg = null;
while((msg = sin.readLine()) != null) {
System.out.println(msg);
}
}catch(IOException e){
System.out.println(e.getMessage());
}
}
class ThreadClient extends Thread{
private Socket mSocket = null;
private String msg = null;
BufferedReader in = null;
PrintWriter out = null;
public ThreadClient(Socket socket){
this.mSocket = socket;
}
public void run(){
try{
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(mSocket.getOutputStream(), "GB2312"), true);
while(true){
msg = in.readLine();
out.println(msg);
if(msg.trim().equals("exit")){
in.close();
out.close();
mExecutorService.shutdownNow();
break;
}
}
}catch(IOException e){
System.out.println("see you");
System.exit(0);
}
}
}
public static void main(String[] args){
new TCPclient();
}
}
标签:java,TCPSocket,聊天室
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot详细讲解视图整合引擎thymeleaf
2023-12-04 16:31:37
![](https://img.aspxhome.com/file/2023/2/77382_0s.png)
java判断字符串是否为数字的方法小结
2023-11-25 05:54:52
一个依赖搞定 Spring Boot 接口防盗刷的流程分析
2023-06-01 16:46:41
![](https://img.aspxhome.com/file/2023/4/93344_0s.png)
java利用Future实现多线程执行与结果聚合实例代码
2023-09-24 11:07:18
简单了解Spring beanfactory循环依赖命名重复属性
2023-10-27 19:39:14
![](https://img.aspxhome.com/file/2023/3/75013_0s.png)
Java中的main函数的详细介绍
2021-10-03 16:09:16
JavaWeb如何实现禁用浏览器缓存
2021-09-13 01:27:45
Java实现简单的迷宫游戏详解
2022-11-22 05:00:52
![](https://img.aspxhome.com/file/2023/0/63300_0s.png)
Java实现医院管理系统
2023-11-22 18:36:07
![](https://img.aspxhome.com/file/2023/2/74992_0s.png)
springboot2.5.6集成RabbitMq实现Topic主题模式(推荐)
2021-10-03 22:01:22
![](https://img.aspxhome.com/file/2023/4/83494_0s.png)
java项目构建Gradle的使用教程
2023-06-07 19:17:41
![](https://img.aspxhome.com/file/2023/9/84649_0s.png)
C# SendInput 模拟鼠标操作的实现方法
2022-03-15 00:07:25
Spring Boot 详细分析Conditional自动化配置注解
2021-11-25 21:56:14
![](https://img.aspxhome.com/file/2023/9/61799_0s.png)
Java常用内置注解用法分析
2023-11-24 04:53:46
c#接口使用示例分享
2022-02-22 17:38:46
redis与ssm整合方法(mybatis二级缓存)
2022-02-27 22:14:27
![](https://img.aspxhome.com/file/2023/3/62463_0s.png)
Java中定时器Timer致命缺点案例详解
2022-08-27 08:38:45
![](https://img.aspxhome.com/file/2023/0/66200_0s.png)
SpringBoot Aop 详解和多种使用场景解析
2022-10-02 12:03:47
![](https://img.aspxhome.com/file/2023/4/73124_0s.png)
C#如何通过匿名类直接使用访问JSON数据详解
2023-07-20 12:31:53
![](https://img.aspxhome.com/file/2023/7/67057_0s.jpg)
Java8 CompletableFuture runAsync学习总结submit() execute()等
2023-05-25 04:13:43
![](https://img.aspxhome.com/file/2023/3/71943_0s.png)