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