会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132482个问题
人工智能/第四阶段:人工智能基础-高等数学知识强化/线性代数高级 21961楼
Python 全系列/第二阶段:Python 深入与提高/模块 21963楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/反射技术(旧) 21964楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 21965楼
JAVA 全系列/第十二阶段:消息中间件(异步消息传递)/ActiveMQ 21966楼
人工智能/第四阶段:人工智能基础-高等数学知识强化/线性代数高级 21967楼

老师,一对多聊天服务中,我的客户端不是每次都能返回消息。

下面的是服务端的代码

package com.bjsxt.ls.网络编程.TCP.一对多聊天服务器;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;


//创建服务端接收消息的线程
class ChatReceive extends Thread{
    private Socket ss;
    public ChatReceive(Socket n){
        this.ss=n;
    }

    @Override
    public void run() {
        this.receive1();
    }

    //创建接收消息的线程
    public void receive1(){
        BufferedReader br =null;
        try {
            br = new BufferedReader(new InputStreamReader(this.ss.getInputStream()));
            while (true){
                String jieshou = br.readLine();
                synchronized ("同步锁") {       //要让发送消息和接收消息的线程处于同步状态,需要在两个线程上加上同一个名字的对象锁
                    ChatServer.BUF = jieshou; //将接收到的消息保存到缓存区
                    "同步锁".notify();        //当消息存到缓存区之后,唤醒发送消息线程,可以发送消息了
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (this.ss != null){
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

//创建服务端发送消息的线程类
class ChatSend extends Thread{
    private Socket ss;
    public  ChatSend(Socket n){
        this.ss=n;
    }

    @Override
    public void run() {
        this.send1();
    }

    //创建发送消息的方法
    public void send1(){
        PrintWriter pw = null ;
        try {
            pw = new PrintWriter(this.ss.getOutputStream());
            while (true) {
                synchronized ("同步锁") { //要让发送消息和接收消息的线程处于同步状态,需要在两个线程上加上同一个名字的对象锁
                    "同步锁".wait();  //发送消息的线程应先处于等待状态
                    pw.println(ChatServer.BUF); //将消息从缓存区取出,因为BUF是static的,直接用类名就能调用
                    pw.flush();
                }
            }
        }  catch (Exception e) {
            e.printStackTrace();
        }finally {
            if (pw != null){
                pw.close();
            }
            if (this.ss != null){
                try {
                    this.ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

//主线程用于启动服务端,客户端可以使用之前的“点对点优化”中的GoodP2P
public class ChatServer {
    //定义一个字符串类型的缓存区
    public static String BUF;
    public static void main(String[] args) {
        System.out.println("正在启动服务器.....");
        System.out.println("<HappyDay>聊天室启动");
        ServerSocket serverSocket = null;
        try {
            serverSocket =new ServerSocket(332);
            while (true){
                Socket sk =serverSocket.accept(); //将启动服务端放在循环中,因为每次发送或接收消息都需要启动服务端
                System.out.println("已经连接到IP地址为["+ InetAddress.getLocalHost() +"]的主机。");
                new ChatReceive(sk).start(); //启动线程
                new ChatSend(sk).start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

这个是用的优化的点对点聊天作为客户端的代码

package com.bjsxt.ls.网络编程.TCP.点对点优化;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

//创建发送消息线程
class send1 extends Thread{
    private Socket socket =null;
    private Scanner scanner =null;
    public send1(Socket s1,Scanner s2){
        this.socket =s1;
        this.scanner=s2;
    }

    //创建发送消息的方法
    public void sengmessage(){
        PrintWriter pw =null;
        try{
            pw =new PrintWriter(socket.getOutputStream());
            scanner = new Scanner(System.in);
            while (true){
                String say =scanner.nextLine();
                pw.println(say);
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (pw != null){
                pw.close();
            }
            if (scanner != null){
                scanner.close();
            }
            if (socket != null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public void run() {
        this.sengmessage();
    }
}

//创建接收消息的线程
class receive extends Thread{
    private Socket socket =null;
    public receive(Socket x){
        this.socket = x;
    }
    public void receivemessage(){
        BufferedReader br= null;
        try {
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true){
                String rs = br.readLine();
                System.out.println("收到的消息是:"+rs);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket!= null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    @Override
    public void run() {
        this.receivemessage();
    }
}
public class GoodP2P {
    public static void main(String[] args) {
        Scanner scanner = null;
        ServerSocket serverSocket = null;
        Socket socket = null;
        scanner =new Scanner(System.in);
        System.out.println("请输入:server,<port> 或者:" + "<ip>,<port>");
        String str = scanner.nextLine();
        String arr[] =str.split(",");//以逗号为分割点存入数组

        //判断当输入的server时,启动服务端
        if ("server".equals(arr[0])){
            System.out.println("TCP点对点通信,正在启动服务端,连接端口号["+arr[1]+"]");
            try {
                serverSocket =new ServerSocket(Integer.parseInt(arr[1]));//将字符串强转为整型
                socket = serverSocket.accept();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("连接成功!");
        }
        //当输入的不是server时,即进入客户端
        else {
            try {
                socket = new Socket(arr[0],Integer.parseInt(arr[1]));
                System.out.println("连接成功!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //启动发送消息的线程
        new send1(socket,scanner).start();
        new receive(socket).start();
        if (serverSocket != null){
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

下面的运行的截图:

服务端测试


客户端1



客户端2




客户端3




JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 21968楼
Python 全系列/第十二阶段:Python_Django3框架/Django初级 21969楼
JAVA 全系列/第三阶段:数据库编程/JDBC技术(旧) 21970楼
JAVA 全系列/第六阶段:项目管理与SSM框架/SpringMVC 21972楼

egoShop.zip

老师,我的项目现在可以正常运行,但是以import store 就会报错,视频里有些东西,vue3.0好像是更新了,和视频里不一样,老师麻烦您帮我看看,怎么能把数据存储到全局

WEB前端全系列/第二十阶段:Vue2企业级项目(旧)/Ego商城高级Vue实战项目 21973楼


image.png

egoshop.zip

老师,我点击编辑然后提交的时候服务器报这个错误,我是按照视频中的一样写的

WEB前端全系列/第二十阶段:Vue2企业级项目(旧)/Ego商城高级Vue实战项目 21974楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637