unity3d C# soket客户端接受失败
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
using UnityEngine.UI;
using UnityEngine.Networking;
public class ss : MonoBehaviour {
public TcpClient client;
public BinaryReader breader;
public BinaryWriter bwriter;
public NetworkStream netstream;
string abc = "";
string abc0 = "";
Text text;
public bodyController CF;
public float m_speed = 5f;
private static Socket clientSocket;
private static byte[] result = new byte[1024];
// Use this for initialization
void Start () {
CF = GameObject.Find("RimaVirtualBody/rima").GetComponent<bodyController>();
client = new TcpClient("172.20.20.241", 8140);
// client = new TcpClient("127.0.0.1", 8139);
// netstream = client.GetStream();
clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//IPAddress mIp = IPAddress.Parse("172.20.20.241");
//IPEndPoint ip_end_point = new IPEndPoint(mIp, 8140);
IPAddress mIp = IPAddress.Parse("127.0.0.1");
IPEndPoint ip_end_point = new IPEndPoint(mIp, 8140);
clientSocket.Connect(ip_end_point);
//breader = new BinaryReader(netstream);
// bwriter = new BinaryWriter(netstream);
Thread threadReceive = new Thread(new ThreadStart(ReceiveData));
threadReceive.IsBackground = false;
threadReceive.Start();
//text = GameObject.Find("Canvas/Text").GetComponent<Text>();
}
private void ReceiveData()
{
int length = clientSocket.Receive(result);
//string receiveSting = null;
abc = Encoding.UTF8.GetString(result, 0, length);
Debug.Log(abc);
}
// Update is called once per frame
void Update () {
if(abc!=abc0)
{
if (abc == "聆听") CF.hear();
if (abc == "跳舞") CF.dance();
if (abc == "谈话") CF.talk();
if(abc=="立正") CF.idle();
abc0 = abc;
}
else
{
}
}
}
总是只接受一个字符串,或者把对方发来的接成yige
前者原因是while(true)漏了,后者是因为没关闭clientSocket.Receive,可以clientSocket.close();
while (true)
{
int length = clientSocket.Receive(result);
//string receiveSting = null;
abc = Encoding.UTF8.GetString(result, 0, length);
Debug.Log(abc);
}
前者改动后可以省略后者的改动。