停止unity端时抛出异常 这个是什么原因造成的
namespace tttt
{
//新建一个类,用于处理收到的连接
class Clients
{
private Socket clientSocket;
private Thread t;
private byte[] data=new byte[1024];
public Clients(Socket s)
{
Console.WriteLine("构造。。。");
clientSocket = s;
t =new Thread(ReceiveMsg);
t.Start();
}
void ReceiveMsg()
{
while (true)
{
Console.WriteLine("aaaaaa");
if (clientSocket.Poll(10,SelectMode.SelectRead))
{
Console.WriteLine("bbbbb");
break;
}
int length = clientSocket.Receive(data);
string msg = Encoding.UTF8.GetString(data, 0, length);
Console.WriteLine("接受的数据:"+msg);
}
}
}
}
//unity端
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using UnityEngine;
using UnityEngine.UI;
public class ClientScript : MonoBehaviour
{
private Socket clientSocket;
private string message;
public string ipAddress = "192.168.31.106";
public int port = 7788;
public Text ChaText;
public InputField MsgInputField;
void Start () {
ConnectToServer();
}
// Update is called once per frame
void Update () {
if (message!=null && message!="")
{
ChaText.text += "\n" + message;
message = "";
}
}
void ConnectToServer()
{
//连接到服务器
clientSocket = new socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
EndPoint point = new IPEndPoint(IPAddress.Parse(ipAddress), port);
clientSocket.Connect(point);
}
public void SendMsg()
{
byte[] msgBytes = Encoding.UTF8.GetBytes(MsgInputField.text);
print(MsgInputField.text);
//MsgInputField.text = "";
clientSocket.Send(msgBytes);
}
void OnDestroy()
{
clientSocket.Shutdown(SocketShutdown.Both);
clientSocket.Close();
}
}
图看不清 异常是
“System.Net.Sockets.SocketException”类型的未经处理的异常在 System.dll 中发生
远程主机强迫关闭了一个现有的连接。”