我正在尝试接收与C# on Unity的套接字通信。
如果中断unityRecieve.cs,下面的Send.py将导致错误。
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 = 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);
}
}https://jump1268.hatenablog.com/entry/2018/11/25/143459
当unitiRecieve.cs被中断时,我如何使Send.py继续运行而不给出错误消息?
发布于 2019-10-21 05:42:31
不确定是否考虑过异常处理。如果你不这样做,这可能会给你指明正确的方向。
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/try-catch
https://stackoverflow.com/questions/58478799
复制相似问题