张德长

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

客户端同步方式和异步方式

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
}

 

posted on   张德长  阅读(69)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示