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

📄 server.java

📁 有关手机游戏网络对战方面的源码
💻 JAVA
字号:
package unicoco;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Gauge;

public class Server extends Alert implements CommandListener, Runnable {
	static Server instance;

	public final static UUID serviceUUID = new UUID(
			"da822f1b1b4d40c397c36286c753de77", false);

	public final static String serviceName = new String("BluetoothGobang");

	StreamConnection conn;

	Command yes = new Command("yes", Command.OK, 1);

	Command no = new Command("no", Command.CANCEL, 1);

	Command cancel = new Command("Cancel", Command.CANCEL, 1);

	DataInputStream dis;

	DataOutputStream dos;

	Server() {
		super("Server");
		addCommand(cancel);
		setCommandListener(this);
		setTimeout(Alert.FOREVER);
	}

	Gauge gauge = new Gauge(null, false, Gauge.INDEFINITE,
			Gauge.CONTINUOUS_RUNNING);

	boolean serviceClosed;

	StreamConnectionNotifier scn;

	public void run() {
		try {
			LocalDevice.getLocalDevice().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());
			setString(" Initial success,wait for client connection...");
			setIndicator(gauge);
		} catch (BluetoothStateException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		while (!serviceClosed) {
			// 等待Client连接的标志
			wait = true;
			try {
				conn = scn.acceptAndOpen();
			} catch (IOException e) {
				// 关闭了scn会在这里收到异常
//				e.printStackTrace();
				return;
			}
			try {
				setIndicator(null);
				RemoteDevice rd = RemoteDevice.getRemoteDevice(conn);
				String name = rd.getFriendlyName(false);
				StringBuffer sb = new StringBuffer("Device:");
				sb.append(name).append(" connected,play game with it?\n");
				setString(sb.toString());
				removeCommand(cancel);
				addCommand(yes);
				addCommand(no);
				dos = conn.openDataOutputStream();
				dis = conn.openDataInputStream();
				serviceClosed = true;
				LocalDevice.getLocalDevice().setDiscoverable(
						DiscoveryAgent.NOT_DISCOVERABLE);
				while (wait) {

				}
			} catch (IOException e) {
//				e.printStackTrace();
			}

		}
	} // 等待用户选择是否允许连结

	boolean wait = true;

	public void commandAction(Command c, Displayable d) {

		if (c.getLabel().equalsIgnoreCase("Cancel")) {
			removeCommand(yes);
			removeCommand(no);
			setIndicator(null);
			//结束run中的几个嵌套的while循环
			serviceClosed = true;
			wait = false;
			if (conn != null) {
				try {
					conn.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
//					e.printStackTrace();
				}
			}
			try {
				scn.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				// 直接返回
//				e1.printStackTrace();
			}
			try {
				LocalDevice.getLocalDevice().setDiscoverable(
						DiscoveryAgent.NOT_DISCOVERABLE);
			} catch (BluetoothStateException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
			}
			MIDGame.display.setCurrent(SelectList.getInstance());
		} else if (c.getLabel().equalsIgnoreCase("yes")) {
			try {
//				 结束等待的空循环
				wait = false;
				scn.close();
				dos.writeBoolean(true);
				dos.flush();
				ConnectionControler.iniConnCtrl(dis, dos, conn);
				SelectList.instance=null;
				System.gc();
				MIDGame.display.setCurrent(GameMainCanvas.getInstance(true));
			} catch (Exception e) {
//				 TODO Client选择连接后,等待Server相应时,退出的处理方法
//				e.printStackTrace();
				Alert exitInfo = new Alert("Infomation", null, null,
						AlertType.WARNING);
				StringBuffer sb = new StringBuffer(' ');
				sb.append("Client was exit,the connection is disconnected...");
				exitInfo.setTimeout(Alert.FOREVER);
				exitInfo.setString(sb.toString());
				MIDGame.display.setCurrent(exitInfo, getInstance());
				return;
			}

		} else if (c.getLabel().equalsIgnoreCase("no")) {
			removeCommand(yes);
			removeCommand(no);
			addCommand(cancel);
			serviceClosed = false;
			try {
				LocalDevice.getLocalDevice().setDiscoverable(
						DiscoveryAgent.GIAC);
			} catch (BluetoothStateException e1) {
				// TODO Auto-generated catch block
//				e1.printStackTrace();
			}
			try {
				dos.writeBoolean(false);
				dos.close();
				dis.close();
				conn.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
//				e.printStackTrace();
			}
			setString("Wait for client connection...\n");
			setIndicator(gauge);
			wait = false;
		}
	}

	public final static Server getInstance() {
		if (instance == null) {
			instance = new Server();
		}
		instance.serviceClosed = false;
		instance.wait = true;
		new Thread(instance).start();
		return instance;
	}
}

⌨️ 快捷键说明

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