Unity不接受TCP连接,可能拒绝连接

时间:2013-09-27 15:36:17

标签: c# sockets tcp unity3d

我已经坚持了这么久。

我的脚本不允许任何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);
        }
    }

我已经尝试使用套接字测试连接到我的Unity应用程序,我收到错误,说连接被拒绝了。任何人都可以看到这个代码中发生了什么?

0 个答案:

没有答案