会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132497个问题
Python 全系列/第二阶段:Python 深入与提高/游戏开发-坦克大战 23252楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:基于FastDFS+Nginx+Kinkeditor实现商品新增 23253楼

package Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 接收客户端消息的线程类
 */
class ChatReceive extends Thread{
    private Socket socket;

    public ChatReceive(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 msg=br.readLine();
                synchronized ("abc"){
                    //把读取到的数据写入公共数据区
                    ChatRoomServer.buf="["+this.socket.getInetAddress()+"]"+msg;
                    "abc".notifyAll();

                }
            }
        }catch (Exception 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();
                }
            }

        }
    }
}

/**
 * 向客户端发送消息的线程类
 */
class ChatSend extends Thread{
    private Socket socket;

    public ChatSend(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        this.SendMsg();
    }
    /**
     * 将公共数据区的数据发送到客户端
     */
    private void SendMsg(){
        PrintWriter pw=null;
        try{
            pw=new PrintWriter(this.socket.getOutputStream());
            while (true){
                synchronized("abc"){
                    //让线程处于等待状态
                    "abc".wait();
                    pw.println(ChatRoomServer.buf);
                    pw.flush();

                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (pw!=null){
                pw.close();
            }
            if (this.socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class ChatRoomServer {
    public static  String buf;
    public static void main(String[] args) {
        System.out.println(" 1.0");
        System.out.println("Listen at 8888");
        ServerSocket serverSocket=null;
        try {
            serverSocket=new ServerSocket(8888);
            while (true){
                Socket socket=serverSocket.accept();
                System.out.println("连接到 "+socket.getInetAddress());
                new ChatReceive(socket).start();
                new ChatSend(socket).start();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

老师我这里代码怎么报错,没找到问题

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 23256楼
Python 全系列/第三阶段:Python 网络与并发编程/网络通信 23257楼
JAVA 全系列/第十八阶段:亿级高并发电商项目_架构/编码(旧)/电商:使用SpringCache+Redis实现门户导航缓存 23260楼
JAVA 全系列/第五阶段:JavaWeb开发/Ajax技术详解(旧) 23262楼
Python 全系列/第一阶段:Python入门/面向对象 23263楼

springiocdemo.zip

我导包怎么出现错误!!!!!!!!!!!!!!!!!!!!!!!!!!!!

JAVA 全系列/第六阶段:项目管理与SSM框架/Spring 23264楼
JAVA 全系列/第九阶段:Spring Boot实战/Spring Boot 23265楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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