客户端同步方式和异步方式
using System;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class EchoClient : MonoBehaviour
{
public const int port = 8888;
Socket socket;
public InputField inputField;
public Text textShow;
StringBuilder stringBuilder=new StringBuilder();
public Button sendButton;
public Button connetButton;
byte[] receiveBuffer = new byte[1024];
string receiveString = "";
#region 异步模式
/// <summary>
/// 异步连接
/// </summary>
public void AsyncConnection()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.BeginConnect("192.168.0.105", port, ConnectionCallBack,socket);
}
/// <summary>
/// 异步连接回调
/// </summary>
/// <param name="ar"></param>
private void ConnectionCallBack(IAsyncResult ar)
{
try
{
Socket socket = ar.AsyncState as Socket;
socket.EndConnect(ar);
Debug.Log("成功连接服务器");
stringBuilder.Append("\n"+"成功连接服务器");
//连接成功后,就开始异步接收
socket.BeginReceive(receiveBuffer,0,1024,0,ReceiveCallBack,socket);
}
catch (Exception ex)
{
stringBuilder.Append("\n" + "连接失败+" + ex.Message);
Debug.Log("连接失败+"+ex.Message);
}
}
/// <summary>
/// 异步接收回调
/// </summary>
/// <param name="ar"></param>
private void ReceiveCallBack(IAsyncResult ar)
{
try
{
Socket socket = ar.AsyncState as Socket;
int length = socket.EndReceive(ar);
receiveString = Encoding.Default.GetString(receiveBuffer, 0, length);
stringBuilder.Append("\n" + receiveString);
//接收完消息后,再次开启异步接收
socket.BeginReceive(receiveBuffer,0,1024,0,ReceiveCallBack,socket);
}
catch (Exception ex)
{
stringBuilder.Append("\n" + "接收失败+" + ex.Message);
Debug.Log("接收失败+" + ex.Message);
}
}
/// <summary>
/// 异步发送
/// </summary>
public void AsyncSend()
{
string sendString = inputField.text;
byte[] sendBuffer = Encoding.Default.GetBytes(sendString);
socket.BeginSend(sendBuffer,0,sendBuffer.Length,0,SendCallBack,socket);
//stringBuilder.Append("\n" + "要发送的数据长度" + sendBuffer.Length);
Debug.Log("要发送的数据长度" + sendBuffer.Length);
}
/// <summary>
/// 异步发送回调
/// </summary>
/// <param name="ar"></param>
/// <exception cref="NotImplementedException"></exception>
private void SendCallBack(IAsyncResult ar)
{
try
{
Socket socket = ar.AsyncState as Socket;
int length = socket.EndSend(ar);
Debug.Log("发送成功"+length);
//stringBuilder.Append("\n" + "发送成功" + length);
}
catch (Exception ex)
{
stringBuilder.Append("\n" + "发送失败+" + ex.Message);
Debug.Log("发送失败+" + ex.Message);
}
}
private void Start()
{
sendButton.onClick.AddListener(AsyncSend);
connetButton.onClick.AddListener(AsyncConnection);
}
private void Update()
{
textShow.text = stringBuilder.ToString();
}
#endregion
#region 同步模式
/// <summary>
/// 同步连接
/// </summary>
public void OnConnection()
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect("192.168.0.105", 8888);
textShow.text = "连接成功";
}
/// <summary>
/// 同步发送
/// </summary>
public void Send()
{
string sendString = inputField.text;
byte[] sendBuffer = Encoding.Default.GetBytes(sendString);
socket.Send(sendBuffer);
byte[] receiveBuffer = new byte[1024];
int length = socket.Receive(receiveBuffer);
string receiveString = Encoding.Default.GetString(receiveBuffer, 0, length);
textShow.text = receiveString;
socket.Close();
}
#endregion
}