connectioncontroler.java

来自「有关手机游戏网络对战方面的源码」· Java 代码 · 共 104 行

JAVA
104
字号
package unicoco;

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

import javax.microedition.io.StreamConnection;

public class ConnectionControler {
	// 发送棋子的坐标
	static DataInputStream dis;

	static DataOutputStream dos;

	static StreamConnection conn;

	public final static void iniConnCtrl(DataInputStream dis,
			DataOutputStream dos, StreamConnection conn) {
		ConnectionControler.dis = dis;
		ConnectionControler.dos = dos;
		ConnectionControler.conn = conn;
	}

	public final static void sendChess(byte x, byte y) {
		try {
			dos.writeByte(99);
			dos.writeByte(x);
			dos.writeByte(y);
			dos.flush();
		} catch (Exception e) {
			MIDGame.game.notifyDestroyed();
		}
	}

	public final static void sendUserInfo(String userName, short[] wfs)
			throws IOException {
		dos.writeUTF(userName);
		dos.writeShort(wfs[0]);
		dos.writeShort(wfs[1]);
		dos.flush();
	}

	public final static byte readMessageType() throws IOException, EOFException,
			InterruptedIOException {
		byte messageType = 0;
		messageType = dis.readByte();
		return messageType;
	}

	public final static byte[] readChess() {
		byte[] chess = new byte[2];
		try {
			chess[0] = dis.readByte();
			chess[1] = dis.readByte();
		} catch (Exception e) {
			MIDGame.game.notifyDestroyed();
		}
		return chess;
	}

	public final static String readMessage() {
		String message = null;
		try {
			message = dis.readUTF();
		} catch (Exception e) {
			MIDGame.game.notifyDestroyed();
		}
		return message;
	}

	public final static void sendMessage(String ms) {
		try {
			dos.writeByte(98);
			dos.writeUTF(ms);
			dos.flush();
		} catch (Exception e) {
			MIDGame.game.notifyDestroyed();
		}
	}

	public final static String readUserName() {
		String name = null;
		try {
			name = dis.readUTF();
		} catch (IOException e) {
//			e.printStackTrace();
		}
		return name;
	}

	public final static short[] readUserWF() {
		short[] test = new short[2];
		try {
			test[0] = dis.readShort();
			test[1] = dis.readShort();
		} catch (Exception e) {
			MIDGame.game.notifyDestroyed();
		}
		return test;
	}
}

⌨️ 快捷键说明

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