关于SOCKET的问题。。。。 首先,为各客户端都是起了线程处理的,在每个线程服务器端会有超时的处理,你用Socket类的setSoTimeout可以设订超时时间,然后catch()捕获就可以判断断开联结了。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 服务器端的多线程处理如楼上讲的,你的问题给出: ServerSocket servSock = new ServerSocket(9000); Socket socket = servSock.accept(); boolean isAlive = socket.getKeepAlive(); 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) { } } }}我该如何修改程序,以使我的程序能知道哪个客户端断开了连结? 可以定义一个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状态.不知有否帮助. 主要做用是当某一客户端断开联结时,用来释放VECTOR里相应的无素!不知道有什么好方法,因为我是才对JAVA进行学习的,所以对JAVA的类库比较陌生!所以做起程序来找不着东!谢谢大家指教! JAVA的JVM垃圾回收算法问题 java中怎么把数据库中数据查询出来在窗体中显示? JAVA运行环境变量的配置深入讨论(很希望老紫竹也来指点指点) 关于JAVA中 jar命令 正则判断特殊字符 用JAVA语言什么工具最好或者哪方面用什么工具好? CoreJava的例子,居然严重错误 大家看下这段源码如何?? 如何在MS的ACTIVE DIRECTORY中添加自定义属性 前几天从C转向JAVA,这题会出错?我想跳楼! 请评论一下java流行的开发工具! 怎样在命令行使用ftp命令?
ServerSocket servSock = new ServerSocket(9000);
Socket socket = servSock.accept();
boolean isAlive = socket.getKeepAlive();
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)
{
}
}
}
}我该如何修改程序,以使我的程序能知道哪个客户端断开了连结?
有一点不明白,你要什么时候检查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状态.
不知有否帮助.