会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题
会帮你学得更全面
截止目前,同学们一共提了 58296个问题,问题会在24小时内得到回答
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 1楼

image.png

老师好像是我唤醒线程这一块出了问题运行连接都成功了就是聊天系统这一块似乎消息都在缓冲区我唤醒线程那一块出了问题 但是我校对了几遍也没发现那里出错了

package com.bzsxt;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
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 ("砸瓦鲁多"){
                    //把读取到的数据写入公共数据区
                    ChatRoomServer.buf="["+this.socket.getInetAddress()+"] "+msg;
                    //唤醒发送消息的线程对象
                    "砸瓦鲁多".notifyAll();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (this.socket != null){
                try {
                    this.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 ("砸瓦鲁多"){
                    //让发送消息的线程处于等待状态
                    "砸瓦鲁多".wait();
                    //将公共数据区中的消息发送给客户端
                    pw.print(ChatRoomServer.buf);
                    pw.flush();
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (pw != null){
                pw.close();
            }
            if (this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class ChatRoomServer {
    //定义公共数据区
    public static String buf;
    public static void main(String[] args) {
        System.out.println("Chat Server Version 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();
                }
            }
        }
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程 2楼

image.png

老师明明显示连接成功了怎么还会报错呀这是代码

package com.bzsxt;

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 Send1 extends Thread{
    private Socket socket;
    private Scanner scanner;
    public Send1(Socket socket,Scanner scanner) {
        this.socket = socket;
        this.scanner = scanner;
    }
    @Override
    public void run() {
        this.sendMsg();
    }
    /**
     * 发送消息
     */
    private void sendMsg(){
        PrintWriter pw = null;
        try{
            //创建向对方输出消息的对象流
            pw = new PrintWriter(this.socket.getOutputStream());
            while(true){
                String msg = scanner.nextLine();
                pw.println(msg);
                pw.flush();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally{
            if (scanner != null){
                scanner.close();
            }
            if (pw != null){
                pw.close();
            }
            if (this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

/**
 * 接收消息的线程
 */
class Receive1 extends Thread{
    private Socket socket;
    public Receive1(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();
                System.out.println("他说:"+msg);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if (br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (this.socket != null){
                try {
                    this.socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
public class GoodTCP {
    public static void main(String[] args) {
        Scanner scanner = null;
        ServerSocket serverSocket = null;
        Socket socket = null;
        try{
            scanner = new Scanner(System.in);
            System.out.println("请输入:server,<port>或:<ip>,<port>");
            String str = scanner.nextLine();
            String[]arr = str.split(",");
            if ("server".equals(arr[0])){
                //启动服务端
                System.out.println("TCP Server Listen at"+arr[1]+" .....");
                serverSocket = new ServerSocket(Integer.parseInt(arr[1]));
                System.out.println("连接成功!");
            }else{
                //启动客户端
                socket = new Socket(arr[0],Integer.parseInt(arr[1]));
                System.out.println("连接成功!");
            }
            //启动发送消息的线程
            new Send1(socket,scanner).start();
            //启动接收消息的线程
            new Receive1(socket).start();
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if (serverSocket != null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程 5楼
JAVA 全系列/第六阶段:SSM 框架和项目开发/Mybatis 6楼
JAVA 全系列/第三阶段:数据库编程和设计/MySQL数据库的使用 7楼
JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符 8楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程 11楼
大数据全系列/第四阶段:Hadoop 分布式文件系统HDFS/Hadoop 分布式文件系统HDFS 13楼
大数据全系列/第四阶段:Hadoop 分布式文件系统HDFS/Hadoop 分布式文件系统HDFS 14楼

image.png老师,请帮忙看看哪里错了

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 send1 extends Thread{
    private Socket socket;
    private Scanner scanner;
    public send1(Socket socket,Scanner scanner){

        this.socket=socket;
        this.scanner=scanner;
    }

    @Override
    public void run() {
        this.sendMsg();
    }
    /**
     * 创建发现消息方法
     */
    public void sendMsg(){
        PrintWriter pw=null;
        try{
            //创建发送信息流对象
            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(this.socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }


}

/**
 * 创建接受消息线程
 */
class receive1 extends Thread{
    private Socket socket;
    public receive1 (Socket socket){
        this.socket=socket;
    }

    @Override
    public void run() {
        this.receiveMeg();
    }
    /**
     * 用于接受对方消息的方法
     */
    public void receiveMeg(){
        BufferedReader br=null;
        try{
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            while (true){
                String str= br.readLine();
                System.out.println("对方说:"+str);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}







public class GoodTCP {
    public static void main(String[] args) {
        Scanner scanner=null;
        ServerSocket serverSocket=null;
        Socket socket=null;
        try{
            scanner=new Scanner(System.in);
            System.out.println("请输入:server,<port>或者<ip>,<port>");
            String str=scanner.nextLine();
            String[] arr=str.split(",");//用逗号分割内容
            if("server".equals(arr[0])){
                //启动服务器
                System.out.println("TCP Server Listen at"+arr[1]+"....");
                serverSocket=new ServerSocket(Integer.parseInt(arr[1]));
                socket=serverSocket.accept();
                System.out.println("链接成功");
            }else{
                //启动客户端
                socket=new Socket(arr[0],Integer.parseInt(arr[1]));
                System.out.println("链接成功");

            }
            //启动发送消息线程
            new send1(socket,scanner).start();
            new receive1(socket).start();

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(serverSocket!=null){
                try {
                    serverSocket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

    }
}


JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程 15楼
关闭
扫码观看
在线咨询
我要报名

©2014-2021 百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备14032124号-2    京ICP备18060230号-2