Cocos2dX通过Java服务器向Unity传输数据四

来源:互联网 发布:软件开发课程设计 编辑:程序博客网 时间:2024/05/16 11:23

Unity3D相关代码:

using UnityEngine;using System;using System.Net;using System.Net.Sockets;using System.Collections;using System.Text;public class UnitySocket  {public  Socket mSocket=null;    public UnitySocket()    {            }    public  void Connect(string IP,int localPort)    {        mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);        try        {            IPAddress ip = IPAddress.Parse(IP);            IPEndPoint ipe = new IPEndPoint(ip, localPort);            //---------------------m1-----------------------            //mSocket.Connect(ipe);            //---------------------m2------------------------            mSocket.BeginConnect(ipe,new AsyncCallback(Connected),null);        }        catch(Exception e)        {            e.GetType();        }    }    void Connected(IAsyncResult iar)    {        mSocket.EndConnect(iar);    }    bool ReceiveFlag = true;    public  byte[] readData = new byte[1024];   public void startReceive()    {        if (ReceiveFlag)        {            ReceiveFlag = false;            mSocket.BeginReceive(readData,0,readData.Length,SocketFlags.None,new AsyncCallback(endReceive),mSocket);        }    }   public void endReceive(IAsyncResult iar)   {       ReceiveFlag = true;       Socket remote = (Socket)iar.AsyncState;       int recv = remote.EndReceive(iar);       if (recv > 0)       {       }   }   public void SendMessage(int rotation)   {       byte[] msg = BitConverter.GetBytes(rotation);       mSocket.BeginSend(msg,0,msg.Length,SocketFlags.None,new AsyncCallback(SendData),mSocket);   }   public void SendMessage(string data)   {       byte[] msg = Encoding.UTF8.GetBytes(data);       mSocket.BeginSend(msg,0,msg.Length,SocketFlags.None,new AsyncCallback(SendData),mSocket);   }   void SendData(IAsyncResult iar)   {       Socket remote = (Socket)iar.AsyncState;       int sent = remote.EndSend(iar);   }    public  void Send(int data)    {        byte[] longth = BitConverter.GetBytes(data);        mSocket.Send(longth);    }    public  void Send(float data)    {        byte[] longth = BitConverter.GetBytes(data);        mSocket.Send(longth);    }    public  void Send(string data)    {        byte[] longth = Encoding.UTF8.GetBytes(data);        mSocket.Send(longth);    }    public  int ReceiveInt()    {        byte[] recvBytes = new byte[4];        mSocket.Receive(recvBytes, 4, 0);        int data=BitConverter.ToInt32(recvBytes,0);        return data;    }    public double ReceiveDouble()    {        byte[] recvBytes=new byte[8];        mSocket.Receive(recvBytes, 4, 0);        double data = BitConverter.ToDouble(recvBytes, 0);        return data;    }    public string ReceiveString(int length)    {        byte[] recvBytes = new byte[length];        mSocket.Receive(recvBytes, length, 0);        string data= Encoding.UTF8.GetString(recvBytes);        return data;    }}

using UnityEngine;using System.Collections;using UnityNetwork;using System;public class BoxManager : NetworkManager{    NetTCPClient client = null;    UnitySocket socket = null;    int ra = 0;    int flag = 0;    public void Start()    {        //client = new NetTCPClient();        //client.Connect("172.27.35.1", 59422);        socket = new UnitySocket();        socket.Connect("172.27.35.1",59422);        //BoxMove.Instance.yAngle = socket.ReceiveInt();        //socket.Send(3);        //socket.Send("sss");        //int a = socket.ReceiveInt();        //Debug.Log(a);        //int b = socket.ReceiveInt();        //int c = socket.ReceiveInt();                //Debug.Log(b);        //Debug.Log(c);    }    public void Send(NetBitStream stream)    {        client.Send(stream);    }    public override void Update()    {        base.Update();        socket.SendMessage(5);        socket.SendMessage("UNITY");        socket.startReceive();        ra = BitConverter.ToInt32(socket.readData, 0);        //if (socket.ReceiveString(8).StartsWith("Rotation")) ra = socket.ReceiveInt();        BoxMove.Instance.yAngle = ra;        //flag += 1;        //if (flag > 90)        //{        //    flag = 0;        //}             Debug.Log(ra);    }}

using UnityEngine;using System.Collections;using UnityNetwork;public class BoxMove : MonoBehaviour {    public static BoxMove Instance = null;    BoxManager _boxMgr;    Vector3 s = new Vector3(1,0,0);    public float yAngle=0.0f;// Use this for initializationvoid Start () {        DontDestroyOnLoad(this);        Instance = this;        _boxMgr = new BoxManager();        _boxMgr.Start();}// Update is called once per framevoid Update () {        _boxMgr.Update();        //yAngle += 1.0f;        transform.localRotation = Quaternion.AngleAxis(yAngle, s);}}


0 0
原创粉丝点击