C# 在unity上进行套接字通信时避免错误
我正在尝试接收与Unity上的C#的套接字通信 如果Send.py被中断,以下unityRecieve.cs将导致错误 Send.pyC# 在unity上进行套接字通信时避免错误,c#,sockets,unity3d,C#,Sockets,Unity3d,我正在尝试接收与Unity上的C#的套接字通信 如果Send.py被中断,以下unityRecieve.cs将导致错误 Send.py import socket import random HOST = '127.0.0.1' PORT = 50007 client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) while True: a = random.randrange(3) result
import socket
import random
HOST = '127.0.0.1'
PORT = 50007
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
while True:
a = random.randrange(3)
result = str(a)
print(a)
client.sendto(result.encode('utf-8'),(HOST,PORT))
time.sleep(2.0)
unityRecieve.cs
using UnityEngine;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class unityRecieve : MonoBehaviour
{
static UdpClient udp;
void Start()
{
int LOCA_LPORT = 50007;
udp = new UdpClient(LOCA_LPORT);
udp.Client.ReceiveTimeout = 100;
}
void Update()
{
IPEndPoint remoteEP = null;
byte[] data = udp.Receive(ref remoteEP);
string text = Encoding.UTF8.GetString(data);
Debug.Log(text);
}
}
当Send.py被中断时,我如何使UnitRecieve.cs保持运行而不发出错误消息?不确定您是否考虑过异常处理。如果没有,这可能会为您指明正确的方向