我正在编一个游戏,想编成多人游戏有一个问题请教大家 使用listener技术。每个客户机都在服务器注册为接收者,在服务器收到一个客户机的消息时,向所有的接收者发送该消息。很多java书关于网络编程的都有例子的。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这是服务器程序import java.io.*;import java.net.*;class ExServiceThread extends Thread {private Socket m_socket;private BufferedReader in;private PrintWriter out;public ExServiceThread (Socket s) throws IOException {m_socket=s;in = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream())),true);start();}public void run() {try {while (true) {String str = in.readLine();System.out.println("Receive:"+str);out.println(str);if (str.equals("BYE")) break;}System.out.println("Closing...");} catch (IOException e) {} finally {try { m_socket.close();}catch (IOException e) {}} }}class ExListenThread extends Thread {static final int PORT =4321;private ServerSocket m_socket;private boolean stop=false;public ExListenThread() throws IOException {m_socket = new ServerSocket(PORT);start(); }public void terminate() {stop=true;}public void run(){System.out.println("server started:");try {while (!stop) {Socket c_socket =m_socket.accept();try {new ExServiceThread(c_socket);} catch (IOException e) {c_socket.close();}}} catch (IOException e) {}finally {try {m_socket.close();}catch (IOException e) {} } }}public class demo {public static void main(String[] args) throws IOException {try {ExListenThread listenthread=new ExListenThread();while (true){BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));String str=stdin.readLine();if (str.equals("quit")) {listenthread.terminate();break;}}} catch (IOException e) {} }}这是客户端程序//通话器客户端import java.net.*;import java.io.*;import java.lang.*; public class demo1dos { public static void main(String args[]){if (args.length<1) {//判断命令加参数没有 System.out.println("you forget the name of the server!"); System.out.println("see also: myclient yxf"); System.exit(1); //如果没加参数就退出 }Socket socket;String s;String len;InputStream Is;OutputStream Os;DataInputStream DIS;PrintStream PS;try{//向主机名为args[0]的服务器申请连接//注意端口号要与服务器保持一致:43210 socket=new Socket(args[0],4321);System.out.println("client ok");System.out.println("************************************************");System.out.println("");//获得对应socket的输入/输出流Is=socket.getInputStream();Os=socket.getOutputStream();//建立数据流DIS=new DataInputStream(Is);PS=new PrintStream(Os);DataInputStream in=new DataInputStream(System.in);while(true) { System.out.print("you say:"); s=in.readLine();//读取用户输入的字符串 PS.println(s);//将读取得字符串传给server if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 else { System.out.println(""); System.out.println("please wait server's message..."); System.out.println(""); } s=DIS.readLine();//从服务器获得字符串 System.out.println("server said:"+s); //打印字符串 if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 }//关闭连接DIS.close();//关闭数据输入流PS.close();//关闭数据输出流Is.close();//关闭输入流Os.close();//关闭输出流socket.close();//关闭socket}catch(Exception e){System.out.println("Error:"+e);}}}这个程序只能客户机和sever程序通信时,sever才能和客户机通信。能不能让服务器程序主动针对某个客户机通信? 你也可以用数据报UDP,多播数据报呀!数据报很快而且不很占资源!然后用线程监察客户消息啊 可以建个数据库用来存客户的ip地址,客户每次登录在数据库保存ip,然后服务器程序取他们的ip,然后用UDP发消息 是的服务器把接到的数据都传给每个客户机上Socket socket;我把socket里面的内容显示出来了,是 Socket[add=0.0.0.0/0.0.0.0,..一长串地址数据,我想把它保存在all[i]字符型数组里,但显示不兼容的类型错误,我应该怎么保存呢?如果能保存这个地址数据的话我就能把数据传给每个客户机上 这个你要吗?http://www-900.ibm.com/developerWorks/java/l-oicq/index.shtmlhttp://www-900.ibm.com/developerWorks/java/l-oicq/src.zip程序http://www-900.ibm.com/developerWorks/java/l-oicq/pic.zip抓的图 目前是用jdk编程Socket socket;我把socket里面的内容显示出来了,是 Socket[add=0.0.0.0/0.0.0.0,..一长串地址数据,我想把它保存在all[i]字符型数组里,但显示不兼容的类型错误,我应该怎么保存呢? 请使用 DexlphiX-DirectPlay一般情况应该是 客户发送到服务器,服务器视情况再将数据转发到其他各客户。 DexlphiX-DirectPlay这个我没听说过怎么用呀?能举个例子吗? 我也曾经用udp作过类似的,可是udp数据报经常地丢失,特别是不在局域网中。 你编的游戏是图形化的吗客户端也是用纯java写吗 不知道你是用什么工具的,我有个VC的C/S聊天室源程序,如果要的话,你的MAIL 我用的是纯java编写的游戏是图形化的。请大家讨论一下做这个游戏的方法 //取得联接ipms=new ServerSoceket(port);Socket cs=ms.accept();InetAddress cip=cs.getInetAddress();System.out.println(cip); Socket socket;是指socket类型的。 你为什么不建个数据库用来存客户的ip地址,客户每次登录在数据库保存ip,然后服务器程序取他们的ip,然后用UDP发消息 。我觉得http://www-900.ibm.com/developerWorks/java/l-oicq/index.shtml和你的原理差不多,在http://www-900.ibm.com/developerWorks/java/l-oicq/src.zip程序里的server.java里有你要的东西,但是是放到数据库里的,比如:String setip="update icq set ip=? where icqno=?"; PreparedStatement prest=c.prepareCall(setip); prest.clearParameters();prest.setString(1,socket.getInetAddress().getHostAddress()); prest.setInt(2,g);int set=prest.executeUpdate(); noclassdeffounderror java输出问题 关于方法调用的困惑? 需要一个JSP熟手帮忙,要懂ORACLE 谁用过 ofbiz 做过 checkbox的例子,会的请进 请教 注意:C:\java\Loginfrm.java 使用或覆盖了已过时的 API。 哪个有j2ee的文档 怎样找到上次提交后new的线程? 大侠帮忙呀,是有关于Applet的小问题 朋友 帮忙! 关于java中的corejava.*包。我在书中看到有不少都用,但编程的时候说没有这个包,是怎么回事? 菜鸟求救!!!
import java.io.*;
import java.net.*;class ExServiceThread extends Thread {private Socket m_socket;
private BufferedReader in;
private PrintWriter out;public ExServiceThread (Socket s) throws IOException {
m_socket=s;
in = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream())),true);
start();
}
public void run() {
try {
while (true) {
String str = in.readLine();
System.out.println("Receive:"+str);
out.println(str);
if (str.equals("BYE")) break;
}
System.out.println("Closing...");
} catch (IOException e) {} finally {
try { m_socket.close();}
catch (IOException e) {}
}
}
}class ExListenThread extends Thread {
static final int PORT =4321;
private ServerSocket m_socket;
private boolean stop=false;public ExListenThread() throws IOException {
m_socket = new ServerSocket(PORT);
start();
}public void terminate() {stop=true;}public void run(){
System.out.println("server started:");
try {
while (!stop) {
Socket c_socket =m_socket.accept();
try {
new ExServiceThread(c_socket);
} catch (IOException e) {
c_socket.close();
}
}
} catch (IOException e) {}finally {
try {
m_socket.close();
}catch (IOException e) {}
}
}
}public class demo {
public static void main(String[] args) throws IOException {
try {
ExListenThread listenthread=new ExListenThread();
while (true){
BufferedReader stdin =new BufferedReader(new InputStreamReader(System.in));
String str=stdin.readLine();
if (str.equals("quit")) {
listenthread.terminate();
break;
}
}
} catch (IOException e) {}
}
}这是客户端程序
//通话器客户端
import java.net.*;
import java.io.*;
import java.lang.*;
public class demo1dos {
public static void main(String args[]){
if (args.length<1)
{//判断命令加参数没有
System.out.println("you forget the name of the server!");
System.out.println("see also: myclient yxf");
System.exit(1); //如果没加参数就退出
}Socket socket;
String s;
String len;
InputStream Is;
OutputStream Os;
DataInputStream DIS;
PrintStream PS;try{//向主机名为args[0]的服务器申请连接
//注意端口号要与服务器保持一致:43210
socket=new Socket(args[0],4321);System.out.println("client ok");
System.out.println("************************************************");
System.out.println("");//获得对应socket的输入/输出流
Is=socket.getInputStream();
Os=socket.getOutputStream();
//建立数据流
DIS=new DataInputStream(Is);
PS=new PrintStream(Os);
DataInputStream in=new DataInputStream(System.in);while(true)
{
System.out.print("you say:");
s=in.readLine();//读取用户输入的字符串
PS.println(s);//将读取得字符串传给server
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出
else
{
System.out.println("");
System.out.println("please wait server's message...");
System.out.println("");
}
s=DIS.readLine();//从服务器获得字符串
System.out.println("server said:"+s); //打印字符串
if(s.trim().equals("BYE"))break; //如果是"BYE",就退出 }//关闭连接
DIS.close();//关闭数据输入流
PS.close();//关闭数据输出流
Is.close();//关闭输入流
Os.close();//关闭输出流
socket.close();//关闭socket
}
catch(Exception e){
System.out.println("Error:"+e);
}
}}
这个程序只能客户机和sever程序通信时,sever才能和客户机通信。能不能让服务器程序主动针对某个客户机通信?
然后用线程监察客户消息啊
Socket socket;
我把socket里面的内容显示出来了,是 Socket[add=0.0.0.0/0.0.0.0,..
一长串地址数据,我想把它保存在all[i]字符型数组里,但显示不兼容的类型错误,我应该怎么保存呢?
如果能保存这个地址数据的话我就能把数据传给每个客户机上
http://www-900.ibm.com/developerWorks/java/l-oicq/index.shtml
http://www-900.ibm.com/developerWorks/java/l-oicq/src.zip程序
http://www-900.ibm.com/developerWorks/java/l-oicq/pic.zip抓的图
Socket socket;
我把socket里面的内容显示出来了,是 Socket[add=0.0.0.0/0.0.0.0,..
一长串地址数据,我想把它保存在all[i]字符型数组里,但显示不兼容的类型错误,我应该怎么保存呢?
这个我没听说过怎么用呀?
能举个例子吗?
客户端也是用纯java写吗
ms=new ServerSoceket(port);
Socket cs=ms.accept();
InetAddress cip=cs.getInetAddress();
System.out.println(cip);
是指socket类型的。
和你的原理差不多,在http://www-900.ibm.com/developerWorks/java/l-oicq/src.zip程序
里的server.java里有你要的东西,但是是放到数据库里的,比如:
String setip="update icq set ip=? where icqno=?";
PreparedStatement prest=c.prepareCall(setip);
prest.clearParameters();
prest.setString(1,socket.getInetAddress().getHostAddress());
prest.setInt(2,g);
int set=prest.executeUpdate();