用socket实现多线程server
用socket实现多线程server,我现在写好了server。如何实现多线程,给个思路。谢谢
[解决办法]
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html
lz看最下面
KKMultiServer和KKMultiServerThread
前面还有客户端的多线程
[解决办法]
package goin;
import java.io.BufferedReader;
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;
}
}
}
}
//---------供参考------------//
[解决办法]
每个客户端一个线程比较耗费资源,线程池应该更好