用socket实现多线程server

zydolphin 2007-04-05 09:16:01
用socket实现多线程server,我现在写好了server。如何实现多线程,给个思路。谢谢
...全文
294 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
skywoodsky 2007-04-05
  • 打赏
  • 举报
回复
每个客户端一个线程比较耗费资源,线程池应该更好
ljbatustc 2007-04-05
  • 打赏
  • 举报
回复
闪咯
ljbatustc 2007-04-05
  • 打赏
  • 举报
回复
哇哈哈,刚好在灌水萨。
上个月刚写了个小LAN聊天程序,你要用得着就给你,hoho
zydolphin 2007-04-05
  • 打赏
  • 举报
回复
哇……
回复那么快啊,这是我们的作业。这两天正在赶。
谢谢哥哥%姐姐%们了
ljbatustc 2007-04-05
  • 打赏
  • 举报
回复
不好意思,其实看看这一段就可以了。
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);
}
}
ljbatustc 2007-04-05
  • 打赏
  • 举报
回复
核心思想:为每一个客户端连接启动一个线程。
ljbatustc 2007-04-05
  • 打赏
  • 举报
回复
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;
}
}
}
}
//---------供参考------------//
vdragon 2007-04-05
  • 打赏
  • 举报
回复
http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

lz看最下面
KKMultiServer和KKMultiServerThread
前面还有客户端的多线程

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧