为什么在EditPlus中运行没有出错,在eclipse中运行就出错了呢
代码如下:import java.io.*;
import java.net.*;public class ChatServer {
boolean started = false;
ServerSocket ss = null;

public static void main(String[] args) {
new ChatServer().start();//这行报错
}

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

try {

while(started) {
Socket s = ss.accept();
Client c = new Client(s);
System.out.println("a client connected!");
new Thread(c).start();
//dis.close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ss.close();//这行报错
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

class Client implements Runnable {
private Socket s;
private DataInputStream dis = null;
private boolean bConnected = false;

public Client(Socket s) {
this.s = s;
try {
dis = new DataInputStream(s.getInputStream());
bConnected = true;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public void run() {
try {
while(bConnected) {
String str = dis.readUTF();
System.out.println(str);
}
} catch (EOFException e) {
System.out.println("Client closed!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dis != null) dis.close();
if(s != null) s.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}

}
}
在eclipse中提示的错误如下:
java.net.SocketException: Unrecognized Windows Sockets error: 0: JVM_Bind
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.bind(Unknown Source)
at java.net.ServerSocket.bind(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at java.net.ServerSocket.<init>(Unknown Source)
at ChatServer.start(ChatServer.java:17)
at ChatServer.main(ChatServer.java:12)
Exception in thread "main" java.lang.NullPointerException
at ChatServer.start(ChatServer.java:41)
at ChatServer.main(ChatServer.java:12)
请帮看看,急用 谢谢

解决方案 »

  1.   

    你的客户端写的就像是服务器端代码一样
    下面是我写的  仅供参考
    客户端:
    import java.net.*;
    import java.io.*;
    public class TestClient {
    public static void main(String[] args) throws Exception{
    Socket so = new Socket("127.0.0.1",2222);
    DataInputStream dis = new 
      DataInputStream(so.getInputStream());
    DataOutputStream dos = new 
      DataOutputStream(so.getOutputStream());
    BufferedReader br = new BufferedReader(
    new InputStreamReader(System.in));
    while(true){
        String tt = br.readLine();
        dos.writeUTF(tt);
        if("over".equalsIgnoreCase(tt)) break;
        System.out.println(dis.readUTF());
    }
    dis.close();dos.close();
    so.close();
    }//一个人聊天完毕,第二个客户还可以聊
    }//服务器永远保持等待连接(不用考虑线程)
    服务器端:
    public class TestServer {
    public static void main(String[] args) throws Exception{
    ServerSocket ss = new ServerSocket(2222);
    while(true){
    Socket so = ss.accept();//停住
        ServerThread st = new ServerThread(so);
        st.start();
    }//使用多线程技术实现 多个客户 同时 运行
    }
    }
    服务器端线程:
    import java.io.DataInputStream;
    import java.io.DataOutputStream;
    import java.net.Socket;
    import java.net.ServerSocket;
    public class ServerThread extends Thread{
    private Socket so;
    private static String word = "";
    public ServerThread(Socket so){
    this.so = so;
    }
        public void run(){
          try{    
        DataInputStream dis = new 
          DataInputStream(so.getInputStream());
        DataOutputStream dos = new 
          DataOutputStream(so.getOutputStream());
        while(true){
            String temp = dis.readUTF();
            if("over".equalsIgnoreCase(temp)) break;
            word = word+temp+"\n";
            dos.writeUTF("Server:"+word);
        }
        dis.close();dos.close();
        so.close();
          }catch(Exception ee){
         ee.printStackTrace();
          }
        }
    }
      

  2.   

    这是端口冲突吧,你看看你在CMD运行服务器端以后服务器有没有关闭?还有在Eclipse运行完服务器端后也要手动关闭它…然后再运行新的服务,新的客户端…