资源描述:
《unity3dsocket和javasocket通信byfeng侠》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、Unity3D客户端:namespaceLSocket.Net
{
/****@authorfeng侠,qq:313785443*@date2010-12-23**///描述:封装c#socket数据传输协议
usingUnityEngine;
usingSystem;
usingSystem.Net.Sockets;
usingSystem.Net;
usingSystem.Collections;
usingSystem.Text;
usingLSocket.Type;
usingLSocket.cmd;
publicclassUnitySocket
{
publ
2、icstaticSocketmSocket=null;
publicUnitySocket()
{
}
publicstaticvoidSocketConnection(stringLocalIP,intLocalPort)
{
mSocket=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
try
{
IPAddressip=IPAddress.Parse(LocalIP);
IPEndPointipe=newIPEndPoint(ip,LocalPort);
3、
mSocket.Connect(ipe);
Send("");//thesecondconnect.ifit'snotequalspolicyhead,itwillcomeinthemainconnect!
Send(CommandID.LOGIN);
Send("feng");
strings=ReceiveString(100);//从服务器端接受返回信息
MonoBehaviour.print(s.Length);
MonoBehaviour.print(s);
}
catch(Exceptione)
{
//E
4、rrLog.RecordErr(e,ModuleName,"AsySocket","");
}
}
publicstaticvoidSend(shortdata)
{
byte[]longth=TypeConvert.getBytes(data,false);
mSocket.Send(longth);
}
publicstaticvoidSend(longdata)
{
byte[]longth=TypeConvert.getBytes(data,false);
mSocket.Send(longth);
}
publicstaticvoidSend(in
5、tdata)
{
byte[]longth=TypeConvert.getBytes(data,false);
mSocket.Send(longth);
}
publicstaticvoidSend(stringdata)
{
byte[]longth=Encoding.UTF8.GetBytes(data);
mSocket.Send(longth);
}
publicstaticshortReceiveShort()
{
byte[]recvBytes=newbyte[2];
mSocket.Receive(recvBytes,2,0);//从服务器端接
6、受返回信息
shortdata=TypeConvert.getShort(recvBytes,true);
returndata;
}
publicstaticintReceiveInt()
{
byte[]recvBytes=newbyte[4];
mSocket.Receive(recvBytes,4,0);//从服务器端接受返回信息
intdata=TypeConvert.getInt(recvBytes,true);
returndata;
}
publicstaticlongReceiveLong()
{
byte[]recvBytes=newbyte[
7、8];
mSocket.Receive(recvBytes,8,0);//从服务器端接受返回信息
longdata=TypeConvert.getLong(recvBytes,true);
returndata;
}
publicstaticstringReceiveString(intlength)
{
byte[]recvBytes=newbyte[length];
mSocket.Receive(recvBytes,length,0);//从服务器端接受返回信息
stringdata=Encoding.UTF8.GetString(recvB