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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器