qq_24824529 2015-07-09 07:55 采纳率: 10%
浏览 1746
已采纳

关于java聊天室,求助

问题如下:
客户端服务器端都启动,需要客户端发一句话服务器才启动。
不加while只能接受客户端的一句话,加while服务器收不到任何东西。
*********客户端************************
import java.awt.*;
import java.net.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;

public class Desktop extends JFrame implements ActionListener {
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
private Socket cs = null;
private JPanel p = new JPanel();
private JTextArea output = new JTextArea();
private JTextField input = new JTextField();
private DataOutputStream dos;
//private DataInputStream dis;
private JButton b1 = new JButton("进入");
private JButton b2 = new JButton("退出");
private JLabel fuwuqis = new JLabel("服务器:");
private JTextField fuwuqi = new JTextField("127.0.0.1");
private JLabel duankous = new JLabel("端口:");
private JTextField duankou = new JTextField("8888");
private JLabel nichengs = new JLabel("昵称:");
private JTextField nicheng = new JTextField("游客1");
private JButton b3 = new JButton("发送");
private String talking;
//UI界面(不需要再修改)
Desktop() {
p.setLayout(new BorderLayout());
JPanel p1 = new JPanel();
JPanel p2 = new JPanel();
p1.add(b1);
b1.addActionListener(this);
p1.add(b2);
b2.addActionListener(this);
p1.add(fuwuqis);
p1.add(fuwuqi);
p1.add(duankous);
p1.add(duankou);
p1.add(nichengs);
p1.add(nicheng);
p2.setLayout(new BorderLayout());
p2.add(input, BorderLayout.CENTER);
p2.add(b3, BorderLayout.EAST);
b3.addActionListener(this);
p.add(p1, BorderLayout.NORTH);
p.add(p2, BorderLayout.SOUTH);
p.add(output, BorderLayout.CENTER);
b2.setEnabled(false);
b1.setEnabled(true);
b3.setEnabled(false);
input.setEditable(false);
output.setEditable(false);
fuwuqi.setEditable(true);
duankou.setEditable(true);
nicheng.setEditable(true);
this.setTitle("聊天室for客户端");
this.add(p);
this.setSize(500, 300);
this.setVisible(true);
}
//连接函数,流与Socket所在
public void connect() {
try {
cs = new Socket(fuwuqi.getText(), Integer.parseInt(duankou.getText()));
System.out.print("加入连接");
dos = new DataOutputStream(cs.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

public void DataOutputStream() {
    try {
        if(talking == null) {
            dos.writeUTF("");
        } else {
            dos.writeUTF(talking);
        }
        dos.flush();
        //dos.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

public void actionPerformed(ActionEvent e) {
    String S = e.getActionCommand();
    if ("发送".equalsIgnoreCase(S)) {
        talking = input.getText().trim();
        String snicheng = nicheng.getText().trim();
        output.append("\n\r" + snicheng + " : " + talking);
        input.setText("");
        DataOutputStream();
    } else if ("进入".equalsIgnoreCase(S)) {
        connect();
        output.append("进入成功\n 服务器:" + fuwuqi.getText() + "\t端口:" + duankou.getText() + "\t昵称:" + nicheng.getText() + "\n");
        b1.setEnabled(false);
        b2.setEnabled(true);
        b3.setEnabled(true);
        input.setEditable(true);
        fuwuqi.setEditable(false);
        duankou.setEditable(false);
        nicheng.setEditable(false);
    } else if ("退出".equalsIgnoreCase(S)) {
        output.append("退出成功");
        b1.setEnabled(true);
        b2.setEnabled(false);
        b3.setEnabled(false);
        input.setEditable(false);
        fuwuqi.setEditable(true);
        duankou.setEditable(true);
        nicheng.setEditable(true);
    }
}

public static void main(String args[]) {
    new Desktop();
}

}
***********服务器********************************
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;

public class Servicer extends JFrame implements ActionListener {
private class WindowCloser extends WindowAdapter {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}

//private ServerSocket ss;
//private Socket s;
private JPanel p = new JPanel();
private JTextArea output = new JTextArea();
private JTextField input = new JTextField();
//private DataOutputStream dos;
private DataInputStream dis;
private JButton b1 = new JButton("启动");
private JButton b2 = new JButton("退出");
private JButton b3 = new JButton("发送");
//private String talking;
private boolean started = false;
//UI界面(不需要再修改)
Servicer() {
    p.setLayout(new BorderLayout());
    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    p1.add(b1);
    b1.addActionListener(this);
    p1.add(b2);
    b2.addActionListener(this);
    p2.setLayout(new BorderLayout());
    p2.add(input, BorderLayout.CENTER);
    p2.add(b3, BorderLayout.EAST);
    p.add(p1, BorderLayout.NORTH);
    p.add(p2, BorderLayout.SOUTH);
    p.add(output, BorderLayout.CENTER);
    b2.setEnabled(false);
    b1.setEnabled(true);
    b3.setEnabled(false);
    input.setEditable(false);
    output.setEditable(false);
    this.setTitle("聊天室for服务端");
    this.add(p);
    this.setSize(500, 300);
    this.setVisible(true);
}

public void start() {
    try {
        ServerSocket ss = new ServerSocket(8888);               
        started = true;
        //while(started) {
            boolean bConnected = false;
            Socket s = ss.accept();
            output.append("新加入,已连接");
            b1.setEnabled(false);
            b2.setEnabled(true);
            b3.setEnabled(true);
            input.setEditable(true);
            bConnected = true;
            dis = new DataInputStream(s.getInputStream());
        //  while(bConnected){ 
                String talking = dis.readUTF();
                output.append("\n\r" + "snicheng" + " : " + talking);
        //  }
            dis.close();
        //}
    } catch (IOException e1) {
        e1.printStackTrace();
    }
}

public void actionPerformed(ActionEvent e) {
    String S = e.getActionCommand();
    String takeing = input.getText().toString();
    if ("启动".equalsIgnoreCase(S)) {
        start();
    } else if ("退出".equalsIgnoreCase(S)) {
        output.append("退出成功");
    } else if ("发送".equalsIgnoreCase(S)) {
        output.append("\n\r" + "主机" + " : " + takeing);
        input.setText("");
    }
}

public static void main(String args[]) {
    Servicer d = new Servicer();
}

}

  • 写回答

1条回答 默认 最新

  • danielinbiti 2015-07-09 08:26
    关注
     被阻塞了,ui更新了,但没刷新,bConnected定义成全局变量
     dis = new DataInputStream(s.getInputStream());
                new Thread(){
                    @Override
                    public void run(){
                        while(bConnected){ 
                            String talking="";
                            try {
                                talking = dis.readUTF();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            output.append("\n\r" + "snicheng" + " : " + talking);
                        }
                        try {
                            dis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 android报错 brut.common.BrutException: could not exec (exit code = 1)
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?