在服务器端的聊天窗口起来之后,既开始等待客户端的连接请求,在进入客户端主窗口之前首先需要进入登陆窗口,在该窗口中用可以录入待登陆的服务器地址,以及客户聊天的呢称.单击Login按钮之后,通信双方建立连接,可以开始一对一的对话.
//聊天客户端登陆用户界面
//聊天客户端登陆用户界面
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.*;
public class Login extends JFrame implements ActionListener{
 
 JFrame jf=new JFrame("Login");
 JPanel p1=new JPanel();
 JPanel p2=new JPanel();
 JLabel l1=new JLabel("请输入您的呢称");
 JLabel l2=new JLabel("请输入服务器地址");
 JTextField t1=new JTextField();
 JTextField t2=new JTextField("127.0.0.1");
 JButton loginButton=new JButton("Login");
      
//构造客户端用户界面
public Login(){
p1.setLayout(new GridLayout(2,2));
p1.add(l1);
p1.add(t1);
p1.add(l2);
p1.add(t2);
p2.setLayout(new FlowLayout());
p2.add(loginButton);
jf.getContentPane().add(p1,"North");
jf.getContentPane().add(p2,"Center");
jf.pack();
jf.setLocation(300,200);
jf.setSize(300,150);
jf.setVisible(true);
t1.addActionListener(this);
t2.addActionListener(this);
loginButton.addActionListener(this);
}

//接受用户请求的事件处理方法
public void actionPerformed(ActionEvent e){
if(!t1.getText().equals("")&&!t2.getText().equals(""))
{  
//启动客户端聊天主窗口
new TestClient(t1.getText(),t2.getText());
jf.setVisible(false);

}
}public  static void main(String[] args){
new Login();
}}
服务器端子模块的实现 
//服务器端用户界面
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import javax.swing.*;public class TestServer extends JFrame {
boolean started = false;
boolean bConnected;
ServerSocket ss;
Socket s;
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea ta;


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


//界面初始化
public TestServer(){
this.setTitle("聊天程序服务器端");
JScrollPane jp=new JScrollPane();
ta=new JTextArea(30,30);
Panel p=new Panel();
tf=new JTextField(20);
JButton b=new JButton("发送");
//b.addActionListener((ActionListener) this);
b.addActionListener(new TFListener());
tf.addActionListener(new TFListener());
p.add(tf);
p.add(b);
jp.setViewportView(ta);
this.getContentPane().add(jp);
this.getContentPane().add("South",p);
this.setSize(350,250);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
tf.requestFocus();
this.connect();//建立连接
this.createReadThread();//启动接受信息线程*/
}

//建立通信双方色连接,准备通信所需的流

public void connect(){
try {
ss = new ServerSocket(911);
started = true;
} catch (BindException e) {
System.out.println("端口使用中....");
System.out.println("请关掉相关程序并重新运行服务器!");
System.exit(0);;
} catch (IOException e) {
e.printStackTrace();
}


try{
while(started){
boolean bConnected = false;
        s=ss.accept();
System.out.println("连上拉!!!!");}
bConnected = true;

while(bConnected) {
dis=new DataInputStream( s.getInputStream());//准备输入流
dos=new DataOutputStream( s.getOutputStream());//准备输出流
String str=dis.readUTF();
System.out.print(str);
}
//dis.close();

}catch(IOException e){
System.out.println("没连上客户端");
}

}
//启动接受对方线程的方法
 public void createReadThread(){
  ReadThread rt=new ReadThread(this.ta,this.dis);
  rt.start();
}

//发送信息的事件的处理方法
private class TFListener implements ActionListener {
public void actionPerformed(ActionEvent e){
 try {
 String s=tf.getText().trim();


      ta.append("自己说: "+s);
      ta.append("\n");
      tf.setText("");
      tf.requestFocus();
      dos.writeUTF(s);
     } catch (IOException e1) {
     e1.printStackTrace();
     }
}
客户端子模块的实现
//客户端主界面
import java.awt.Panel;
import java.awt.event.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;import javax.swing.*;public class TestClient extends JFrame implements ActionListener{
Socket s2;
DataInputStream dis;
DataOutputStream dos;
JTextField tf;
JTextArea  ta;
String s11,s22;//构造函数界面初始化
public TestClient(String s1,String s2){
 this.setTitle("聊天程序客户端");
 JScrollPane jp=new JScrollPane();
 ta=new JTextArea(10,10);
 Panel p=new Panel();
 tf=new JTextField(20);
 JButton b=new JButton("发送");
 b.addActionListener((ActionListener) this);
 tf.addActionListener(this);
 p.add(tf);
 p.add(b);
 jp.setViewportView(ta);
 this.getContentPane().add(jp);
 this.getContentPane().add("South",p);
 this.setSize(350,250);
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/* this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent arg0) {
//disconnect();
System.exit(0);
}
}

 );*/
 this.s11=s1;//接受用户呢称
 this.s22=s2;//接受服务器地址
 this.setVisible(true);
 tf.requestFocus();
 this.connect();
 this.createReadThread();
}


public void connect(){
try {
   s2=new Socket(s22,911);
dis=new DataInputStream(s2.getInputStream());
dos = new DataOutputStream(s2.getOutputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
  System.out.println("服务器连接故障");

}
/*public void disconnect() {
try {
dos.close();
s2.close();
} catch (IOException e) {

e.printStackTrace();
}

}*/


public void createReadThread(){
 ReadThread rt=new ReadThread(this.ta,this.dis);
 rt.start();
}

public void actionPerformed(ActionEvent e){


try {
String s=tf.getText().trim();
dos.writeUTF(s11+"说: "+ s);
ta.append("自己说: "+ s);
    ta.append("\n");
tf.setText("");
tf.requestFocus();

} catch (IOException e2) {

e2.printStackTrace();
}


}
}

//
ReadThread.java
imort.io.*;
import.net.*;
import.awt.*
class ReadThread extends Thread{
JTextArea ta;
DataInputStream dis;
//传输文本区域以及输入流的构造方法
public ReadThread(JtextArea t,DataInputStream d){
this.ta=t;
this.dis=d;
}
//接受对方信息线程对应的线程体
public void run(){
try{
   while(true){
ta.append("对方说"+dis.readUTF());//接受对方发送的信息
ta.append("\n");
}
}catch(IOException e){
System.out.println("连接中断");
}
}
}

解决方案 »

  1.   

    www.andrii.javaeye.com上有个现成的一对多聊天系统,去下来看看
      

  2.   

    while(started){
    boolean bConnected = false;
            s=ss.accept();
    System.out.println("连上拉!!!!");}
    你的服务器 在不停的等待客户端的连接
    根本就没有继续往下处理            while (started)
                {
                    boolean bConnected = false;
                    s = ss.accept();
                    System.out.println("连上拉!!!!");
                    started = false;
                }改成这样就可以了