java socket asc_Unity3d socket和java socket通信(Unity部分)

本文展示了如何使用Unity3D客户端通过Java Socket进行数据传输。主要包含Unity3D中的Socket连接、发送与接收各种类型数据的方法,如short、int、long和字符串,以及特定的命令ID定义。
摘要由CSDN通过智能技术生成

Unity3D客户端:

namespace LSocket.Net {

/**

*

* @author feng侠,qq:313785443

* @date 2010-12-23

*

*/

// 描   述:封装c# socket数据传输协议

using UnityEngine;

using System;

using System.Net.Sockets;

using System.Net;

using System.Collections;

using System.Text;

using LSocket.Type;

using LSocket.cmd;

public class UnitySocket {

public static Socket mSocket = null;

public UnitySocket() {

}

public static void SocketConnection(string LocalIP, int LocalPort) {

mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

try {

IPAddress ip = IPAddress.Parse(LocalIP);

IPEndPoint ipe = new IPEndPoint(ip, LocalPort);

mSocket.Connect(ipe);

Send("");//the second connect.if it's not equals policy head,it will come in the main connect!

Send(CommandID.LOGIN);

Send("feng");

string s=ReceiveString(100);//从服务器端接受返回信息

MonoBehaviour.print(s.Length);

MonoBehaviour.print(s);

} catch (Exception e) {

//ErrLog.RecordErr(e, ModuleName, "AsySocket", "");

}

}

public static void Send(short data) {

byte[] longth=TypeConvert.getBytes(data,false);

mSocket.Send(longth);

}

public static void Send(long data) {

byte[] longth=TypeConvert.getBytes(data,false);

mSocket.Send(longth);

}

public static void Send(int data) {

byte[] longth=TypeConvert.getBytes(data,false);

mSocket.Send(longth);

}

public static void Send(string data) {

byte[] longth=Encoding.UTF8.GetBytes(data);

mSocket.Send(longth);

}

public static short ReceiveShort() {

byte[] recvBytes = new byte[2];

mSocket.Receive(recvBytes,2,0);//从服务器端接受返回信息

short data=TypeConvert.getShort(recvBytes,true);

return data;

}

public static int ReceiveInt() {

byte[] recvBytes = new byte[4];

mSocket.Receive(recvBytes,4,0);//从服务器端接受返回信息

int data=TypeConvert.getInt(recvBytes,true);

return data;

}

public static long ReceiveLong() {

byte[] recvBytes = new byte[8];

mSocket.Receive(recvBytes,8,0);//从服务器端接受返回信息

long data=TypeConvert.getLong(recvBytes,true);

return data;

}

public static string ReceiveString(int length) {

byte[] recvBytes = new byte[length];

mSocket.Receive(recvBytes,length,0);//从服务器端接受返回信息

string data=Encoding.UTF8.GetString(recvBytes);

return data;

}

}

}

namespace LSocket.cmd {

public class CommandID {

/** 错误消息命令 **/

public static int ERROR = 1000;

/** 登陆消息命令 **/

public static  int LOGIN = 1001;

/** 退出消息命令 **/

public static  int EXIT = 1002;

/** 获取pdb文件消息命令 **/

public static  int GETPDB= 1003;

public static  int GETPDB_AGAIN = 1006;

/** 其他用户进入消息命令 **/

public static  int OTHER_USERS = 1004;

public static  int ACCEPT=1005;

public CommandID() {

}

}

}

namespace LSocket.Type {

using UnityEngine;

using System.Collections;

public class TypeConvert {

public TypeConvert() {

}

public  static byte[] getBytes(short s, bool asc) {

byte[] buf = new byte[2];

if (asc) {

for (int i = buf.Length - 1; i >= 0; i--) {

buf[i] = (byte) (s & 0x00ff);

s >>= 8;

}

} else {

for (int i = 0; i 

buf[i] = (byte) (s & 0x00ff);

s >>= 8;

}

}

return buf;

}

public static byte[] getBytes(int s, bool asc) {

byte[] buf = new byte[4];

if (asc)

for (int i = buf.Length - 1; i >= 0; i--) {

buf[i] = (byte) (s & 0x000000ff);

s >>= 8;

}

else

for (int i = 0; i 

buf[i] = (byte) (s & 0x000000ff);

s >>= 8;

}

return buf;

}

public static byte[] getBytes(long s, bool asc) {

byte[] buf = new byte[8];

if (asc)

for (int i = buf.Length - 1; i >= 0; i--) {

buf[i] = (byte) (s & 0x00000000000000ff);

s >>= 8;

}

else

for (int i = 0; i 

buf[i] = (byte) (s & 0x00000000000000ff);

s >>= 8;

}

return buf;

}

public  static short getShort(byte[] buf, bool asc) {

if (buf == null) {

//throw new IllegalArgumentException("byte array is null!");

}

if (buf.Length > 2) {

//throw new IllegalArgumentException("byte array size > 2 !");

}

short r = 0;

if (asc)

for (int i = buf.Length - 1; i >= 0; i--) {

r <<= 8;

r |= (short)(buf[i] & 0x00ff);

}

else

for (int i = 0; i 

r <<= 8;

r |= (short)(buf[i] & 0x00ff);

}

return r;

}

public  static int getInt(byte[] buf, bool asc) {

if (buf == null) {

// throw new IllegalArgumentException("byte array is null!");

}

if (buf.Length > 4) {

//throw new IllegalArgumentException("byte array size > 4 !");

}

int r = 0;

if (asc)

for (int i = buf.Length - 1; i >= 0; i--) {

r <<= 8;

r |= (buf[i] & 0x000000ff);

}

else

for (int i = 0; i 

r <<= 8;

r |= (buf[i] & 0x000000ff);

}

return r;

}

public static long getLong(byte[] buf, bool asc) {

if (buf == null) {

//throw new IllegalArgumentException("byte array is null!");

}

if (buf.Length > 8) {

//throw new IllegalArgumentException("byte array size > 8 !");

}

long r = 0;

if (asc)

for (int i = buf.Length - 1; i >= 0; i--) {

r <<= 8;

r |= (buf[i] & 0x00000000000000ff);

}

else

for (int i = 0; i 

r <<= 8;

r |= (buf[i] & 0x00000000000000ff);

}

return r;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值