日期:2014-05-20  浏览次数:20841 次

各位大神请教一个建立简单聊天室问题,跪求指教!!!!
服务器程序

import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.awt.*;
import java.util.concurrent.*;



public class Server {
JTextField servertxtf;
static JTextArea servertxta;
static BufferedReader read;
static PrintWriter write;
static ServerSocket server=null;
static Socket socket=null;

public Server(){
servertxtf=new JTextField(100);
servertxtf.addActionListener(actionlistener);
servertxta=new JTextArea();
JFrame frame=new JFrame("Server");
frame.add(servertxtf,BorderLayout.NORTH);
frame.add(servertxta,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

ActionListener actionlistener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(socket==null){
servertxta.append("System:还未与客户建立连接");
}else{
servertxta.append("Server:"+servertxtf.getText()+"\n");
System.out.println(servertxtf.getText());
write.print(servertxtf.getText());
write.flush();
System.out.println("fuwuqi");
servertxtf.setText("");
}
}
};


static class serverthread implements Runnable{
int clientNo;
public serverthread(int clientNo){
this.clientNo=clientNo;
}

public void run(){
while(true){
try{
servertxta.append(clientNo+":"+read.readLine().toString()+"\n");
}catch(IOException io){
System.out.println("接收信息出错");
break;
}
}
}
}

public static void main(String[]args){
new Server();
int clientNo=1;
ExecutorService exec=Executors.newCachedThreadPool();
try{
server=new ServerSocket(8080);
while(true){
socket=server.accept();
read=new BufferedReader(new InputStreamReader(socket.getInputStream()));
write=new PrintWriter(socket.getOutputStream(),true);
exec.execute(new serverthread(clientNo));
}
}catch(IOException io){
System.out.println("建立服务其出错");
}finally{
try{
read.close();
write.close();
socket.close();
server.close();
}catch(IOException io){
System.out.println("未正常关闭");
}
}
}

}



客户端程序

import java.awt.*;
import javax.swing.*;
import java.io.*;
import java.net.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class Client{
JTextField clienttxtf;
static JTextArea clienttxta;
static BufferedReader read;
static PrintWriter write;
static Socket socket;

public Client(){
clienttxtf=new JTextField(100);
clienttxtf.addActionListener(actionlistener);
clienttxta=new JTextArea();
JFrame frame=new JFrame("Client");
frame.add(clienttxtf,BorderLayout.NORTH);
frame.add(clienttxta,BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}


ActionListener actionlistener=new ActionListener(){
public void actionPerformed(ActionEvent e){
if(socket==null){
clienttxta.append("System:还未与服务器建立连接");
}else{
clienttxta.append("client:"+clienttxtf.getText()+"\n");
write.print(clienttxtf.getText());
write.flush();
clienttxtf.setText("");
}
}
}; 

static class clientthread implements Runnable{
public void run(){
try{
read=new BufferedReader(new InputStreamReader(socket.getInputStream()));