public void OnSucessLogin() { IOSocket io = new IOSocket(); io.WriteInt32(NetMsgDefine.sayhello); io.WriteString16("im client"); //这是我测试bufflist的方法 我们 if (useBufferList) { m_sendArgs.BufferList = NetUtil.CreateBuffList <byte>(Encoding.Default.GetBytes("我是客户端"), 4); } else { m_sendArgs.SetBuffer(io.GetBuffer(), 0, io.GetLength()); } m_socket.SendAsync(m_sendArgs); Recv(); ////方法一:使用Thread类 ThreadStart threadStart = new ThreadStart(SendHeadBeat);//通过ThreadStart委托告诉子线程执行什么方法 if (thread_sendHead == null) { thread_sendHead = new Thread(threadStart); thread_sendHead.Start();//启动新线程 } }
public void SendHeadBeat() { while (true) { if (m_socket != null) { lock (m_socket) { IOSocket io = new IOSocket(); io.WriteInt32(NetMsgDefine.HeadBeat); io.WriteByte(1); if (m_headBeat == null) { m_headBeat = new SocketAsyncEventArgs(); } m_headBeat.SetBuffer(io.GetBuffer(), 0, io.GetLength()); m_socket.SendAsync(m_headBeat); } } Thread.Sleep(1000); } }