unity c# socket 客户端 通信
·
试了好久,终于成功了!

客户端代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class TestNet : MonoBehaviour {
// Use this for initialization
void Start () {
print("connect");
System.Net.Sockets.TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8080));
if (client.Connected)
{
//while (true)
{
var stream = client.GetStream();
byte[] buffer = new byte[1024];
buffer = Encoding.UTF8.GetBytes("123");
stream.Write(buffer, 0, buffer.Length);
byte[] buffer2 = new byte[1024];
stream.Read(buffer2, 0, buffer2.Length);
Debug.Log(Encoding.UTF8.GetString(buffer2,0, buffer2.Length));
}
client.Close();
}
}
// Update is called once per frame
void Update () {
}
}
服务端是用python写的,见我的另一篇博文:
https://blog.csdn.net/u013749051/article/details/85455517
更多推荐
所有评论(0)