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
- Cocos2dX通过Java服务器向Unity传输数据四
- Cocos2dX通过Java服务器向Unity传输数据一
- Cocos2dX通过Java服务器向Unity传输数据二
- Cocos2dX通过Java服务器向Unity传输数据三
- 通过svn来向服务器传输文件
- java服务器与底层硬件通过GPRS传输数据
- Unity3D与JSP TomCat服务器传递数据和文件( 二 ) Unity3D向java传输表单
- 安卓连接服务器,并且向服务器传输JSON数据
- 通过nsurlsession向服务器传送json数据
- Android客户端向Python服务器以POST方式传输数据
- java客户端运用http向服务器批量传输文件
- 通过libcurl向服务器发送数据,返回的数据说明
- java客户端向服务器发送数据
- Android客户端通过GET和POST向服务器发送数据
- 以Gzip的形式通过服务器向客户端发送数据
- 通过httpclient向服务器传送数据并获得返回值
- Android之通过HTTP协议向服务器发送XML数据
- 通过 httpclientget 方法 向服务器中请求数据
- 银行卡,IC卡,接触/非接触卡,CPU卡,NFC小结
- 最大值法灰度化
- getContentResolver()具体的用法
- 欧拉函数模板
- 为什么MathType公式会大小不一
- Cocos2dX通过Java服务器向Unity传输数据四
- Http,自动登录(涉及cookie)
- jenkins--安装
- BZOJ 2463: [中山市选2009]谁能赢呢?
- java package import
- 创建多维数组
- Java中变量的初始化顺序
- 二维滤波cvFilter2D
- Spring MVC 提示:HTTP Status 404 - /**/**