首页 > 代码库 > Libgdx: android单机斗地主支持局域网wifi联网的网络模块核心代码

Libgdx: android单机斗地主支持局域网wifi联网的网络模块核心代码


这个作品是我最近写的,结合我的毕业设计的通信模块和之前的单机版斗地主。我已经上架到豌豆荚了,贴了点广告,看看能不能赚点茶钱。可是一点也不乐观。因此我想分享给大家源码。只要不用于商业。 下面先贴网络模块的核心代码,第一次写这种逻辑用的udp, 经验不够,没有写的那么好看。

这里是我上架的apk,大家下载来试试也无妨: 地址 http://www.wandoujia.com/apps/com.hj.joker

package com.hj.net;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import org.apache.http.conn.util.InetAddressUtils;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.hj.screen.NetGameScreen;
import com.hj.screen.RommScreen;
import com.hj.tool.Card;
import com.hj.tool.Comm;

import android.os.Message;
import android.util.Log;

public class NetManager {

	// 初始化
	public RommScreen rommScreen = null;
	public NetGameScreen netGameScreen = null;
	public static int Max = 3;
	public User me = null, u = null, computer = null;
	public List<User> users = null;
	public Room currentRoom = null;
	public List<Room> roomList = null;// 本主机的所有用户
	public boolean isStop = true;
	public boolean LeaveRoom = false;
	public CardData cards[] = new CardData[56];
	public List<CardData> cardDataList = new ArrayList<CardData>();
	// 协议命令
	public static final int CMD_HOST_REQUEST_ROOM = 10;
	public static final int CMD_REPLY_ROOM = 11;
	public static final int CMD_CREATE_ROOM = 12;
	public static final int MES_UPDATE_ROOM = 13;
	public static final int MES_UPDATE_User = 14;
	public static final int CMD_HOST_JOIN_ROOM = 15;
	public static final int CMD_BROAD_USERLIST = 16;
	public static final int CMD_UPDATE_ROOM = 17;
	public static final int CMD_HOST_LEAVE_ROOM = 18;
	public static final int CMD_LEAVE_ROOM = 19;
	public static final int CMD_DEL_ROOM = 20;
	public static final int CMD_HOST_READY_ROOM = 21;
	public static final int CMD_BEGIN_CARDS = 22;
	public static final int MES_UPDATE_BEGINCARD = 23;
	public static final int MES_UPDATE_LANDLORD = 24;
	public static final int CMD_HOST_FINISH_LANDLORD = 25;
	public static final int CMD_BROAD_NEXT_LANDLORD = 26;
	public static final int CMD_BEGIN_LANDLORD_CARDS = 27;
	public static final int CMD_HOST_START_CARDS = 28;
	public static final int CMD_START_CARDS = 29;
	public static final int MES_SHOW_CARDBUTTON = 30;
	public static final int MES_UPDATE_LANDLORDHEAD = 31;
	public static final int CMD_HOST_SEND_CARDS = 32;
	public static final int CMD_SEND_CARDS = 33;
	public static final int MES_SEND_CARDS = 34;
	public static final int CMD_SEND_CURRENTID_CARDS = 35;
	public static final int MES_FLUSH_CARDS = 36;
	public static final int CMD_HOST_SEND_CARDS_COMPUTER = 37;
	public static final int PORT_SEND = 2429;// 发送端口
	public static final int PORT_RECEIVE = 2425;// 接收端口

	public NetManager() {
		roomList = new ArrayList<Room>();
		users = new ArrayList<User>();
	}

	public void init() {
		users.clear();
		cardDataList.clear();
		for (int i = 0; i < 3; i++) {
			netGameScreen.gInfo.playerList[i].clear();
			netGameScreen.gInfo.playerOutList[i].clear();
		}
		netGameScreen.gInfo.playerList[3].clear();
		// users
	}

	// 发送消息
	public void sendCMD(Msg msg) {
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
		}
		(new UdpSend(msg)).start();
	}

	class UdpSend extends Thread {
		Msg msg = null;

		UdpSend(Msg msg) {
			this.msg = msg;
		}

		public void run() {
			try {
				byte[] data = http://www.mamicode.com/toByteArray(msg);>

如果想要全部源码的,可以 email 我 cq361106306@qq.com. 但是只能研究所用

PS:由于博客中毒我把所有文章都删了才解封。。