//创建消息数据模型 2
//正式项目中,消息的结构一般是消息长度+消息id+消息主体内容 3
public class Message 4
{
public IExtensible protobuf;
public int messageId; 7 } 8 9 public class SocketClientTemp : MonoBehaviour 10 { 11
const int packageMaxLength = 1024; 12 13 Socket mSocket; 14 Thread threadSend; 15
Thread threadRecive; 16 Queue<Message> allMessages = new Queue<Message>(); 17
Queue<byte[]> sendQueue = new Queue<byte[]>();
public bool Init() 20
{ 21 //创建一个socket对象 22
mSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); 23
return SocketConnection("此处是ip", 1111); 24 } 25 26 void Update() 27 { 28
AnalysisMessage(); 29 } 30 31 /// <summary> 32 /// 建立服务器连接 33 /// </summary> 34
/// <param name="ip">服务器的ip地址</param> 35 /// <param name="port">端口</param> 36
bool SocketConnection(string ip, int port) 37 { 38 try 39 { 40
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse(ip), port); 41
//同步连接服务器,实际使用时