当前位置:编程学习 > JAVA >>

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

   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();
       &nb
补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,