用socket实现多线程server,我现在写好了server。如何实现多线程,给个思路。谢谢
解决方案 »
- 求各位高手指点
- 关于JAVA读取文件流的问题
- JS有没有自动选择下拉框里的东西的?
- word文档页数
- 如何设置table属性,使得它不会压缩设置好的列宽?
- 有关鼠标拖曳时显示图标的问题 MouseMotionListener.mouseDragged(..)
- 在Think in java 中经常看到一个对象的"reference"??这个怎么解释才算好的呢??谢谢!!
- 土土地问,import问题
- Oracle的结果集怎么会是readonly的?我明明创的是UpdateAble的,急!!!
- 用Java可以解决这个问题吗?急!!!
- 一段代码 帮忙看以下 急用!
- 请教:java中如何判断点是否在多边形内!!!!!!!!!!!!!!
KKMultiServer和KKMultiServerThread
前面还有客户端的多线程
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Vector;public class GoinServer {
static final int DEFAULT_PORT = 3135; static ServerSocket serverSocket; static Vector<Socket> connections; static Vector<ClientProc> clients; public static void sendAll(String s) {
if (connections != null) {
for (Enumeration e = connections.elements(); e.hasMoreElements();) {// send
// the
// msg
// to
// every
// client
// sockets
try {
PrintWriter pw = new PrintWriter(((Socket) e.nextElement())
.getOutputStream());
pw.println(s);
pw.flush();
} catch (IOException ex) {
}
}
}
System.out.println("sendAll:" + s);
} public static boolean sendOne(String name, String msg) {
if (clients != null) {
for (Enumeration e = clients.elements(); e.hasMoreElements();) {
ClientProc cp = (ClientProc) e.nextElement();
if ((cp.getName()).equals(name)) {// send the msg to a special
// client socket.
try {
PrintWriter pw = new PrintWriter((cp.getSocket())
.getOutputStream());
pw.println(msg);
pw.flush();
return true;
} catch (IOException ioe) {
}
}
}
}
return false;
} public static void addConnection(Socket s, ClientProc cp) {
if (connections == null) {
connections = new Vector<Socket>();
}
connections.addElement(s); if (clients == null) {
clients = new Vector<ClientProc>();
}
clients.addElement(cp);
} public static void deleteConnection(Socket s, ClientProc cp)
throws IOException {
if (connections != null) {
connections.removeElement(s);
s.close();
}
if (clients != null) {
clients.removeElement(cp);
}
} public static Vector getClients() {
return clients;
} public static void main(String[] arg) { int port = DEFAULT_PORT;
try {
serverSocket = new ServerSocket(port);
System.out.println("Goin server has started!");
} catch (IOException e) {
System.err.println(e);
System.exit(1);
}
while (true) {
try {
Socket cs = serverSocket.accept(); ClientProc cp = new ClientProc(cs);// for every user, the
// server start a thread to
// communicate with he/she. Thread ct = new Thread(cp);
ct.start();// the thread take a socket of one user start here. addConnection(cs, cp);// make a communicate channel between
// user and server.
} catch (IOException e) {
System.err.println(e);
}
}
}
}// ClientProc deal the data exchange logic between users and the server.
class ClientProc implements Runnable { Socket s; BufferedReader in; PrintWriter out; private String name = null; private String sex = null; // Date now; public ClientProc(Socket s) throws IOException {
this.s = s;
in = new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new PrintWriter(s.getOutputStream());
this.updateList();
} public String getName() {
return name;
} public String getSex() {
return sex;
} public Socket getSocket() {
return s;
} private void updateList() {// has problems!
Vector cs = GoinServer.getClients();
if (cs != null) {
for (Enumeration e = cs.elements(); e.hasMoreElements();) {
ClientProc cp = (ClientProc) e.nextElement();
String exist_name = cp.getName();
String exit_sex = cp.getSex();
/*
* for (int i = 0;i<cs.size() ;i++ ){ String exist_name
* =((ClientProc)cs.elementAt(i)).getName(); String exit_sex =
* ((ClientProc)cs.elementAt(i)).getSex();
*/
out.println("old" + "&" + exist_name + "&" + exit_sex);
out.flush(); }
}
} // Thread's run
public void run() {
while (name == null) {// the clientpro is a thread, when it begins,
// name=null.
try {
String inmsg;
inmsg = in.readLine();
// ChatServer.sendAll("Notic: "+inmsg+" has come!"); String[] userInfo;
userInfo = inmsg.split("&");
name = userInfo[0];
sex = userInfo[1];
GoinServer.sendAll("Notic: " + name + " has come!"); } catch (IOException ee) {
}
}
while (true) {// listening...
try {
String line = in.readLine(); if (line.equals("quit")) {
GoinServer.sendAll("Notic: " + this.name + " has quit!");
GoinServer.deleteConnection(s, this);
return;
} else if (line.equals("refresh")) {
this.updateList();
} else {// deliver the msg!
// now = new Date();
// String datestr =
// DateFormat.getTimeInstance().format(now);
// ChatServer.sendAll(datestr+" "+name + "\n " +line);
// name is the msg sender. inmsg[2] is the sender's aim
// buddy.
// inmsg[3] is the sender's words
if (line.startsWith("ToAll")) {
String[] inmsg = line.split("&");
GoinServer.sendAll("ToAll" + "&" + name + "&"
+ inmsg[1]);
} else if (line.startsWith("privateChat")) {
String[] inmsg = line.split("&"); GoinServer.sendOne(inmsg[1], "privateChat" + "&" + name
+ "&" + inmsg[2]);
}
}
} catch (IOException e) {
System.out.println("OMG,a Bug! ->" + e);
try {
s.close();
} catch (IOException e2) {
}
return;
}
}
}
}
//---------供参考------------//
while (true) {
try {
Socket cs = serverSocket.accept();ClientProc cp = new ClientProc(cs);// for every user, the
// server start a thread to
// communicate with he/she.Thread ct = new Thread(cp);
ct.start();// the thread take a socket of one user start here.addConnection(cs, cp);// make a communicate channel between
// user and server.
} catch (IOException e) {
System.err.println(e);
}
}
上个月刚写了个小LAN聊天程序,你要用得着就给你,hoho