java线程实现服务器与客户端互发消息

线程实现服务器与客户端互发消息

如果不用多线程来实现服务器与客户端互发消息则当一方发完消息后另一方才可以发,如果用了线程,因为多个线程可以同时执行,只要把发送和接收两个方法放入线程中让他们同时执行就可以实现服务器与客户端互发消息不受限制了

SendThread:

java 复制代码
public class SendThread implements Runnable{
    DataOutputStream dataOutputStream;
    public SendThread(Socket socket) throws IOException {
        //把Socket传进方法,把要发送的内容送入Socket
        dataOutputStream= new DataOutputStream(socket.getOutputStream());
    }

    @Override
    public void run() {
        while(true){
            Scanner scanner=new Scanner(System.in);
            String s=scanner.next();
            try {
                dataOutputStream.writeUTF(s);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

ReceiveThread:

java 复制代码
public class ReceiveThread implements Runnable{
    DataInputStream dataInputStream;
    ReceiveThread(Socket socket) throws IOException {
        //把Socket传进方法,得到Socket中的数据
        dataInputStream=new DataInputStream(socket.getInputStream());
    }
    
    @Override
    public void run(){
        Thread thread=Thread.currentThread();
        while(true) {
            try {
                String s = dataInputStream.readUTF();
                if (thread.getName().equals("客户端")){
                    System.out.println("服务器说:"+s);
                }
                if (thread.getName().equals("服务器")){
                    System.out.println("客户端说:"+s);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Server:

java 复制代码
public static void main(String[] args) {
        try {
            ServerSocket serverSocket=new ServerSocket(7788);
            System.out.println("服务器启动成功");
            Socket socket=serverSocket.accept();
            System.out.println("有客户端连接到服务器");
            SendThread sendThread=new SendThread(socket);
            ReceiveThread receiveThread=new ReceiveThread(socket);
            Thread tse=new Thread(sendThread);
            Thread tre=new Thread(receiveThread,"服务器");
            tse.start();
            tre.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Client:

java 复制代码
public static void main(String[] args) {
        try {
            Socket socket=new Socket("192.168.124.185",7788);
            SendThread sendThread=new SendThread(socket);
            ReceiveThread receiveThread=new ReceiveThread(socket);
            Thread tse=new Thread(sendThread);
            Thread tre=new Thread(receiveThread,"客户端");
            tse.start();
            tre.start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
相关推荐
步菲15 分钟前
springboot canche 无法避免Null key错误, Null key returned for cache operation
java·开发语言·spring boot
TG:@yunlaoda360 云老大23 分钟前
华为云国际站代理商GES的图引擎服务有哪些优势?
服务器·数据库·华为云
毕设源码-朱学姐24 分钟前
【开题答辩全过程】以 基于SpringBoot的中医理疗就诊系统为例,包含答辩的问题和答案
java·spring boot·后端
2201_757830874 小时前
全局异常处理器
java
小徐Chao努力5 小时前
【Langchain4j-Java AI开发】09-Agent智能体工作流
java·开发语言·人工智能
sorry#6 小时前
top简单使用
linux·运维
Coder_Boy_6 小时前
SpringAI与LangChain4j的智能应用-(理论篇3)
java·人工智能·spring boot·langchain
广东大榕树信息科技有限公司6 小时前
如何通过动环监控系统提升机房运行安全与效率?
运维·网络·物联网·国产动环监控系统·动环监控系统
Coder_Boy_6 小时前
基于SpringAI的智能平台基座开发-(六)
java·数据库·人工智能·spring·langchain·langchain4j
半壶清水6 小时前
开源免费的在线考试系统online-exam-system部署方法
运维·ubuntu·docker·开源