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);
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值