package com.ccs.thread;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.util.Log;
import android.widget.Toast;
import com.ccs.bean.Message;
import com.ccs.remote_client.LoginActivity;
public class ClientThread implements Runnable {
public Socket socket;
public ObjectInputStream inputStream;
public ObjectOutputStream outputStream;
final int LOGIN = 0;// 登陆
final int ON_LOGIN = 1;// 登陆成功
final int FALSE_LOGIN = 2;// 登陆失败
final int LOGOUT = 3;// 玩家下线
final int TURN_UP = 4;// 向上
final int TURN_DOWN = 5;// 向下
final int TURN_LEFT = 6;// 向左
final int TURN_RIGHT = 7;// 向右
final int START = 8;// 开始玩
final int PAUSE = 9;// 暂停玩
final int win = 10;// 游戏赢了
final int KICK_OUT = 11;// 强制下线
final int SERVICE_CLOSE = 12; // 服务器退出
String ip;
LoginActivity loginActivity;
String player_name;
public ClientThread(String ip, LoginActivity loginActivity, String player_name ) {
this.ip=ip;
this.loginActivity=loginActivity;
this.player_name=player_name;
}
public void connect() {
try {
socket = new Socket(ip, 3000);
//设置read超时
socket.setSoTimeout(1000);
outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(loginActivity,"连接失败,请检查配置!!", Toast.LENGTH_SHORT).show();
}
}
//发送登陆请求
public boolean isLogin() throws OptionalDataException, ClassNotFoundException, IOException{
Message msg = new Message();
msg.setMsgType(LOGIN);
msg.setMsgPlayer_name(player_name);
sendMsg(msg);
if(readMsg().getMsgType()==ON_LOGIN){
return true;
}else{
return false;
}
}
public void sendMsg(Message message) {
try {
outputStream.writeObject(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public Message readMsg() throws OptionalDataException, ClassNotFoundException, IOException {
return (Message) inputStream.readObject();
}
@Override
public void run() {
connect();
while (true) {
try {
switch (readMsg().getMsgType()) {
// 登陆
case LOGIN:
break;
// 登陆成功
case ON_LOGIN:
break;
// 登陆失败
case FALSE_LOGIN:
break;
// 玩家下线
case LOGOUT:
break;
//向上
case TURN_UP:
break;
//向下
case TURN_DOWN:
break;
// 向左
case TURN_LEFT:
break;
// 向右
case TURN_RIGHT:
break;
// 开始玩
case START:
break;
// 游戏赢了
case win:
break;
// 强制下线
case KICK_OUT:
break;
// 暂停玩
case PAUSE:
break;
}
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
import android.util.Log;
import android.widget.Toast;
import com.ccs.bean.Message;
import com.ccs.remote_client.LoginActivity;
public class ClientThread implements Runnable {
public Socket socket;
public ObjectInputStream inputStream;
public ObjectOutputStream outputStream;
final int LOGIN = 0;// 登陆
final int ON_LOGIN = 1;// 登陆成功
final int FALSE_LOGIN = 2;// 登陆失败
final int LOGOUT = 3;// 玩家下线
final int TURN_UP = 4;// 向上
final int TURN_DOWN = 5;// 向下
final int TURN_LEFT = 6;// 向左
final int TURN_RIGHT = 7;// 向右
final int START = 8;// 开始玩
final int PAUSE = 9;// 暂停玩
final int win = 10;// 游戏赢了
final int KICK_OUT = 11;// 强制下线
final int SERVICE_CLOSE = 12; // 服务器退出
String ip;
LoginActivity loginActivity;
String player_name;
public ClientThread(String ip, LoginActivity loginActivity, String player_name ) {
this.ip=ip;
this.loginActivity=loginActivity;
this.player_name=player_name;
}
public void connect() {
try {
socket = new Socket(ip, 3000);
//设置read超时
socket.setSoTimeout(1000);
outputStream = new ObjectOutputStream(socket.getOutputStream());
inputStream = new ObjectInputStream(socket.getInputStream());
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(loginActivity,"连接失败,请检查配置!!", Toast.LENGTH_SHORT).show();
}
}
//发送登陆请求
public boolean isLogin() throws OptionalDataException, ClassNotFoundException, IOException{
Message msg = new Message();
msg.setMsgType(LOGIN);
msg.setMsgPlayer_name(player_name);
sendMsg(msg);
if(readMsg().getMsgType()==ON_LOGIN){
return true;
}else{
return false;
}
}
public void sendMsg(Message message) {
try {
outputStream.writeObject(message);
} catch (IOException e) {
e.printStackTrace();
}
}
public Message readMsg() throws OptionalDataException, ClassNotFoundException, IOException {
return (Message) inputStream.readObject();
}
@Override
public void run() {
connect();
while (true) {
try {
switch (readMsg().getMsgType()) {
// 登陆
case LOGIN:
break;
// 登陆成功
case ON_LOGIN:
break;
// 登陆失败
case FALSE_LOGIN:
break;
// 玩家下线
case LOGOUT:
break;
//向上
case TURN_UP:
break;
//向下
case TURN_DOWN:
break;
// 向左
case TURN_LEFT:
break;
// 向右
case TURN_RIGHT:
break;
// 开始玩
case START:
break;
// 游戏赢了
case win:
break;
// 强制下线
case KICK_OUT:
break;
// 暂停玩
case PAUSE:
break;
}
} catch (OptionalDataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}