013-05-14 09:28 425人閱讀 收藏 舉報 轉載于:圣典的unity csharp socket 異步通信 客戶端http://game./forum/read.php?tid=1478 - unity csharp socket 異步通信 客戶端
- 作者:圍城(solq)
- bolg:http://www.cnblogs.com/solq/
- 服務端用java nio 測試過是可以的。,。,。。 ...
- demo : http://unitysgui.sinaapp.com/chat
- using UnityEngine;
- using System.Collections;
- using System.Net.Sockets;
- using System.Net;
- using System;
- using System.Text;
- using System.Threading;
-
- public class TestAsyncSocketClient2 : MonoBehaviour
- {
- private Socket client = null;
- private string ip = "127.0.0.1";
- private int port = 8989;
-
- private int size = 1024;
- private byte[] readData = new byte[1024];
- private byte[] data = new byte[1024];
- void Start()
- {
- // socket2.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); 多socket 復用同一端口
- client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- IPEndPoint address = new IPEndPoint(IPAddress.Parse(ip), port);
- //client.Blocking = false;
- client.BeginConnect(address, new AsyncCallback(Connected), null); //建立異步連接服務 , Connected 進行監(jiān)聽
- //connectDone.WaitOne();
- }
- void Connected(IAsyncResult iar) //建立連接
- {
- //Socket client = (Socket)iar.AsyncState;
- client.EndConnect(iar);
- //client.BeginReceive(data, 0, size, SocketFlags.None, new AsyncCallback(ReceiveData), client);
- echo("建立連接");
-
- }
- void Send(string str)
- {
- byte[] msg = Encoding.UTF8.GetBytes(str);
- client.BeginSend(msg, 0, msg.Length, SocketFlags.None, new AsyncCallback(SendData), client); //開始發(fā)送
- }
- void SendData(IAsyncResult iar) //發(fā)送數(shù)據(jù)
- {
- Socket remote = (Socket)iar.AsyncState;
- int sent = remote.EndSend(iar); //關閉發(fā)送
- remote.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(ReceiveData), remote); //開始接收
- }
-
-
-
- void Update()
- {
- startReceive(); //這步很重要,,,,,不然會收不到服務器發(fā)過來的消息
- }
- bool ReceiveFlag = true;
- void startReceive()
- {
- if (ReceiveFlag) {
- ReceiveFlag = false;
- client.BeginReceive(readData, 0, readData.Length, SocketFlags.None, new AsyncCallback(endReceive), client);
- }
- }
-
- void endReceive(IAsyncResult iar) //接收數(shù)據(jù)
- {
- ReceiveFlag = true;
- Socket remote = (Socket)iar.AsyncState;
- int recv = remote.EndReceive(iar);
- if (recv > 0)
- {
- string stringData = Encoding.UTF8.GetString(readData, 0, recv);
- text2 += "\n" + "接收服務器數(shù)據(jù):+++++++++++++++" + stringData;
- }
-
- }
- void ReceiveData(IAsyncResult iar) //接收數(shù)據(jù)
- {
- Socket remote = (Socket)iar.AsyncState;
- int recv = remote.EndReceive(iar); //關閉接收 注意:如果關閉了接收,,就不能接收了 測試是這樣
-
- string stringData = Encoding.UTF8.GetString(data, 0, recv);
- text2 += "\n" + "回收發(fā)送數(shù)據(jù):+++++++++++++++" + stringData;
-
- }
-
-
- void CloseSocket() //關閉socket
- {
- if (client.Connected)
- {
- echo("關閉socket");
- client.Close();
- }
- }
- void OnApplicationQuit()
- {
- CloseSocket();
- }
-
- void echo(object msg)
- {
- Debug.Log(msg);
- }
-
- string text = "";
- string text2 = "";
- Vector2 p = new Vector2(600, 300);
- void OnGUI()
- {
- GUILayout.BeginVertical(GUILayout.Width(500) );
- text = GUILayout.TextField(text);
- if (GUILayout.Button("發(fā)送數(shù)據(jù)"))
- {
- Send(text);
- }
- GUILayout.BeginScrollView(p);
- text2 = GUILayout.TextArea(text2, GUILayout.Height(300));
- GUILayout.EndScrollView();
- GUILayout.EndVertical();
-
- }
-
- }
|