2009-07-27 4 views
5

C#으로 게임을 만들고 있는데 상대방의 진행 상황 (동작 등)을 표시하고 싶습니다. 그래서 나는 게임에서 TCP 프로토콜을 통해 상대방에게 이벤트를 보냅니다."일정 기간 후에 연결된 상대방이 올바르게 응답하지 않아 연결 시도가 실패했습니다."라는 오류를 수정하는 방법은 무엇입니까?

이미 로컬 호스트에 내 응용 프로그램을 시도하고 그것은 작동하지만 인터넷을 통해 통신하기 위해 내 외부 주소를 사용하려고 할 때이 클래스에서 아래 TcpInformer.Connect을 오류() :

연결된 파티 제대로 시간 기간 이후에 응답하지 않았기 때문에

연결 시도가 실패 또는 호스트 연결이 응답 (내 외부 IP 주소) :(포트) 내가 생각

에 실패했기 때문에 설정된 연결 실패 문제는 내가 행동했다는 것이었다. ind NAT. 하지만 이미 IP 10.0.0.1에서 포트 49731에 대한 포트 포워딩을 설정했지만 아무 것도 변경되지 않았습니다.

두 번째 추측은 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

문제가 무엇인지 알아 냈습니다. 나는 10.0.0.1에서 듣고 있었고 인터넷에 하나의 연결로 컴퓨터에서 불가능한 내 외부 IP (내 프로그램의 두 번째 인스턴스)에 도달하려고했습니다. 이 게시물을 삭제해야합니까? –

+0

해결책을 설명하는 답변을 추가하고 동의로 표시하십시오. – Richard

+0

이 게시물을 떠나십시오. 그것은 나를 위해 도움이되었다. –

답변

6

문제점이 무엇인지 알아 냈습니다. 나는 10.0.0.1에서 듣고 있었고 인터넷에 하나의 연결로 컴퓨터에서 불가능한 내 외부 IP (내 프로그램의 두 번째 인스턴스)에 도달하려고했습니다.

0

AWS VPN에서도 같은 문제가 발생했습니다.

proxy.company.com (외부 IP)을 10.0.0.5로 변경했습니다.

이제 작동합니다.

관련 문제