0

我已经坚持这个太久了。

我的脚本不允许任何 TCP 连接从我的设备发出。我已经用 Socket Test 测试了我的发射设备,它们确实可以根据需要工作和传输。但是,Unity 不接受我的信号,我不知道为什么。

有人可以看看我的脚本,看看他们是否能找出原因吗?我省略了大部分调试行。

   public class TCP : MonoBehaviour 
{   
    string ip_address  = "192.168.0.87";
    int port = 54321;
    public GameObject GText;

    Thread listen_thread;
    TcpListener tcp_listener;
    Thread clientThread;

    volatile bool should_stop;

    public void CloseSocket(TcpListener Client)
    {           
        //Client.Close();

    }


    public void Start()
    {          
    IPAddress ip_addy = IPAddress.Parse(ip_address);

    tcp_listener = new TcpListener(ip_addy, 22);
    tcp_listener.Start();

    StartAccept();

    }
    private void StartAccept()
    {
    tcp_listener.BeginAcceptTcpClient(HandleAsyncConnection, tcp_listener);
    GText.GetComponent<GUIText>().text =("Listening Again");
        Debug.Log("listeneing again");
    }
    private void HandleAsyncConnection(IAsyncResult res)
    {
            Debug.Log("handle async");
     byte[] bytes = new byte[1024];
     string data;

    StartAccept(); //listen for new connections again
    TcpClient client = tcp_listener.EndAcceptTcpClient(res);
    NetworkStream stream = client.GetStream();
    //proceed

    int i;

    // Loop to receive all the data sent by the client.
    i = stream.Read(bytes, 0, bytes.Length);

                while (i != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);

                    i = stream.Read(bytes, 0, bytes.Length);

                }
    GText.GetComponent<GUIText>().text =("Second Operation");
    //CloseSocket(tcp_listener);

    }   
        void RequestStop()
    {
        should_stop = true;
    }
    // Use this for initialization
    /**
    void Start () 
    {
        try{
        IPAddress ip_addy = IPAddress.Parse(ip_address);
        tcp_listener = new TcpListener(ip_addy, port);
        listen_thread = new Thread(new ThreadStart(ListenForClients));
        listen_thread.Start();  

        }
        catch(Exception e)
        {
            GText.GetComponent<GUIText>().text =(e.ToString()); 
        }

    }

    private void ListenForClients()
    {
        this.tcp_listener.Start();

        while(true) 
        {
            //blocks until a client has connected to the server
            TcpClient client = this.tcp_listener.AcceptTcpClient();

            this.tcp_listener.Stop();               

            client.Close();

        }

        this.tcp_listener.Stop();




    }

    */
    private void HandleClientComm(object client)
    {
        TcpClient tcp_client = (TcpClient)client;
        NetworkStream client_stream = tcp_client.GetStream();


        byte[] message = new byte[4096];
        int bytes_read;

        while(true)
        {
            bytes_read = 0;

            try
            {
                //blocks until a client sends a message
                bytes_read = client_stream.Read(message, 0, 4096);

            }
            catch (Exception e)
            {
              //a socket error has occured
              GText.GetComponent<GUIText>().text =(e.Message);
                    Debug.Log(e.Message);
              break;
            }

            if(bytes_read == 0)
            {
                //client has disconnected
                break;
            }

            ASCIIEncoding encoder = new ASCIIEncoding();
        }
        try
        {
            clientThread.Abort();
        }
        catch(Exception e)
        {
            GText.GetComponent<GUIText>().text =(e.Message);
            Debug.Log(e.Message);
        }
    }

我也尝试使用套接字测试连接到我的统一应用程序,但我收到一条错误消息,指出连接被拒绝。谁能看到这段代码中发生了什么?

4

0 回答 0