unity3d C# soket客户端接受失败
2024-10-15 23:30:13
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);
}
前者改动后可以省略后者的改动。
最新文章
- Google Android 6.0 权限完全解析
- web服务器页面错误代码集
- Webloigic监控
- Java 数据类型之间的转换 拆分字符串 Date/Calendar的转换
- Delphi 保存写字板程序, 并进行打印
- iOS App中数据加载的6种方式
- 解决Lost connection to MySQL server at &#39;reading initial communication packet&#39;, 的方法
- 使用 Socket.IO 开发聊天室
- 模块使用:time、datetime、calendar、sys、os、os.path、normcase和normapath、random、json、pickle
- Sql学习笔记(二)—— 条件查询
- Oracle debug
- LeetCode 145 二叉树的后序遍历(非递归)
- java高级进阶
- day022 python (re模块和 模块)
- Linux 下的编译安装说明
- Xianfeng轻量级Java中间件平台:功能管理
- 【python】列表&;&;元组&;&;字典
- python的函数介绍 位置参数 关键字参数 默认参数 参数组 *args **kwargs
- Fiddler手机https抓包
- jmeter -分布式性能测试部署
热门文章
- centos下修改hosts文件以及生效命令
- 『与善仁』Appium基础 — 29、获取toast信息
- 【LeetCode】657. Judge Route Circle 解题报告
- 【LeetCode】398. Random Pick Index 解题报告(Python & C++)
- CF 593B Anton and Lines(水题)
- 【剑指Offer】滑动窗口的最大值 解题报告(Python)
- Parenthesis
- Towards Evaluating the Robustness of Neural Networks
- 如何在HTML中添加表格标题?(HTML中table添加标题的2种方法)
- Java程序设计基础笔记 • 【第7章 Java中的类和对象】