cocos2d-x网游之java服务器篇1

来源:未知 责任编辑:智问网络 发表时间:2013-08-28 11:57 点击:
   cocos2d-x网游之java服务器篇
    cocos2d-x是c++开发与java通信要使用inputStream和outputStream进行交互
提供一个简单的多线程服务器,有三个文件。
  
/**
 * @(#)ServerTank.java
 *
 *
 * @author buger
 * @version 1.00 2012/8/30
 */
 
 
import java.util.*;
import java.io.*;
import java.net.*;
 
public class ServerTank
{
    public static void main(String args[])
    {
        new ServerAgentThread().start();
    }
}
 
 
 
/**
 * @(#)ServerThread.java
 *
 *
 * @author  buger
 * @version 1.00 2012/8/31
 */
 
//package wyf.jsc;
 
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class ServerThread extends Thread{
    Socket sc;
    InputStream is;
    OutputStream os;
    boolean flag=true;//循环控制标志位
    int id=0;//第几玩家标志位 0-初始 1-第一玩家 2-第二玩家
    ServerAgentThread father;
 
    public ServerThread(ServerAgentThread father,Socket sc,int id)
    {
        this.father=father;
        this.sc=sc;
        this.id=id;
        try
        {
            is=sc.getInputStream();
            os=sc.getOutputStream();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public void run()
    {
        while(flag)
        {
            try{
                //System.out.println("Server Is Starting!---------------------\n");
                byte[] bytes = new byte[1024];
                int n = is.read(bytes);
                String c_str=new String(bytes,0,n);
                System.out.println("Server Is Starting!---------------------\n");
                System.out.println("Server Is Starting!*********************\n");
                if(c_str.startsWith("#ENTER_REQUEST#"))
                {//请求进入游戏
                    if(id<=2)
                    {
                        System.out.println("id="+id);
                        //允许进入
                        os.write(("<ALLOW_ENTER>").getBytes());
                            //if()
                        father.userList[id-1]=this;
                        System.out.println("Player Loged In!---------------------------------\n");
                        if(id==2)
                        {//向两个用户发游戏开始消息
                            sendMsgTo2Users("<GAME_START>");
                            //向用户发Id消息
                            //sendMsgTo1User("<#YOU_ID#>1",1);
                            //sendMsgTo1User("<#YOU_ID#>2",2);
                        }
                    }
                    else
                    {//人数超,不让进
                        os.write(("<USER_FULL>").getBytes());
                        flag=false;
                        sc.close();
                        //din.close();
                        //dout.close();
                    }
                }
                else if(c_str.startsWith("#Tank_Up#"))
                {
                    os.write(("<Tank_Up>").getBytes());
 
                    System.out.println("Tank_Up---------------------\n");
                }
                else if(c_str.startsWith("#Tank_Down#"))
                {
                    os.write(("<Tank_Down>").getBytes());
 
                    System.out.println("Tank_Down--------------------\n");
                }
                else if(c_str.startsWith("#Tank_Left#"))
                {
                    os.write(("<Tank_Left>").getBytes());
 
                    System.out.println("Tank_Left---------------------\n");
                }
                else if(c_str.startsWith("#Tank_Right#"))
                {
                    os.write(("<Tank_Right>").getBytes());
 
                    System.out.println("Tank_Right---------------------\n");
                }
                else if(c_str.startsWith("#Send_Fire#"))
                {
                    System.out.println("Send_Fire----------------------\n");
                }
 
 
                }
                catch(Exception e)
                {
 
                    System.out.println("有人掉线了");
                    try
                    {
 
                        //sendMsgTo1User("<#ALLOW_EXIT#>",2);
                    }
                    catch(Exception ea)
                    {
 
                        ea.printStackTrace();
                    }
                    try
                    {
                         //sendMsgTo1User("<#ALLOW_EXIT#>",1);
                    }
                    catch(Exception ea)
                    {
                         ea.printStackTrace();
                    }
                    father.initState();
                    break;
            }
        }
    }
 
 
 
    //向两个玩家发同样的消息
    public void sendMsgTo2Users(String msg) throws Exception
    {
        System.out.println("给两个玩家发消息"+msg);
        for(ServerThread sat:father.userList)
        {
            sat.os.write((msg).getBytes());
        }
    }
 
    //向指定ID玩家发送消息
    public void sendMsgTo1User(String msg,int id) throws Exception
    {
        father.userList[id-1].os.write((msg).getBytes());
    }
 
 
}
这是基于我的一个游戏的部分。
 
/**
 * @(#)ServerAgentThread.java
 *
 *
 * @author buger
 * @version 1.00 2012/8/31
 */
//package wyf.jsc;
import java.sql.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import java.net.*;
public class ServerAgentThread extends Thread
{
 
    /**
     * Creates a new instance of <code>ServerAgentThread</code>.
     */
    ServerSocket ss;
    boolean flag=true;//循环控制标志位
    int ucount=0;//玩家数量 0-没有 1-1个玩家 2-两个玩家
    ServerThread[] userList=new ServerThread[2];
    int hitId=0;//0无用户 1-1号用户 2-2号用户
 
    public ServerAgentThread() {
        try
        {
            ss=new ServerSocket(5000);
            System.out.println("Server 1.0 CopyRight(c)All Rights Reserved Playerobe Studios!");
            System.out.println("TankServer服务器以启动");
            System.out.println("正在监听在5555端口.....");
        }catch(Exception e)
        {
            e.printStackTrace();
        }
    }
 
    /**
     * @param args the command line arguments
     */
     public void initState()//服务器回初始状态
     {
        ucount=0;//玩家数量 0-没有 1-1个玩家 2-两个玩家
        userList=new ServerThread[2];
        hitId=0;//0无用户 1-1号用户 2-2号用户
     }
 
    public static void main(String[] args) {
        // TODO code application logic here
        new ServerAgentThread().start();
    }
 
   public void run()
    {
        while(flag)
        {
            try
            {
                Socket sc=ss.accept();
 
                //接到一个用户的连接请求
                System.out.println("接到一个用户的连接请求 "+sc.getInetAddress().toString());
                ucount++;
                ServerThread sat=new ServerThread(this,sc,ucount);
                sat.start();
            }catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
 
 
}
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    用户名: 验证码:点击我更换图片
    最新评论 更多>>

    推荐热点

    • cocos2d-x学习笔记(19)--label 、label atlas
    • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
    • Cocos2d-x学习(一):HelloWorld
    • cocos2dx在xcode下开发,编译到android上(2)
    • cocos2d 设置屏幕默认方向
    • Cocos2d-x 2.0 之 Actions “三板斧” 之一
    • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
    • cocos2d-x学习笔记(18)--游戏打包(windows平台)
    • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
    网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
    Copyright © 2008-2015 计算机技术学习交流网. 版权所有

    豫ICP备11007008号-1