a1670131049 2019-06-04 22:37 采纳率: 50%
浏览 289
已采纳

java 简易c/s通信问题

图片说明
图片说明

问题1:程序打开原样是图1的样子,什么都不显示,只有把程序最大化,然后最小化才恢复正常,请问是什么原样

问题2:BufferedReader缓冲区和BufferedWriter缓冲区的readLine()方法是阻塞方法?如果发送端没有发送结束标记,接收端是不是就一直停在readLine方法

问题3:
Client 类用来从文本框里读取字符串传输,文本区域没有作用
Server 类用来接收Client发送的数据,界面都是一样的,只接收没有发送功能

class WindowByClient extends JFrame implements ActionListener{
    JButton send;
    JTextField text;
    JTextArea area;
    Socket client;
    BufferedWriter out;
    WindowByClient(){
        super("发送程序");
        this.setLayout(new FlowLayout());
        this.setSize(300,300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
    }
    void init(){
        send = new JButton("发送");
        text=new JTextField(10);
        area=new JTextArea(9,16);

        JScrollPane jsp=new JScrollPane(area);
        area.setEditable(false);

        this.add(send);
        this.add(text);
        this.add(jsp);

        try {
            client=new Socket("127.0.0.1",9999);
            out=new BufferedWriter(new OutputStreamWriter(client.getOutputStream()));
        } catch (IOException e) {}
        send.addActionListener(this);
    }
    void sendMethod(){
        try {
            out.write(text.getText());
            out.flush();
        } catch (IOException e) {}
    }
    public void actionPerformed(ActionEvent arg) {
        if(arg.getSource()==send){
            sendMethod();
        }
    }
}
public class Client {
    public static void main(String[] args) {
        new WindowByClient();
    }
}

class WindowByServer extends JFrame{
    JButton send;
    JTextField text;
    JTextArea area;
    ServerSocket server;
    Socket s;
    BufferedReader in;
    WindowByServer(){
        super("发送程序");
        this.setLayout(new FlowLayout());
        this.setSize(300,300);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
    }
    void init(){
        send = new JButton("发送");
        text=new JTextField(10);
        area=new JTextArea(9,16);

        JScrollPane jsp=new JScrollPane(area);
        area.setEditable(false);

        this.add(send);
        this.add(text);
        this.add(jsp);

        try {
            server=new ServerSocket();
            s=server.accept();
            in=new BufferedReader(new InputStreamReader(s.getInputStream()));
        } catch (IOException e) {}

        receiveMethod();
    }
    void receiveMethod(){
        String str=null;
        try {
            while(true){
                while((str=in.readLine())!=null){
                    if(!str.equals(null)){
                        area.append(str);
                    }
                }
            }
        } catch (IOException e) {}
    }
}
public class Server {
    public static void main(String[] args) {
        new WindowByServer();
    }
}

Exception in thread "main" java.lang.NullPointerException
    at Demo_1.WindowByServer.receiveMethod(Server.java:58)
    at Demo_1.WindowByServer.init(Server.java:52)
    at Demo_1.WindowByServer.<init>(Server.java:32)
    at Demo_1.Server.main(Server.java:69)

请问 while((str=in.readLine())!=null) 这句话为什么会报错

  • 写回答

1条回答 默认 最新

  • 亿棵松 2019-06-05 15:30
    关注

    没看你上面的代码,但是最后一句报空指针证明in就是null但是你还对他调用方法,所以

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 1月24日

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘