不知道为什么请大家帮忙一下public class ChatServer 
{
public static void main(String args[]) {
ServerSocket server = null;
Socket socket = null;
Hashtable chatList;
chatList = new Hashtable();

while (true)
{
try
{
server = new ServerSocket(6666);
}
catch (IOException e1)
{
System.out.println("Listening......");
}
try
{
socket = server.accept();
InetAddress address = socket.getInetAddress();
System.out.println("用户的IP:"+address);
}
catch (IOException e)
{
if(socket != null) {
ServerThread chatThread = new ServerThread(socket,chatList);
chatThread.start();
}
else {
continue;
}
}
}
}class ServerThread extends Thread
{
String name = null,sex = null;
Socket socket = null;
DataOutputStream out = null;
DataInputStream in = null;
Hashtable chatList = null; ServerThread(Socket t,Hashtable list) {

chatList = list;
socket = t;
try
{
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e)
{
}
} public void run() {

while (true)
{
String s = null; try
{
s = in.readUTF();
System.out.println(s);
if(s.startsWith("姓名:")) {
name = s.substring(s.indexOf(":")+1,s.indexOf("性别"));
sex = s.substring(s.lastIndexOf(":")+1);
boolean boo = chatList.containsKey(name);
if(boo==false) {
chatList.put(name,this);
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
ServerThread th = (ServerThread)enum1.nextElement();
th.out.writeUTF("聊天者:"+name+"性别"+sex);
if(th != this) {
out.writeUTF("聊天者:"+th.name+"性别"+th.sex);
}
}
}
}
else if (s.startWith("聊天:"))
{
String message = s.substring(s.indexOf(":")+1);
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements)
{
((ServerThread)enum1.nextElements()).out.writeUTF("聊天内容:"+message);
}
}
else if (s.startsWith("用户离线:"))
{
Enumeration enum1 = chatList.elements();
while(enum1.hasMoreElements()) {
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive)
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
socket.close();
System.out.println(name+"用户离线");
break;
}
}
catch (IOException ee)
{
Enumeration enum1 = chatList.elements();
while (enum1.hasMoreElements())
{
try
{
ServerThread th = (ServerThread)enum1.nextElement();
if (th != this&&th.isAlive())
{
th.out.writeUTF("用户离线:"+name);
}
}
catch (IOException eee)
{
}
}
chatList.remove(name);
try
{
socket.close();
}
catch (IOException eee)
{
}
System.out.println(name+"用户离线了");
break;
}
}
}
}

解决方案 »

  1.   


    public class ChatServer 

    public static void main(String args[]) { 
    ServerSocket server = null; 
    Socket socket = null; 
    Hashtable chatList; 
    chatList = new Hashtable(); while (true) 

    try 

    server = new ServerSocket(6666); 

    catch (IOException e1) 

    System.out.println("Listening......"); 

    try 

    socket = server.accept(); 
    InetAddress address = socket.getInetAddress(); 
    System.out.println("用户的IP:"+address); 

    catch (IOException e) 

    if(socket != null) { 
    ServerThread chatThread = new ServerThread(socket,chatList); 
    chatThread.start(); 

    else { 
    continue; 




    }//这里少了个中括号class ServerThread extends Thread 

    String name = null,sex = null; 
    Socket socket = null; 
    DataOutputStream out = null; 
    DataInputStream in = null; 
    Hashtable chatList = null; ServerThread(Socket t,Hashtable list) { chatList = list; 
    socket = t; 
    try 

    in = new DataInputStream(socket.getInputStream()); 
    out = new DataOutputStream(socket.getOutputStream()); 

    catch (IOException e) 


    } public void run() { while (true) 

    String s = null; try 

    s = in.readUTF(); 
    System.out.println(s); 
    if(s.startsWith("姓名:")) { 
    name = s.substring(s.indexOf(":")+1,s.indexOf("性别")); 
    sex = s.substring(s.lastIndexOf(":")+1); 
    boolean boo = chatList.containsKey(name); 
    if(boo==false) { 
    chatList.put(name,this); 
    Enumeration enum1 = chatList.elements(); 
    while(enum1.hasMoreElements()) { 
    ServerThread th = (ServerThread)enum1.nextElement(); 
    th.out.writeUTF("聊天者:"+name+"性别"+sex); 
    if(th != this) { 
    out.writeUTF("聊天者:"+th.name+"性别"+th.sex); 




    else if (s.startWith("聊天:")) 

    String message = s.substring(s.indexOf(":")+1); 
    Enumeration enum1 = chatList.elements(); 
    while (enum1.hasMoreElements) 

    ((ServerThread)enum1.nextElements()).out.writeUTF("聊天内容:"+message); 


    else if (s.startsWith("用户离线:")) 

    Enumeration enum1 = chatList.elements(); 
    while(enum1.hasMoreElements()) { 
    try 

    ServerThread th = (ServerThread)enum1.nextElement(); 
    if (th != this&&th.isAlive) 

    th.out.writeUTF("用户离线:"+name); 


    catch (IOException eee) 



    chatList.remove(name); 
    socket.close(); 
    System.out.println(name+"用户离线"); 
    break; 


    catch (IOException ee) 

    Enumeration enum1 = chatList.elements(); 
    while (enum1.hasMoreElements()) 

    try 

    ServerThread th = (ServerThread)enum1.nextElement(); 
    if (th != this&&th.isAlive()) 

    th.out.writeUTF("用户离线:"+name); 


    catch (IOException eee) 



    chatList.remove(name); 
    try 

    socket.close(); 

    catch (IOException eee) 


    System.out.println(name+"用户离线了"); 
    break; 



    }
    你的程序还有错误!
      

  2.   

    少打了个括号之类的。你拿什么写的代码?用 Eclipse 吧。