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

📄 server.java

📁 J2ME的游戏原代码!希望能帮助有需要帮助的师兄弟们!..
💻 JAVA
字号:
/*
 * 服务器类 作者:肖昶
 */
package fivegame;

import java.io.*;
import javax.bluetooth.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;

public class Server extends Alert implements CommandListener, Runnable {

	private Command backCmd;// 返回命令
	private static Command yesCmd;// 接受连入命令
	private static Command noCmd;// 拒绝连入命令
	private boolean serviceOK;// 服务是否准备好
	private Gauge gauge;// 滚动条提示

	public static final UUID serviceUUID = new UUID(
			"da822f1b1b4d40c397c36286c753de77", false);// 服务唯一标识
	public static final String serviceName = new String("FiveGameBluetooth");// 服务名称
	public StreamConnection conn = null;// 连接流
	public StreamConnectionNotifier scn = null;// 连接通告
	public DataInputStream dis = null;// 输入流
	public DataOutputStream dos = null;// 输出流
	private LocalDevice localDevice = null;// 本地设备
	public static Server instance = null;// 本类实例

	// 构造函数
	Server() {
		super("服务端");
		backCmd = new Command("返回", 3, 1);
		yesCmd = new Command("是", 4, 1);
		noCmd = new Command("否", 3, 1);
		gauge = new Gauge(null, false, -1, 2);
		addCommand(backCmd);
		setTimeout(Alert.FOREVER);
		setCommandListener(this);
	}

	// 开启服务并等待连接
	public void run() {
		serviceOK = false;// 服务未就绪
		try {
			localDevice = LocalDevice.getLocalDevice();// 获取本地设备
			localDevice.setDiscoverable(DiscoveryAgent.GIAC);// 设置设备为可被发现
			StringBuffer url = new StringBuffer("btspp://localhost:");// 设备地址
			url.append(serviceUUID.toString()).append(";name=").append(
					serviceName).append(";authorize=false");
			scn = (StreamConnectionNotifier) Connector.open(url.toString());// 创建Notifier
			serviceOK = true;// 服务已就绪
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (serviceOK) {// 如果服务就绪
			setString(" 设备初始化成功,等待连接...");
			setIndicator(gauge);// 滚动条提示
		} else {// 如果服务未就绪
			// removeCommand(backCmd);
			setString(" 设备初始化失败,退出");
			return;
		}
		try {
			conn = scn.acceptAndOpen();// 监听连接请求,在未接收到连接情求时在此阻塞
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		try {
			setIndicator(null);// 移除滚动条提示
			RemoteDevice rd = RemoteDevice.getRemoteDevice(conn);// 从连接流中获取连入设备
			String name = rd.getFriendlyName(false);// 连入设备名
			StringBuffer sb = new StringBuffer("设备:<");
			sb.append(name).append(">已连接,是否开始?\n");
			setString(sb.toString());// 提示连接
			removeCommand(backCmd);// 移除返回命令
			addCommand(yesCmd);// 接受连入的命令
			addCommand(noCmd);// 拒绝连入的命令
			dos = conn.openDataOutputStream();// 打开输出流
			dis = conn.openDataInputStream();// 打开输入流
			conn.close();
			System.out.println("连接已经关闭");
			scn.close();// 关闭服务
			System.out.println("服务已经关闭");
			localDevice.setDiscoverable(0);// 设置设备为不可被发现
			System.out.println("设备不可见");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 命令动作响应, CommandListener接口必须实现的方法
	public void commandAction(Command cmd, Displayable display) {
		if (cmd == backCmd) {// 返回命令
			try {
				if (scn != null)
					scn.close();// 关闭服务,此时acceptAndOpen()将捕捉到异常
				System.out.println("服务已经关闭");
				localDevice.setDiscoverable(0);// 设置设备为不可被发现
			} catch (Exception e) {
				e.printStackTrace();
			}
			instance = null;
			System.gc();
			FiveGame.display.setCurrent(SelectList.getInstance());
		} else if (cmd == yesCmd) {// 接受连入
			try {
				dos.writeBoolean(true);// 通知客户端服务器已经接受接入
				dos.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
			ConnectionControler.initConnCtrl(dis, dos);// 初始化通讯控制器
			System.gc();
			FiveGame.display.setCurrent(MainGame.getInstance(true));// 切换至游戏画面
		} else if (cmd == noCmd) {// 拒绝连入
			try {
				dos.writeBoolean(false);// 通知客户端服务器拒绝接受接入
				dos.flush();
				dos.close();
				System.out.println("输出流已经关闭");
				dis.close();
				System.out.println("输入流已经关闭");
			} catch (IOException e) {
				e.printStackTrace();
			}
			instance = null;
			System.gc();
			FiveGame.display.setCurrent(SelectList.getInstance());
		}
	}

	// 返回本类的实例
	public static final Server getInstance() {
		if (instance == null)
			instance = new Server();
		(new Thread(instance)).start();
		return instance;
	}
}

⌨️ 快捷键说明

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