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("<!policy-file-request>");//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.Length; 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.Length; 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.Length; 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 < buf.Length; 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 < buf.Length; 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 < buf.Length; i++) {
r <<= 8;
r |= (buf[i] & 0x00000000000000ff);
}
return r;
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。