服务器端:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.*;
import java.util.Scanner;
public class Server extends Thread{
public static String[] vs = new String[10];
public static int i =0;
private Socket socket;

public  void run(){
try{
ServerSocket server = new ServerSocket(1600);
while(true){
System.out.println("服务器启动中......");
socket = server.accept();
ClientLine client = new ClientLine(socket);
client.start();
}
}catch(IOException e){
System.out.println("程序关闭....");
System.exit(0);
}
}

public static synchronized void message(String msg){
vs[i] = msg;
i = (i+1)%10;
vs[i] = "*";
}

public static void main(String args[]){
new Server().start();
}

}
class ClientLine extends Thread {

Socket client;
String msg;
BufferedReader in;
PrintWriter out;
ClientLine(Socket socket){
this.client = socket;
}
public void run(){
try{

in = new BufferedReader(new InputStreamReader(
                          client.getInputStream()));
out = new PrintWriter(client.getOutputStream());

SendToAll send = new SendToAll();
send.start();
msg = in.readLine();
while(!msg.equals("exit")){
Server.message(msg);
msg = in.readLine();
}
if(msg.equals("exit")){
in.close();
out.close();
client.close();
}

}catch(IOException e){
System.out.println("出现异常!");
}
}
public class SendToAll extends Thread{
private int j = 0;
public void run(){

try{
sleep(500);
}catch(InterruptedException e){
System.out.println("同步错误....");

}
while(true){
if(!Server.vs[j].equals("*")){
out.println(Server.vs[j]);
out.flush();
j = (j+1)%10;

}
}

}
}
}客户端:import java.util.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;class Client extends JFrame{
JLabel l1;
JTextField text1;
JTextArea ta,tt;
JButton b1,b2;
JPanel p1,p2;
Socket socket = null;
BufferedReader in;
PrintWriter out;
String b,c;

Client(){
//设定窗口,并建立两个按钮事件`
setSize(400,400);
setLocation(300,300);
setVisible(true);
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
l1 = new JLabel("请输入你的昵称:");
text1 = new JTextField(10);
b1 = new JButton("建立连接");
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){    //按钮事件` 
l1.setEnabled(false);
text1.setEditable(false);
b1.setEnabled(false);
repaint();
count();                            //调用连接方法` 开始连接`

}
});
b2 = new JButton("发送");
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
b = ta.getText();
out.print(b);
ta.setText("");
}
});
p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p2 = new JPanel(new BorderLayout());
ta = new JTextArea(5,1);
tt = new JTextArea();
tt.setEditable(false);
cp.add(p2,BorderLayout.SOUTH);
cp.add(p1,BorderLayout.NORTH);
JScrollPane scroll1 = new JScrollPane(tt);
cp.add(scroll1,BorderLayout.CENTER);
p1.add(l1);
p1.add(text1);
p1.add(b1);
JScrollPane scroll = new JScrollPane(ta);
p2.add(scroll,BorderLayout.CENTER);
p2.add(b2,BorderLayout.EAST);
repaint();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
public void count(){                                //定义建立连接的方法
try{
socket = new Socket("127.0.0.1",1600);
}catch(IOException a){System.out.println("找不到服务器");} try{
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream());
new ClientThread().start();
}catch(IOException Ee){ System.out.println("连接异常");} 
}

class ClientThread extends Thread{
public void run(){
String msg;
try{
msg = in.readLine();
while(true){
tt.append(msg+"\n");
msg = in.readLine();
}
}catch(IOException e){
System.out.println("连接异常!");}
}
}
public static void main(String args[]){
new Client();
}
}为什么会出现连接异常`
我是对照别人写的一个聊天室程序写的`

解决方案 »

  1.   

    服务器启动中......
    服务器启动中......
    Exception in thread "Thread-2" java.lang.NullPointerException
    at ClientLine$SendToAll.run(Server.java:88)
    出现异常!
      

  2.   

    异常数显在ClientLine中的SendToAll中的类的run()方法
      

  3.   

    貌似 是因为数组为NULL`  可以结了`
      

  4.   

    private Socket socket;你这个服务端使用同一个socket和客户端连接??能行吗?
      

  5.   

                SendToAll send = new SendToAll();
                send.start();
                msg = in.readLine();
                while(!msg.equals("exit")){
                    Server.message(msg);
                    msg = in.readLine();
                }
    这几句顺序有问题。已经开始调用run方法了,还没有给vs数组赋值呢。
      

  6.   

    我想,你用Debug调试一下会比在这问有效些。
      

  7.   

    NullPointerExceptiondebug一下再清楚不过了
      

  8.   

    是啊,不行就DEBUG去找问题原因
    再不行就用最原始的System.out.println();去调