请问服务器端如何控制客户端的连接和短开连接? 有多个客户端连上服务器,我想在服务器这里短开其中一个客户端的连接,而保留其他客户端的连接,该如何做??? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 什么客户? 是数据库的客户, 还是WEB的客户端, 还是代理服务器的客户? 我做了一个socket方面的通讯,如下代码,现想在下面代码来处理断开客户端的连接: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("端口使用中...."); } 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) { 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) { 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(); } } } }} 请教一下如何用JAVA打开含有中文的文件并正确显示其中的文字? dom4j读取xml问题 求解(约瑟夫问题) 怎么编写一个监听某个端口的程序? 一個小數點四舍五入後,如結果1.1,我怎樣改成1.10,在結果後面加一位0? 如何读取jar包里的图片文件? 现在还可以用Vector与Hashtable吗? 谁能解决这个问题?是为什么? 请教:编译的时候为什么说不认识WindowAdapter? 请各位高手帮忙看看客户端关了readLine()为什么不报错. 请帮我看看这段代码. 怎么调用windows平台的输入法?
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("端口使用中....");
} 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) {
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) {
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();
}
}
}
}
}