现在在做的是,我按下了unity server之后会开启server并开始send message到client(unity)。
目前我传送的是机械臂的6个关节角度,接着我设计一个按钮来移动机械臂的各关节角度,旋转之后,我的server端传给client的资讯是之前旧的,新旋转的各关节角度并没有传送到客户端,想请问是什么原因?
目前的步骤是:
先开启unity server接着到unity play按下connect开启client并接收传过来的6个关节角度,虚拟的手臂会按照接收到的6轴的各角度去旋转,接着我回去到上面的图按下test,机械臂会旋转一定的角度,但是旋转完之后,server端传给client的资讯并没有更新传送,想请问一下为什么?
unity server:
double joint1 = 0;
double joint2 = 0;
double joint3 = 90;
double joint4 = 0;
double joint5 = 0;
double joint6 = 0;
//創建服務器
Socket severSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//ip地址和端口號綁定
EndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 10001);
//綁定服務器
severSocket.Bind(endPoint);
//發起監聽
severSocket.Listen(1000);
Console.WriteLine("server already opened!");
Console.WriteLine("Waiting for Client connect!");
Socket cilentSocket = severSocket.Accept();//這裡會阻塞,等待鏈接
Console.WriteLine("client is connect.");
string data = string.Format("{0},{1},{2},{3},{4},{5}", joint1, joint2, joint3, joint4, joint5, joint6);
byte[] sendMsg = System.Text.Encoding.UTF8.GetBytes(data);
int sendLength = cilentSocket.Send(sendMsg, SocketFlags.None);
}
client (unity):
using System;
using System.Net;
using UnityEngine;
using System.Net.Sockets;
using System.Collections.Generic;
/// <summary>
/// 客戶端的控制腳本
/// </summary>
public class ClientSocketController : MonoBehaviour
{
/// <summary>
/// 鏈接對象
/// </summary>
public Socket clientSocket;
/// <summary>
/// ip地址
/// </summary>
public string ipAddress = "127.0.0.1";
/// <summary>
/// 端口號,這個是服務器開設的端口號
/// </summary>
public int portNumber = 10001;
/// <summary>
/// 鏈接間隔時間
/// </summary>
public float connectInterval = 1;
/// <summary>
/// 當前鏈接時間
/// </summary>
public float connectTime = 0;
/// <summary>
/// 鏈接次數
/// </summary>
public int connectCount = 0;
/// <summary>
/// 是否在連接中
/// </summary>
public bool isConnecting = false;
//-------------------CoderZ 2022.06.16-------------------
private Queue<string> dataQueue = new Queue<string>();
void Start()
{
// //調用開始連接
// ConnectedToServer();
}
/// <summary>
/// 鏈接到服務器
/// </summary>
public void ConnectedToServer()
{
//鏈接次數增加
connectCount++;
isConnecting = true;
Debug.Log("這是第" + connectCount + "次連接");
//如果客戶端不為空
if (clientSocket != null)
{
try
{
//斷開連接,釋放資源
clientSocket.Disconnect(false);
clientSocket.Close();
}
catch (System.Exception e)
{
Debug.Log(e.ToString());
}
}
//創建新的鏈接(固定格式)
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//設置端口號和ip地址
EndPoint endPoint = new IPEndPoint(IPAddress.Parse(ipAddress), portNumber);
//發起鏈接
clientSocket.BeginConnect(endPoint, OnConnectCallBack, "");
}
/// <summary>
/// 開始鏈接的回調
/// </summary>
/// <param name="ar"></param>
public void OnConnectCallBack(IAsyncResult ar)
{
Debug.Log("連接完成!!!!");
if (clientSocket.Connected)
{
//鏈接成功
Debug.Log("連接成功");
connectCount = 0;
//開啟收消息
ReceiveFormServer();
}
else
{
//鏈接失敗
Debug.Log("連接失敗");
//計時重置
connectTime = 0;
}
isConnecting = false;
//結束鏈接
clientSocket.EndConnect(ar);
}
/// <summary>
/// 向服務器發送字符串信息
/// </summary>
/// <param name="msg"></param>
public void SendMessageToServer(string msg)
{
//將字符串轉成byte數組
byte[] msgBytes = System.Text.Encoding.UTF8.GetBytes(msg);
clientSocket.BeginSend(msgBytes, 0, msgBytes.Length, SocketFlags.None, SendMassageCallBack, 1);
}
/// <summary>
/// 發送信息的回調
/// </summary>
/// <param name="ar"></param>
public void SendMassageCallBack(IAsyncResult ar)
{
//關閉消息發送
int length = clientSocket.EndSend(ar);
Debug.Log("信息發送成功,發送的信息長度是:" + length);
}
/// <summary>
/// 從服務器接收消息
/// </summary>
public void ReceiveFormServer()
{
//定義緩衝池
byte[] buffer = new byte[512];
clientSocket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, ReceiveFormServerCallBack, buffer);
}
/// <summary>
/// 信息接收方法的回調
/// </summary>
/// <param name="ar"></param>
public void ReceiveFormServerCallBack(IAsyncResult ar)
{
//結束接收
int length = clientSocket.EndReceive(ar);
byte[] buffer = (byte[])ar.AsyncState;
//將接收的東西轉為字符串
string msg = System.Text.Encoding.UTF8.GetString(buffer, 0, length);
//開啟下一次接收消息
ReceiveFormServer();
//CoderZ 2020.06.16 将服务端发送来的数据存入队列
dataQueue.Enqueue(msg);
}
void Update()
{
//CoderZ 2020.06.16 从队列中取出数据
if (dataQueue.Count > 0)
{
string data = dataQueue.Dequeue();
string[] r_robottarget = data.Split(',');
//打印
for (int i = 0; i < r_robottarget.Length; i++)
{
Debug.Log(r_robottarget[i]);
}
//第一個關節數據
joint1.Instance.ReceiveData(r_robottarget[0]);
//第二個關節數據
joint2.Instance.ReceiveData(r_robottarget[1]);
//第三個關節數據
joint3.Instance.ReceiveData(r_robottarget[2]);
//第四個關節數據
joint4.Instance.ReceiveData(r_robottarget[3]);
//第五個關節數據
joint5.Instance.ReceiveData(r_robottarget[4]);
//第六個關節數據
joint6.Instance.ReceiveData(r_robottarget[5]);
}
}
}
还请各位博主,帮忙指导一下!