当前位置: 首页 > news >正文

TUP实现一对一聊天

package TCP;
 
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.Scanner;
 
/**
 * 发送消息线程
 */
class Send extends Thread{
    private Socket socket;
    public Send(Socket socket){
        this.socket  =socket;
    }
    @Override
    public void run() {
        this.sendMsy();
    }
    /**
     * 发送消息
     */
    private void sendMsy(){
        Scanner scanner =null;
        PrintWriter pw =null;
        try{
            scanner =new Scanner(System.in);
            pw =new PrintWriter(this.socket.getOutputStream());
            while(true){
                String str =scanner.nextLine();
                pw.println(str);
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (scanner!=null){
                scanner.close();
            }
            if (pw!=null){
                pw.close();
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
/**
 * 接收消息的线程
 */
class receive extends Thread{
    private Socket socket=null;
    public receive(Socket socket){
        this.socket =socket;
    }
 
    @Override
    public void run() {
        this.receiveMsg();
    }
    /**
     * 用于接收对方消息
     */
    private void receiveMsg(){
        BufferedReader br =null;
        try{
            br =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while(true){
                String mr = br.readLine();
                System.out.println("A说:"+mr);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
        }
    }
}
 
public class ChatSocketServer {
    public static void main(String[] args) {
        ServerSocket serverSocket =null;
        try{
            serverSocket =new ServerSocket(8888);
            System.out.println("服务端已启动等待连接");
            Socket socket = serverSocket.accept();
            System.out.println("连接成功!");
            new Send(socket).start();
            new receive(socket).start();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

以上是服务器端,下面是客户端

package TCP;
 
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;
 
public class ChatSocketClient {
    public static void main(String[] args) {
 
        try {
            Socket socket =new Socket("127.0.0.1",8888);
            System.out.println("连接成功!");
            new ClientSend(socket).start();
            new Clientreive(socket).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
/**
 * 用于发送消息线程类
 */
class ClientSend extends Thread{
    @Override
    public void run() {
        this.sendMsy();
    }
    private Socket socket;
    public ClientSend(Socket socket){
        this.socket  =socket;
    }
    /**
     * 发送消息
     */
    private void sendMsy(){
        Scanner scanner =null;
        PrintWriter pw =null;
        try{
            scanner =new Scanner(System.in);
            pw =new PrintWriter(this.socket.getOutputStream());
            while(true){
                String str =scanner.nextLine();
                pw.println(str);
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (scanner!=null){
                scanner.close();
            }
            if (pw!=null){
                pw.close();
            }
            if (socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
 
/**
 *用于接收消息线程类
 */
class Clientreive extends Thread{
    private Socket socket=null;
    public Clientreive(Socket socket){
        this.socket =socket;
    }
 
    @Override
    public void run() {
        this.receiveMsg();
    }
    /**
     * 用于接收对方消息
     */
    private void receiveMsg(){
        BufferedReader br =null;
        try{
            br =new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
            while(true){
                String mr = br.readLine();
                System.out.println("B说:"+mr);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
 
        }
    }
}
 

http://www.lryc.cn/news/266578.html

相关文章:

  • Kafka设计原理详解
  • 光耦继电器
  • 【C++练级之路】【Lv.5】动态内存管理(都2023年了,不会有人还不知道new吧?)
  • 2016年第五届数学建模国际赛小美赛A题臭氧消耗预测解题全过程文档及程序
  • springMVC-与spring整合
  • 【二叉树】【单调双向队列】LeetCode239:滑动窗口最大值
  • 如何使用树莓派Bookworm系统中配置网络的新方法NetworkManager
  • 恶意软件分析沙箱在网络安全策略中处于什么位置?
  • ARM学习(24)Can的高阶认识和错误处理
  • 网络通信--深入理解网络和TCP / IP协议
  • IPC之九:使用UNIX Domain Socket进行进程间通信的实例
  • 学习在UE中通过Omniverse实现对USD文件的Live-Sync(实时同步编辑)
  • 实现打印一个数字金字塔。例如:输入5,图形如下图所示
  • hive sql常用函数
  • Spark系列之:使用spark合并hive数据库多个分区的数据到一个分区中
  • 《重构-改善既有代
  • vue3(七)-基础入门之事件总线与动态组件
  • 【计算机网络】网络层——IP协议
  • 《钢结构设计标准》中抗震性能化设计的概念
  • 【算法】【动规】回文串系列问题
  • 4-Docker命令之docker logs
  • svelte基础语法学习
  • Node.js教程-mysql模块
  • 网络通信协议
  • Spark集群部署与架构
  • DshanMCU-R128s2 SDK 架构与目录结构
  • 【5G PHY】NR参考信号功率和小区总传输功率的计算
  • k8s学习 — 各知识点快捷入口
  • 【Python】Python 批量转换PDF到Excel
  • Python并行计算和分布式任务全面指南