问题如下:
客户端服务器端都启动,需要客户端发一句话服务器才启动。
不加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();
}
}