当前位置:  开发笔记 > 编程语言 > 正文

如何修复"连接尝试失败,因为连接方在一段时间后没有正确响应......"错误?

如何解决《如何修复"连接尝试失败,因为连接方在一段时间后没有正确响应"错误?》经验,为你挑选了1个好方法。

我正在用C#制作游戏,我想展示对手的进度(动作等等).所以我通过TCP协议将游戏中的事件发送给对手.

我已经在localhost上尝试了我的应用程序,但是当我尝试使用我的外部地址以便通过互联网进行通信时,我在TcpInformer.Connect()类中收到以下错误:

连接尝试失败,因为连接方在一段时间后未正确响应,或建立的连接失败,因为连接的主机未能响应(我的外部IP地址):(端口)

我认为问题在于我是在NAT背后.但我已经在IP 10.0.0.1上为端口49731设置了portforwarding,但没有任何改变.

我的第二个猜测是Windows防火墙,但即使我停止防火墙,我的应用程序也没有开始工作.

我连接两台PC的代码是:


        TcpInformer peer;
        TcpHost server;

        public void PrepareConnection() // for server (host)
        {
            playerType = PlayerType.One;
            server = new TcpHost(form, this);
            server.Start("10.0.0.1", 49731);
        }

        public void PrepareConnection2() // for client
        {
            playerType = PlayerType.Two;
            peer = new TcpInformer(form, this);
            peer.Connect("MY EXTERNAL IP", 49731);
        }


// classes TcpHost and TcpInformer

    public interface ITcpCommunication
    {
        #region Operations (3) 

        void ReadData();

        void SendData(byte[] message);

        void SendData(byte[] message, int size);

        #endregion Operations 
    }

    public class TcpInformer : ITcpCommunication
    {
        #region Fields (9) 

        private NetworkStream con_ns;
        private TcpClient con_server;
        private bool connected;
        private Fmain form;
        private SecondPlayer player;
        private int port;
        private string server;
        private string stringData;

        #endregion Fields 

        #region Delegates and Events (1)

        // Events (1) 

        public event SimulationEventHandler ReadEvent;

        #endregion Delegates and Events 

        #region Constructors (1) 

        public TcpInformer(Fmain form, SecondPlayer player)
        {
            this.form = form;
            connected = false;
            this.player = player;
        }

        #endregion Constructors 

        #region Methods (6) 

        // Public Methods (5) 

        /// 
        /// 
        /// 
        /// e.g., server = "127.0.0.1"
        /// e.g., port = 9050
        public void Connect(string server, int port) 
        {
            this.port = port;
            this.server = server;
            connected = true;

            try
            {
                con_server = new TcpClient(this.server, this.port);
            }
            catch (SocketException ex)
            {
                connected = false;
                MessageBox.Show("Unable to connect to server" + ex.Message);
                return;
            }

            con_ns = con_server.GetStream();
        }

        public void Disconnect()
        {
            form.Debug("Disconnecting from server...", "Player2Net");
            con_ns.Close();
            con_server.Close();
        }

        public void ReadData()
        {
            if (con_ns != null)
            {
                if (con_ns.DataAvailable)
                {
                    byte[] data = new byte[1200];
                    int received = con_ns.Read(data, 0, data.Length);

                    player.ProcessReceivedData(data, received);
                }
            }
            else
            {
                form.Debug("Warning: con_ns is not inicialized.","player2");
            }
        }

        public void SendData(byte[] message)
        {
            con_ns.Write(message, 0, message.Length);
            con_ns.Flush();
        }

        public void SendData(byte[] message, int size)        
        {
            if (con_ns != null)
            {
                con_ns.Write(message, 0, size);
            }
        }
        // Private Methods (1) 

        private void Debug(string message)
        {
            form.Debug("Connected to: " + server + "port: " + port.ToString() + ": " + message, "Player2Net");
        }

        #endregion Methods 
    }

    public class TcpHost : ITcpCommunication
    {
        #region Fields (9) 

        private ASCIIEncoding enc;
        private Fmain form;
        private TcpListener listener;
        private SecondPlayer player;
        private int port;
        private Socket s;
        private string server;
        private bool state;

        #endregion Fields 

        #region Delegates and Events (1)

        // Events (1) 

        public event SimulationEventHandler ReadEvent;

        #endregion Delegates and Events 

        #region Constructors (1) 

        public TcpHost(Fmain form, SecondPlayer player)
        {
            this.player = player;
            this.form = form;
            state = false;
            enc = new ASCIIEncoding();
        }

        #endregion Constructors 

        #region Methods (5) 

        // Public Methods (5) 

        public void Close()
        {
            state = false;
            s.Close();
            listener.Stop();
        }

        public void ReadData()
        {
            if (state == true)
            {
                if (s.Available > 0) // if there's any data
                {
                    byte[] data = new byte[1200];
                    int received = s.Receive(data);
                    player.ProcessReceivedData(data, received);
                }
            }
        }

        public void SendData(byte[] message)
        {
            if (state == true)
            {
                s.Send(message);
            }
        }

        public void SendData(byte[] message, int size)
        {
            if (state == true)
            {
                s.Send(message, size, SocketFlags.None);
            }
        }

        public void Start(string p_ipAddress, int listenPort)
        {
            //IPAddress ipAddress = IPAddress.Loopback
            IPAddress ipAddress = IPAddress.Parse(p_ipAddress);

            IPEndPoint ipLocalEndPoint = new IPEndPoint(ipAddress, listenPort); 

            //listener = new TcpListener(ipAddress, listenPort);
            listener = new TcpListener(ipLocalEndPoint);
            server = "[provider]";
            port = listenPort;
            listener.Start();
            form.Debug("Server is running", "Player1Net");
            form.Debug("Listening on port " + listenPort, "Player1Net");
            form.Debug("Waiting for connections...", "Player1Net");
            s = listener.AcceptSocket();
            form.Debug("Connection accepted from " + s.RemoteEndPoint, "Player1Net");
            state = true;
        }

        #endregion Methods 
    }


有没有办法检查出什么问题?非常感谢帮助!



1> Martin Vseti..:

我发现了什么问题.我正在收听10.0.0.1并尝试访问我的外部IP(我的程序的第二个实例),这在连接到Internet的计算机上是不可能的.

推荐阅读
跟我搞对象吧
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有