Java使用TCP套接字实现多人聊天功能详解
作者:hu_beliefs 时间:2023-12-16 15:42:40
本文实例讲述了Java使用TCP套接字实现多人聊天功能。分享给大家供大家参考,具体如下:
废话不多说,直接开搞:
先创建一个服务端:
package interWebWriterEven2;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
/**
* 服务端
* @author
*
*/
public class ServiceT {
static ArrayList<Socket> list = new ArrayList<Socket>();
public static void main(String[] args) {
ServerSocket server;
try {
server = new ServerSocket(9999);
while(true){
Socket socket = server.accept();
System.out.println(socket.getInetAddress().getHostName()+"连接成功。IP:"+socket.getInetAddress().getHostAddress());
list.add(socket);
ServiceThreadT t =new ServiceThreadT(socket);
t.start();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
接着是服务端的线程:
package interWebWriterEven2;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
/**
* 服务端线程
* @author
*
*/
public class ServiceThreadT extends Thread{
private Socket socket;
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public ServiceThreadT(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
try {
@SuppressWarnings("resource")
BufferedWriter wFile = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D://messagesT.txt",true)));
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String strOut = "";
String strIn = "";
while(true){
strIn = socket.getInetAddress().getHostName()+":"+br.readLine();
strOut = strIn;
wFile.write(strIn);
wFile.newLine();
wFile.flush();
for (Socket soc : ServiceT.list) {
if(soc != socket){
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(soc.getOutputStream()));
bw.write(strOut);
System.out.println(strOut);
bw.newLine();
bw.flush();
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
然后是客户端(应用端):
package interWebWriterEven2;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
/**
* 客户端
* @author
*
*/
public class ClientT {
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1",9999);
ClientThreadT t = new ClientThreadT(socket);
t.start();
BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String strIn = "";
while(true){
strIn = br.readLine();
System.out.println(strIn);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
最后是客户端的线程:
package interWebWriterEven2;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.Scanner;
/**
* 客户端的线程
* @author
*
*/
public class ClientThreadT extends Thread{
private Socket socket;
public Socket getSocket() {
return socket;
}
public void setSocket(Socket socket) {
this.socket = socket;
}
public ClientThreadT(Socket socket) {
super();
this.socket = socket;
}
@Override
public void run() {
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
try {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
while(true){
System.out.println();
bw.write(sc.nextLine().trim());
bw.newLine();
bw.flush();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
先运行服务端哦,否则将会报空指针异常的。
希望本文所述对大家java程序设计有所帮助。
来源:https://blog.csdn.net/hu_belif/article/details/81675911
标签:Java,TCP套接字,多人聊天
0
投稿
猜你喜欢
关于java String中intern的深入讲解
2023-01-24 18:18:36
SpringBoot中使用Filter和Interceptor的示例代码
2022-06-28 17:20:04
android 之Spinner下拉菜单实现级联
2023-01-24 04:15:45
springboot之Jpa通用接口及公共方法使用示例
2023-02-17 16:18:52
SpringCloud:feign对象传参和普通传参及遇到的坑解决
2023-02-17 11:18:58
springboot 防止重复请求防止重复点击的操作
2021-09-19 16:03:00
@Autowired注解在抽象类中失效的原因及解决
2021-07-15 06:12:04
利用Android实现光影流动特效的方法详解
2023-09-03 01:22:31
基于C#对用户密码使用MD5加密与解密
2022-11-24 23:05:23
Spring boot配置文件加解密详解
2023-11-12 00:17:29
springBoot集成Elasticsearch 报错 Health check failed的解决
2022-12-07 05:18:16
浅谈JDK8中的Duration Period和ChronoUnit
2023-01-07 20:10:01
C#远程发送和接收数据流生成图片的方法
2021-08-31 00:30:10
SpringBoot整合mybatis-generator-maven-plugin的方法
2022-08-14 18:34:22
spring boot实现过滤器和拦截器demo
2023-08-24 07:15:01
C#编程自学之数据类型和变量一
2023-07-30 02:45:49
Ireport的安装与使用教程
2021-08-08 00:16:43
android当前apn的状态以及获取方法
2022-06-20 14:52:25
C#后台创建控件并获取值的方法
2022-10-20 08:39:46
C#实现两接口中同名方法实例分析
2022-09-08 13:04:42