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

Java服务器代码如下:

package com.insect.server;

import java.net.ServerSocket;
import java.net.socket;

public class ServerThread extends Thread{
	
	boolean flag=false;
	ServerSocket ss;
	
	public void run(){
		try {
			ss=new ServerSocket(59422);
			System.out.println("Server Listening on 59422...");
			flag=true;
		} catch (Exception e) {
			// Todo: handle exception
			e.printstacktrace();
		}
		
		while(flag){
			try {
				Socket sc=ss.accept();
				System.out.println(sc.getInetAddress()+" connect...");
				ServerAgent.flag=true;
				new ServerAgent(this,sc).start();
			} catch (Exception e) {
				// Todo: handle exception
			}
		}
	}
	
	public static void main(String[] args) {
		// Todo Auto-generated method stub
		new ServerThread().start(); 
		
	}
}


package com.insect.server;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.socket;







import com.insect.data.GameData;
import com.insect.util.UnityStream;

import static com.insect.util.IoUtil.*;

public class ServerAgent extends Thread{

	Socket sc;
	static ServerThread st;
	DataOutputStream dout;
	DataInputStream din;
	static boolean flag=true;
	
	public ServerAgent(Socket sc){
		this.sc=sc;
		try {
			din=new DataInputStream(sc.getInputStream());
			dout=new DataOutputStream(sc.getoutputStream());
		} catch (Exception e) {
			// Todo: handle exception
		}
	}
	
	public ServerAgent(ServerThread st,Socket sc){
		this.sc=sc;
		ServerAgent.st=st;
		try {
			din=new DataInputStream(sc.getInputStream());
			dout=new DataOutputStream(sc.getoutputStream());
		} catch (Exception e) {
			// Todo: handle exception
			e.printstacktrace();
		}
	}
	
	public void run(){
		while(flag){
			try {

				String msg=readStr(din);
				System.out.println(msg);
				if(msg.startsWith("COCOS")){
					GameData.rotation=readInt(din);
					System.out.println(GameData.rotation+"cocos");
				}else if(msg.startsWith("UNITY")){
					sendInt(dout,GameData.rotation);
					System.out.println(GameData.rotation+"unity");
				}
				
			} catch (Exception e) {
				// Todo: handle exception
				
			}
		}
		try {
			din.close();
			dout.close();
			sc.close();
		} catch (Exception e) {
			// Todo: handle exception
			e.printstacktrace();
		}
	}
	
}

package com.insect.data;

public class GameData {

	public static int rotation=50;
	
}

package com.insect.util;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.ObjectOutputStream;


public class IoUtil {

	public static String readMessage(DataInputStream din)throws Exception{
		byte[] buf=new byte[2];
		int count=0;
		while(count<2){
			int tc=din.read(buf);
			count=count+tc;
		}
		
		return ConvertUtil.fromBytesToString(buf);
	}
	
	public static int readInt(DataInputStream din) throws Exception
	{
		byte[] buf=new byte[4];
		int count=0;
		while(count<4)
		{
			int tc=din.read(buf);
			count=count+tc;
		}		
		return ConvertUtil.fromBytesToInt(buf);
	}
	
	
	public static void sendInt(DataOutputStream dout,int a) throws Exception
	{
		byte[] buf=ConvertUtil.fromIntToBytes(a);
		dout.write(buf);
		dout.flush();
	}
	
	public static void sendStr(DataOutputStream dout,String str) throws Exception
	{
		byte[] buf=ConvertUtil.fromStringToBytes(str);
		sendInt(dout,buf.length); 
		dout.write(buf);
		dout.flush();
	}	
	
	public static String readStr(DataInputStream din) throws Exception
	{
		//接收字符串长度
		int len=readInt(din);	
		byte[] buf=new byte[len];
		int count=0;
		while(count<len)
		{
			int tc=din.read(buf);
			count=count+tc;
		}
		return ConvertUtil.fromBytesToString(buf);
	}
	
	public static void Sendobject(ObjectOutputStream oos,UnityStream us) throws	Exception
	{
		oos.writeObject(us);
		oos.flush();
	}
	
}

package com.insect.util;

import java.nio.charset.Charset;

public class ConvertUtil {

	public static byte[] fromStringToBytes(String s){
		byte[] ba=s.getBytes(Charset.forName("UTF-8"));
		return ba;
	}
	
	public static byte[] fromIntToBytes(int k){
		byte[]buff=new byte[4];
		buff[0]=(byte)(k&0x000000FF);
		buff[1]=(byte)((k&0x0000FF00)>>>8);
		buff[2]=(byte)((k&0x00FF0000)>>>16);
		buff[3]=(byte)((k&0xFF000000)>>>24);
		return buff;
	}
	
	public static byte[] fromFloatToBytes(float f){
		int ti=Float.floatToIntBits(f);
		return fromIntToBytes(ti);
	}

	public static String fromBytesToString(byte[] bufId){
		String s=null;
		try {
			s=new String(bufId,"UTF-8");
		} catch (Exception e) {
			// Todo: handle exception
			e.printstacktrace();
		}
		return s;
	}
	
	public static int fromBytesToInt(byte[]buff){
		return (buff[3]<<24)+((buff[2]<<24)>>>8)+((buff[1]<<24)>>>16)+((buff[0]<<24)>>>24);
	}
	
	public static float fromBytesToFloat(byte[]buf){
		int k=fromBytesToInt(buf);
		return Float.intBitsToFloat(k);
	}
	
}

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...