首先,为各客户端都是起了线程处理的,在每个线程服务器端会有超时的处理,你用Socket类的setSoTimeout可以设订超时时间,然后catch()捕获就可以判断断开联结了。

解决方案 »

  1.   

    服务器端的多线程处理如楼上讲的,你的问题给出:
        ServerSocket servSock = new ServerSocket(9000);
        Socket socket = servSock.accept();
        boolean isAlive = socket.getKeepAlive();
      

  2.   

    package csserver;import java.io.*;
    import java.net.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */public class ClientThread extends Thread
    {
      String str;
      DataInputStream in;
      DataOutputStream out;
      Socket clientSocket=null;  public ClientThread(Socket client)
      {
        clientSocket=client;
      }  public void SendMsg(String str)throws IOException
      {
        out.writeUTF(str);
      }  public void run()
      {
        try
        {
         in=new DataInputStream(clientSocket.getInputStream());
         out=new DataOutputStream(clientSocket.getOutputStream());
         while(true)
         {
    str=in.readUTF();
    csserver.ReceiveMsg.append(str+"\n");
         }
        }
        catch(Exception e)
        {
          e.printStackTrace();
        }
        finally
        {
          try
          {
    clientSocket.close();
          }
          catch(Exception e1)
          {
          }
        }
      }
    }package csserver;import java.io.*;
    import java.awt.*;
    import javax.swing.*;
    import java.net.*;
    import java.util.*;
    import java.awt.event.*;/**
     * <p>Title: </p>
     * <p>Description: </p>
     * <p>Copyright: Copyright (c) 2002</p>
     * <p>Company: </p>
     * @author unascribed
     * @version 1.0
     */public class csserver extends JFrame implements ActionListener
    {
      static JTextArea ReceiveMsg = new JTextArea();
      static JTextArea SendMsg = new JTextArea();
      JButton Send = new JButton();
      JButton Exit = new JButton();
      Vector clients=new Vector();
      Socket clientSocket;
      ServerSocket serverSocket;
      ClientThread clientThread;  public csserver()
      {
        try
        {
          jbInit();
          serverSocket=new ServerSocket(8088,6);
          while(true)
          {
    clientSocket=serverSocket.accept();
    clientThread=new ClientThread(clientSocket);
    clients.addElement(clientThread);
    clientThread.start();
          }    }
        catch(Exception e)
        {
          e.printStackTrace();
        }
        finally
        {
          try
          {
    serverSocket.close();
          }
          catch(Exception e1)
          {
          }
        }
      }  private void jbInit() throws Exception
      {
        ReceiveMsg.setBorder(BorderFactory.createEtchedBorder());
        ReceiveMsg.setBounds(new Rectangle(17, 16, 332, 143));
        this.getContentPane().setLayout(null);
        SendMsg.setBorder(BorderFactory.createEtchedBorder());
        SendMsg.setBounds(new Rectangle(18, 169, 330, 32));
        Send.setBounds(new Rectangle(86, 219, 79, 29));
        Send.setBorder(BorderFactory.createEtchedBorder());
        Send.addActionListener(this);
        Send.setText("Send");
        Exit.setBounds(new Rectangle(206, 219, 79, 29));
        Exit.setBorder(BorderFactory.createEtchedBorder());
        Exit.addActionListener(this);
        Exit.setText("Exit");
        this.getContentPane().add(SendMsg, null);
        this.getContentPane().add(ReceiveMsg, null);
        this.getContentPane().add(Exit, null);
        this.getContentPane().add(Send, null);
        this.setTitle("Sever");
        this.setLocation(330,330);
        this.setSize(380,300);
        this.show();
      }  public static void main(String[] args)
      {
        new csserver();
      }  public void actionPerformed(ActionEvent e)
      {
        if(e.getSource()==Exit)
        {
          System.exit(0);
        }
        if(e.getSource()==Send&&clientThread!=null)
        {
          try
          {
    for(int i=0;i<clients.size();i++)
    {
      ClientThread clientThreads=(ClientThread)clients.elementAt(i);
      clientThreads.SendMsg(SendMsg.getText());
    }
    SendMsg.setText("");
          }
          catch(Exception e1)
          {
          }
        }
      }
    }我该如何修改程序,以使我的程序能知道哪个客户端断开了连结?
      

  3.   

    可以定义一个hashmap保存socket的状态,我看你用了vector,要查socket是否连接,可能要遍历.
    有一点不明白,你要什么时候检查socket的状态,作什么用,frame上是否要显示,等.
    如果你只是在SendMsg前检查是否连接,在clientThreads.SendMsg(SendMsg.getText());之前检查一下
    就可以了.
    ClientThread中加一个变量boolean isActive = true;并提供get方法.
    如果socket.getKeepAlive()性能不是太差的话,
         while(true)
         {
    str=in.readUTF();
    csserver.ReceiveMsg.append(str+"\n");
         }
    方法中隔一段时间检查一下连接.断了,设isActive状态. 
    否则,看socket连接断会不会抛异常,抛的话,改变isActive状态. 
    在csserver 中SendMsg前先判断clientThreads的isActive状态.
    不知有否帮助.
      

  4.   

    主要做用是当某一客户端断开联结时,用来释放VECTOR里相应的无素!不知道有什么好方法,因为我是才对JAVA进行学习的,所以对JAVA的类库比较陌生!所以做起程序来找不着东!谢谢大家指教!