? ? ? ? path.text = Application.persistentDataPath+"/";
? ? }
? ? ///
? ? /// 向服务器发送消息
? ? ///
? ? ///
? ? public IEnumerator sendData()
? ? {
? ? ? ? int i=0;
? ? ? ? while(true)
? ? ? ? {
? ? ? ? ? ? string sendStr = i.ToString()+"你好 server,I am Android";
? ? ? ? ? ? byte[] sendBytes = UTF8Encoding.UTF8.GetBytes(sendStr);
? ? ? ? ? ? clientSocket.Send(sendBytes);
? ? ? ? ? ? i++;
? ? ? ? ? ? yield return new WaitForSeconds(1f);
? ? ? ? }
? ? }
? ? ///
? ? /// 得到服务器回应
? ? ///
? ? ///
? ? public IEnumerator getInfo()
? ? {
? ? ? ? while(true)
? ? ? ? {
? ? ? ? ? ? byte[] revBytes = new byte[1024];
? ? ? ? ? ? int bytes = clientSocket.Receive(revBytes, revBytes.Length, 0);
? ? ? ? ? ? string revStr ="";
? ? ? ? ? ? //revStr += Encoding.ASCII.GetString(revBytes, 0, bytes);
? ? ? ? ? ? revStr += UTF8Encoding.UTF8.GetString(revBytes,0,bytes);
? ? ? ? ? ? Debug.Log ("接收到服务器消息:"+revStr);
? ? ? ? ? ? text.text = "From Server:"+revStr;
? ? ? ? ? ? yield return null;
? ? ? ? }
}
}
服务器端运行结果:


服务器回送消息代码:string strSend = "Hello Android Client!" + DateTime.Now.Second;

局域网下测试没有什么问题,以后无论是做应用还是网络游戏肯定少不了的是Socket网络数据传输,多了解点网络知识还是很有必要的尤其是TCP协议,如有错误,欢迎指正。