目前使用史陶比尔的c# soap当作server端,unity为client端,目前想要丢6个关节变数到unity,之后再分别丢到相对应的关节中,但本人机械专业,对程式不太了解,求指导。
下面是史陶比尔c# soap的程式码(server):
private void button55_Click_1(object sender, EventArgs e)
{
double c_px = 311.2;
double c_py = 18.38;
double c_pz = 137.44;
double c_rx = 10;
double c_ry = 179.93;
double c_rz = 11.53;
//創建服務器
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.");
while (true)
{
byte[] sendMsg = System.Text.Encoding.UTF8.GetBytes(string.Format(c_px.ToString())+',');
byte[] sendMsg2 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_py.ToString())+',');
byte[] sendMsg3 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_pz.ToString())+',');
byte[] sendMsg4 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_rx.ToString())+',');
byte[] sendMsg5 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_ry.ToString())+',');
byte[] sendMsg6 = System.Text.Encoding.UTF8.GetBytes(string.Format(c_rz.ToString()));
int sendLength = cilentSocket.Send(sendMsg, SocketFlags.None);
int sendLength2 = cilentSocket.Send(sendMsg2, SocketFlags.None);
int sendLength3 = cilentSocket.Send(sendMsg3, SocketFlags.None);
int sendLength4 = cilentSocket.Send(sendMsg4, SocketFlags.None);
int sendLength5 = cilentSocket.Send(sendMsg5, SocketFlags.None);
int sendLength6 = cilentSocket.Send(sendMsg6, SocketFlags.None);}
}
有什么方法可以把上面的c_px, c_py, c_pz, c_rx, c_ry, c_rz的变成一个byte array并用tcp socket的方式传到unity? 我目前是一个一个传到unity。
感谢各位,顺便附上我的client程式码(我也是参考别的博主的)。
using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Net;
using System;
using System.Collections.Generic;
using System.Linq;
/// <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;
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);
//Debug.Log(msg); //接收到的消息是 //"接收到的消息是"+
//開啟下一次接收消息
ReceiveFormServer();
string robottarget = Convert.ToString(msg);
robottarget = string.Join("", robottarget.Split());
List<string> r_robottarget = robottarget.Split(',').ToList();
//Debug.Log(robottarget);
Debug.Log(r_robottarget);
}
void Update()
{
if (Input.GetMouseButton(0))
{
SendMessageToServer("哭阿!");
}
if (clientSocket != null && clientSocket.Connected == false)
{
//鏈接沒有成功
//計時
connectTime += Time.deltaTime;
if (connectTime > connectInterval && isConnecting == false)//如果時間大於鏈接重置時間間隔且沒有鏈接
{
if (connectCount >= 7)
{
Debug.Log("已經嘗試了7次,請檢查網絡連接");
clientSocket = null;
}
else
{
//重連一次
ConnectedToServer();
}
}
}
}
private void OnDestroy()
{
Debug.Log("物體被銷毀");
//關閉客戶端
clientSocket.Close();
}
}