⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 server.java

📁 用Java语言实现的泡泡堂小游戏精简版。
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class Server implements Runnable, CommandListener{
    /*定义舞台*/
    Display display;
    /*定义父类,以供传递控制*/
    GameMIDlet parent;
    /*定义服务器端开发的监听端口*/
    ServerSocketConnection scn;
    /*端口连接控制*/
    SocketConnection sc;
    /*定义输入流,以接收客户端传来的数据*/
    InputStream is;
    /*定义输出流,以向客户端发送数据*/
    OutputStream os;
    /*定义游戏控制类,以便实现游戏的初始化和控制*/
    MyGameWithTiledLayerCanvas t;
	/*标记传输是否结束*/
	boolean stop;
/*定义画板*/
	Graphics h;
   /*定义个Alert以显示版本等信息*/
    Alert al;
    Command exitCommand=new Command("Exit", Command.EXIT, 1);
     Command exitCommand1=new Command("结束", Command.EXIT, 1);
    public Server(GameMIDlet m)
	{
	 parent=m;
	 /*得到父类舞台*/
	 display = Display.getDisplay(parent);	
	/*对Alert进行设置*/
	al = new Alert("Socket Server");
	al.setType(AlertType.INFO);
	al.setTimeout(Alert.FOREVER);
	al.setString("Version:1.0                          "
	            +"Auther:You GanQuan                   "
	            +"Date:2004/05/29                      "
	            +"Address:SWUST  ");
	al.addCommand(exitCommand);
	al.setCommandListener(this);
	display.setCurrent(al);
	}
	public void start() {
        /*线程*/
        Thread t = new Thread(this);
        t.start();
    }
	public void run()
	{
		try{
		/*服务器常驻5000端口*/
		scn = (ServerSocketConnection) Connector.open("socket://:8000");
        /*控制与前来请求的客户端建立连接*/
            sc = (SocketConnection) scn.acceptAndOpen();
	       /*打开输入流*/
	       is = sc.openInputStream();
            /*打开输出流*/
            os = sc.openOutputStream();
	        /*以传递值开始游戏*/
	        t=new MyGameWithTiledLayerCanvas(1,os);
            t.addCommand(exitCommand1);
            t.setCommandListener(this);
            display.setCurrent(t);
	        t.start();
            /*不停地对数据进行接收并解析之,直到游戏结束*/
            while(true)
            {
              /*将字节流转化为数据流*/
              DataInputStream dis = new DataInputStream(is) ;
                String sb="";
                int c=0;
                /*对数据的一次读入*/
                while (((c = is.read()) != '\n') && (c != -1)) {
                    sb=sb+(char)c;
                }
                if (c == -1) {                    
                    break;
                }
                /*数据解析还原*/
                String sb1="",sb2="",sb3="",sb4="";
                int size=sb.indexOf(":");
                int size1=sb.indexOf(";");
                int size2=sb.indexOf(",");
                for(int i=0;i<size;i++)
                sb1=sb1+sb.charAt(i);            	
            	for(int i=size+1;i<size1;i++)
            	sb2=sb2+sb.charAt(i);
            	for(int i=size1+1;i<size2;i++)
            	sb3=sb3+sb.charAt(i);
            	for(int i=size2+1;i<sb.length();i++)
            	sb4=sb4+sb.charAt(i);
            	Integer x1 = Integer.valueOf(sb1);
            	int x2 = x1.intValue();
            	x1=Integer.valueOf(sb2);
            	int y2=x1.intValue();
                 x1 = Integer.valueOf(sb3);
            	int z2 = x1.intValue();
                x1 = Integer.valueOf(sb4);
            	int w2 = x1.intValue();
               /*设置游戏控制部分的某些值以实现画面的互动*/
                t.ex=x2;
                t.ey=y2;
                t.state1=w2;
                /*如果客户端的某个位置安了泡泡,服务器端也要安*/
                if(z2!=-1)
                {Bomb bomb=new Bomb(z2,t);
                	bomb.start();
                }
            } 
           /*当游戏结束,对相应资源的回收*/
           t.exit();
           stop();
           parent.notifyDestroyed();
    } 
    catch (IOException ioe) {

                if (!stop) {
                    ioe.printStackTrace();
                }
            }
 catch (Exception e) {
            e.printStackTrace();
        }
    }        
        /*对一些按纽事件的处理*/
         public void commandAction(Command c, Displayable s) {
         	String cmd =c.getLabel();
         	if(cmd.equals("Exit")){
         	parent.notifyDestroyed();
            parent.destroyApp(true);
        }
        else if(cmd.equals("结束")){
           t.exit();
           stop();
           parent.notifyDestroyed();
        }
    }
    	/*资源的回收*/
    	public void stop() {
        try {
            stop = true;

            if (is != null) {
                is.close();
            }

            if (os != null) {
                os.close();
            }

            if (sc != null) {
                sc.close();
            }

            if (scn != null) {
                scn.close();
            }
        } catch (IOException ioe) {}
    }
}	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -