private static final long serialVersionUID = 1L; //序列化对象
public static final String CLIENT_INDEX ="ClientIndex"; //指令值(用户唯一标识)command
public static final String GET_PLAYER_COUNT ="GetPlayerCount"; //获取玩家总数command
public static final String GET_ALL_ROOM ="GetAllRoom"; //获取房间信息command
public static final String CREATEROOM ="CreateNewRoom"; //创建新房间command
public static final String GETINROOM ="GetInRoom"; //加入指定房间command
public static final String STARTGAME ="StartGame"; //下发比赛题目command
public static final String USERTABLE ="UserTable"; //本地用户信息command
public static final String REGISTERVALID ="RegisterValid"; //用户提交注册信息command
public static final String TEST = "test";
public String LevelInfo; //题别信息
public String RoomList; //room列表
private String CommendHead; //command头
private String CommendValue; //command值
private int ClientIdx; //指令值(用户唯一标识)
private Player player; //玩家对象
private String groupArr; //存放指定级别的题号字符串
private String userInfo; //存放用户注册信息的字符串
private Room room;
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public String getLevelInfo() {
return LevelInfo;
}
public void setLevelInfo(String levelInfo) {
LevelInfo = levelInfo;
}
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
private int RoomID;
public int getRoomID() {
return RoomID;
}
public void setRoomID(int roomID) {
RoomID = roomID;
}
public String getGroupArr() {
return groupArr;
}
public void setGroupArr(String groupArr) {
this.groupArr = groupArr;
}
public Command(){
super();
}
public Command(String head,String value,int index){
super();
CommendHead = head;
CommendValue = value;
ClientIdx = index;
}
public String getRoomList() {
return RoomList;
}
public void setRoomList(String roomList) {
RoomList = roomList;
}
public String getCommendHead() {
return CommendHead;
}
public void setCommendHead(String commendHead) {
CommendHead = commendHead;
}
public String getCommendValue() {
return CommendValue;
}
public void setCommendValue(String commendValue) {
CommendValue = commendValue;
}
public int getClientIdx() {
return ClientIdx;
}
public void setClientIdx(int clientIdx) {
ClientIdx = clientIdx;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
}
Player类
package com.wsst.common;
import java.io.Serializable;
public class Player implements Serializable {
private static final long serialVersionUID = 1L; //序列化对象
private String name; //玩家账号昵称
private String ipAdress; //玩家IP
private int clientId; //玩家指令值
private int roomID; //玩家所在的房间号
private String totalScores; //玩家游戏得分
private String selectedItem; //玩家某项题目所选答案
public Player(int clientIndex) {
this.clientId = clientIndex;
}
public String getIPAdress() {
return ipAdress;
}
public void setIPAdress(String iPAdress) {
this.ipAdress = iPAdress;
}
public int getClientIdx() {
return clientId;
}
public void setClientIdx(int clientIdx) {
this.clientId = clientIdx;
}
public int getRoomID() {
return roomID;
}
public void setRoomID(int roomID) {
this.roomID = roomID;
}
public String getName() {
return name;
}
public void setName(String Name) {
this.name = Name;
}
public String getTotalScores() {
return totalScores;
}
public void setTotalScores(String TotalScores) {
this.totalScores = TotalScores;
}
public String getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(String SelectedItem) {
this.selectedItem = SelectedItem;
}
}
Socket传送部分代码
// 接收
public void run() {
try {
while ((mCommend = (Command) mInputStream.readObject()) != null{}
}catch。。。。。
传送跟接收的代码一样一样
int len = buildShort(bytes2[0], bytes2[1]);
byte[] recvData = new byte[len];
int totalCount = 0;
int readCount = 0;
while (totalCount < len ) {
readCount = inputStream.read(recvData, totalCount, len - totalCount);
if (readCount == -1)
throw new BaseException(GlobalConstant.ERROR_CODE_40032);
totalCount = readCount + totalCount;
}
#15
你先想着怎么解决问题。给你个例子吧
int len = buildShort(bytes2[0], bytes2[1]);
byte[] recvData = new byte[len];
int totalCount = 0;
int readCount = 0;
while (totalCount < len ) {
readCount = inputStream.read(recvData, totalCount, len - totalCount);
if (readCount == -1)
throw new BaseException(GlobalConstant.ERROR_CODE_40032);
totalCount = readCount + totalCount;
}
我理解上面例子的含义是:一直读取输入流【while (totalCount < len )】,直到不满足条件。我想问一下,(Command) mInputStream.readObject()这个是不是也应该有方法不断读取里面的对象,我想找到其中某个匹配的Command?我试了一下,while (mInputStream.readObject().equals(true))这样问题好像更多了,异常还是会出现,希望帮忙指导一下。
private static final long serialVersionUID = 1L; //序列化对象
public static final String CLIENT_INDEX ="ClientIndex"; //指令值(用户唯一标识)command
public static final String GET_PLAYER_COUNT ="GetPlayerCount"; //获取玩家总数command
public static final String GET_ALL_ROOM ="GetAllRoom"; //获取房间信息command
public static final String CREATEROOM ="CreateNewRoom"; //创建新房间command
public static final String GETINROOM ="GetInRoom"; //加入指定房间command
public static final String STARTGAME ="StartGame"; //下发比赛题目command
public static final String USERTABLE ="UserTable"; //本地用户信息command
public static final String REGISTERVALID ="RegisterValid"; //用户提交注册信息command
public static final String TEST = "test";
public String LevelInfo; //题别信息
public String RoomList; //room列表
private String CommendHead; //command头
private String CommendValue; //command值
private int ClientIdx; //指令值(用户唯一标识)
private Player player; //玩家对象
private String groupArr; //存放指定级别的题号字符串
private String userInfo; //存放用户注册信息的字符串
private Room room;
public Room getRoom() {
return room;
}
public void setRoom(Room room) {
this.room = room;
}
public String getLevelInfo() {
return LevelInfo;
}
public void setLevelInfo(String levelInfo) {
LevelInfo = levelInfo;
}
public String getUserInfo() {
return userInfo;
}
public void setUserInfo(String userInfo) {
this.userInfo = userInfo;
}
private int RoomID;
public int getRoomID() {
return RoomID;
}
public void setRoomID(int roomID) {
RoomID = roomID;
}
public String getGroupArr() {
return groupArr;
}
public void setGroupArr(String groupArr) {
this.groupArr = groupArr;
}
public Command(){
super();
}
public Command(String head,String value,int index){
super();
CommendHead = head;
CommendValue = value;
ClientIdx = index;
}
public String getRoomList() {
return RoomList;
}
public void setRoomList(String roomList) {
RoomList = roomList;
}
public String getCommendHead() {
return CommendHead;
}
public void setCommendHead(String commendHead) {
CommendHead = commendHead;
}
public String getCommendValue() {
return CommendValue;
}
public void setCommendValue(String commendValue) {
CommendValue = commendValue;
}
public int getClientIdx() {
return ClientIdx;
}
public void setClientIdx(int clientIdx) {
ClientIdx = clientIdx;
}
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
}
Player类
package com.wsst.common;
import java.io.Serializable;
public class Player implements Serializable {
private static final long serialVersionUID = 1L; //序列化对象
private String name; //玩家账号昵称
private String ipAdress; //玩家IP
private int clientId; //玩家指令值
private int roomID; //玩家所在的房间号
private String totalScores; //玩家游戏得分
private String selectedItem; //玩家某项题目所选答案
public Player(int clientIndex) {
this.clientId = clientIndex;
}
public String getIPAdress() {
return ipAdress;
}
public void setIPAdress(String iPAdress) {
this.ipAdress = iPAdress;
}
public int getClientIdx() {
return clientId;
}
public void setClientIdx(int clientIdx) {
this.clientId = clientIdx;
}
public int getRoomID() {
return roomID;
}
public void setRoomID(int roomID) {
this.roomID = roomID;
}
public String getName() {
return name;
}
public void setName(String Name) {
this.name = Name;
}
public String getTotalScores() {
return totalScores;
}
public void setTotalScores(String TotalScores) {
this.totalScores = TotalScores;
}
public String getSelectedItem() {
return selectedItem;
}
public void setSelectedItem(String SelectedItem) {
this.selectedItem = SelectedItem;
}
}
Socket传送部分代码
// 接收
public void run() {
try {
while ((mCommend = (Command) mInputStream.readObject()) != null{}
}catch。。。。。
传送跟接收的代码一样一样
int len = buildShort(bytes2[0], bytes2[1]);
byte[] recvData = new byte[len];
int totalCount = 0;
int readCount = 0;
while (totalCount < len ) {
readCount = inputStream.read(recvData, totalCount, len - totalCount);
if (readCount == -1)
throw new BaseException(GlobalConstant.ERROR_CODE_40032);
totalCount = readCount + totalCount;
}
#15
你先想着怎么解决问题。给你个例子吧
int len = buildShort(bytes2[0], bytes2[1]);
byte[] recvData = new byte[len];
int totalCount = 0;
int readCount = 0;
while (totalCount < len ) {
readCount = inputStream.read(recvData, totalCount, len - totalCount);
if (readCount == -1)
throw new BaseException(GlobalConstant.ERROR_CODE_40032);
totalCount = readCount + totalCount;
}
我理解上面例子的含义是:一直读取输入流【while (totalCount < len )】,直到不满足条件。我想问一下,(Command) mInputStream.readObject()这个是不是也应该有方法不断读取里面的对象,我想找到其中某个匹配的Command?我试了一下,while (mInputStream.readObject().equals(true))这样问题好像更多了,异常还是会出现,希望帮忙指导一下。