一个SocketServer 如何同时向多个客户端发送数据? 最好能有事例。 基于socket的类msn系统,我刚做的,由socket server和applet client组成。可以提供设计文档(rose file),和一些基本代码(sorry for no complete code)如果需要,留你的email 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 thank you .give me please. [email protected] I also want.[email protected] 也能给我一份吗?[email protected] 请问 chenyuan_tongji : 如何通过服务器转接实现两个socket客户端之间的通信? thinforg001(瘦青蛙) :我给你留短信了 client端的unique标志解决方法很多呢比如某个client第一次和server交互的时候,server为其产生一个client key,保存在数据结构里面,和处理该client的thread匹配;总之,你自己东东好好想想肯定会有解决知道的,方法都是人想出来的,好不好是另外一回事,先解决问题。我也不知道自己的solution是不是规范,但是目前看,效果不错,就是这样。 随便给个例子就成。我java用的不是很多,所以里面都什么可以用我实在不清楚。戏可以自己演,但是道具你得给我呀。 谢谢您啊!也给我一份![email protected] to: chenyuan_tongji谢谢您啊!也给我一份![email protected] 一个 ServerSocket 只能和一个客户端建立连接.可以用多线程建立多个ServerSocket处理多客户. [email protected]我想要,谢谢 import java.net.*;import java.io.*;public class MyServer extends Thread{ protected MyClient[] clients; protected int maxnum; protected int number; public MyServer(int num) { this.maxnum = num; clients = new MyClient[this.maxnum]; } public static void main(String[] args) { MyServer myServer = new MyServer(20); myServer.start(); // add your GUI code here; // when you want send message to all client, invoke sendAll(); myServer.stop(); } public void run() { try { int port = 80; ServerSocket ss = new ServerSocket(port); while (true) { Socket client = ss.accept(); if (number < maxnum) { clients[number] = new MyClient(client); number++; } else { System.out.println("Get the Max Thread"); } } } catch (Exception e) { System.out.println(e.toString()); } } public void sendAll(String msg) { int i, j=0; for (i=0; i<maxnum; i++) { if (clients[i] != null) { clients[i].sendMessage(msg); j++; } } System.out.println("Message send to " + j + " client!"); }}class MyClient{ Socket client; BufferedReader in; PrintWriter out; MyClient (Socket s) { clie nt = s; try { in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(new OutputStreamWriter(client.getOutputStream())); } catch (Exception e) { System.out.println(e.toString()); } } public void sendMessage(String msg) { out.println(msg); out.flush(); }};一个简单的例子,还有许多问题,你可以参考一下。 import java.net.*;import java.io.*;public class MyServer extends Thread{ protected MyClient[] clients; protected int maxnum; protected int number; public MyServer(int num) { this.maxnum = num; clients = new MyClient[this.maxnum]; } public static void main(String[] args) { MyServer myServer = new MyServer(20); myServer.start(); // add your GUI code here; // when you want send message to all client, invoke sendAll(); myServer.stop(); } public void run() { try { int port = 80; ServerSocket ss = new ServerSocket(port); while (true) { Socket client = ss.accept(); if (number < maxnum) { clients[number] = new MyClient(client); number++; } else { System.out.println("Get the Max Thread"); } } } catch (Exception e) { System.out.println(e.toString()); } } public void sendAll(String msg) { int i, j=0; for (i=0; i<maxnum; i++) { if (clients[i] != null) { clients[i].sendMessage(msg); j++; } } System.out.println("Message send to " + j + " client!"); }}class MyClient{ Socket client; BufferedReader in; PrintWriter out; MyClient (Socket s) { clie nt = s; try { in = new BufferedReader(new InputStreamReader(client.getInputStream())); out = new PrintWriter(new OutputStreamWriter(client.getOutputStream())); } catch (Exception e) { System.out.println(e.toString()); } } public void sendMessage(String msg) { out.println(msg); out.flush(); }};重贴一个,希望csdn能自动将"/tab"转化为"4space" 谢谢给我一份,[email protected] 读取文件问题 JDBC PreparedStatement execute执行后成功插入数据库 确返回false 为什么? 大家有人见过这个错误提示吗?5555555555555希望大家说说啊。或者指引一条处理这个问题的方法啊55谢谢 Spring调用 [闭月羞花猫]项目中,最简单的build.xml文件的书写方式 如何获取类对象的个数? 讨论一下多态的用途 编译的错误 java方面哪些书比较经典? 只使用InputStream/OutputStream进行IO操作行吗? 问个简单的问题 怎么interrupt线程
[email protected]
[email protected]
thinforg001(瘦青蛙) :我给你留短信了
比如某个client第一次和server交互的时候,server为其产生一个client key,保存在数据结构里面,和处理该client的thread匹配;总之,你自己东东好好想想肯定会有解决知道的,方法都是人想出来的,好不好是另外一回事,先解决问题。我也不知道自己的solution是不是规范,但是目前看,效果不错,就是这样。
戏可以自己演,但是道具你得给我呀。
也给我一份![email protected]
也给我一份![email protected]
可以用多线程建立多个ServerSocket处理多客户.
我想要,谢谢
import java.io.*;public class MyServer extends Thread
{
protected MyClient[] clients;
protected int maxnum;
protected int number;
public MyServer(int num) {
this.maxnum = num;
clients = new MyClient[this.maxnum];
} public static void main(String[] args) {
MyServer myServer = new MyServer(20);
myServer.start(); // add your GUI code here;
// when you want send message to all client, invoke sendAll(); myServer.stop();
} public void run() {
try {
int port = 80;
ServerSocket ss = new ServerSocket(port);
while (true) {
Socket client = ss.accept();
if (number < maxnum) {
clients[number] = new MyClient(client);
number++;
}
else {
System.out.println("Get the Max Thread");
}
}
} catch (Exception e) {
System.out.println(e.toString());
}
} public void sendAll(String msg) {
int i, j=0;
for (i=0; i<maxnum; i++) {
if (clients[i] != null) {
clients[i].sendMessage(msg);
j++;
}
}
System.out.println("Message send to " + j + " client!");
}
}class MyClient{
Socket client;
BufferedReader in;
PrintWriter out; MyClient (Socket s) {
clie nt = s;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (Exception e) {
System.out.println(e.toString());
}
} public void sendMessage(String msg) {
out.println(msg);
out.flush();
}
};一个简单的例子,还有许多问题,你可以参考一下。
import java.io.*;public class MyServer extends Thread
{
protected MyClient[] clients;
protected int maxnum;
protected int number;
public MyServer(int num) {
this.maxnum = num;
clients = new MyClient[this.maxnum];
} public static void main(String[] args) {
MyServer myServer = new MyServer(20);
myServer.start(); // add your GUI code here;
// when you want send message to all client, invoke sendAll(); myServer.stop();
} public void run() {
try {
int port = 80;
ServerSocket ss = new ServerSocket(port);
while (true) {
Socket client = ss.accept();
if (number < maxnum) {
clients[number] = new MyClient(client);
number++;
}
else {
System.out.println("Get the Max Thread");
}
}
} catch (Exception e) {
System.out.println(e.toString());
}
} public void sendAll(String msg) {
int i, j=0;
for (i=0; i<maxnum; i++) {
if (clients[i] != null) {
clients[i].sendMessage(msg);
j++;
}
}
System.out.println("Message send to " + j + " client!");
}
}class MyClient{
Socket client;
BufferedReader in;
PrintWriter out; MyClient (Socket s) {
clie nt = s;
try {
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
out = new PrintWriter(new OutputStreamWriter(client.getOutputStream()));
} catch (Exception e) {
System.out.println(e.toString());
}
} public void sendMessage(String msg) {
out.println(msg);
out.flush();
}
};重贴一个,希望csdn能自动将"/tab"转化为"4space"
[email protected]