public class Talk_clinet extends JFrame implements ActionListener {
String name, output, input;
JTextField down;// 输入框
TextArea up;// 输出框
JButton send;
String word;
Socket socket; public Talk_clinet(String name) {
this.socket = socket;
this.name = name;
setLayout(null);
setTitle(name);
setSize(380, 580);
setVisible(true);
up = new TextArea(null, 5, 5, TextArea.SCROLLBARS_NONE);
up.setBounds(10, 10, 280, 340);
up.setEditable(false);
down = new JTextField();
down.setBounds(10, 360, 180, 120);
send = new JButton("发送");
send.setBounds(200, 390, 80, 80);
add(up);
add(down);
add(send);
send.addActionListener(this);
} String sendMsg()// 发送聊天
{
return output;
} void getMsg(String a)// 获得聊天
{
up.append(name + ": " + a + "\n");
} public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("发送")) {
this.output = down.getText();
up.append(name + ": " + output + "\n");
down.setText(null);
}
}
}
public class Server {
private ServerSocket server;
private static List<Socket> list = new ArrayList<Socket>(); // 保存连接对象
private ExecutorService exec = Executors.newCachedThreadPool();// 创建线程池 public Server() throws IOException {
try {
server = new ServerSocket(8888);
Socket client = null;
System.out.println("服务器运行....");
while (true) {
client = server.accept();
list.add(client);// 添加服务端
exec.execute(new ChatTask(client));// 运行服务器端线程
}
} catch (IOException e) {
e.printStackTrace();
}
} public static class ChatTask implements Runnable {
private Socket socket;
private BufferedReader br;
private PrintWriter pw;
private String msg = null; public ChatTask(Socket socket) throws IOException {
this.socket = socket;
br = new BufferedReader(new InputStreamReader(socket
.getInputStream()));
} public void run() {
try {
while ((msg = br.readLine()) != null) {
sendMsg();
}
} catch (IOException e) {
System.out.println("发送错误");
}
} private void sendMsg() throws IOException // 向list中所有的客户端发送消息
{
System.out.println(msg);
for (Socket client : list) {
pw = new PrintWriter(client.getOutputStream(), true);
pw.println(msg);
}
}
} public static void main(String[] args) throws IOException {
new Server();
}
}
public class Chat {
Talk_clinet client = new Talk_clinet("li");
private static ExecutorService exec = Executors.newCachedThreadPool(); public Chat() {
try {
Socket socket = new Socket("121.250.216.47", 8888);
exec.execute(new clinet(socket));
System.out.println("连接到服务器...."); BufferedReader br = new BufferedReader(new InputStreamReader(socket
.getInputStream())); String msg ;
while  ((msg = br.readLine()) != null) {
System.out.println(msg);
client.getMsg(msg);
}
} catch (IOException e) {
e.printStackTrace();
}
} public class clinet implements Runnable {
private String msg;
private Socket socket;
PrintWriter pw; public clinet(Socket socket) {
this.socket = socket;
} public void run() {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
pw = new PrintWriter(socket.getOutputStream(), true);
String msn;
while (true) {
msn = client.sendMsg();// 发送消息
pw.println(msn);// 送入输出流,感觉主要是这一部分
System.out.println(msn);
}
} catch (IOException e) {
e.printStackTrace();
}
}
} public static void main(String[] args) {
new Chat();
}
}
输出端一直冒出null,还有用户之间没有通信,求怎么解决

解决方案 »

  1.   

    感觉写的好难看,主要是三个类,
    第一个是Talk_clinet,用来写的聊天界面
    第二个类是Server,是服务器类,里面有内中内ChatTask ,
    第三个类是Chat类,里面有类中类clinet,是客户端通信线程
      

  2.   

    LZ你是不是要在Chat的Button事件里面调用msn = client.sendMsg()
      

  3.   

    String sendMsg()// 发送聊天
    {
    return output;
    }
    output开始没赋值,也没见你在程序中赋值啊。
      

  4.   

    我在Talk_client中的button加的action有给output赋值的。