2014-12-01 132 views
0

我在这里有一个问题。每次启动我的测试程序时,我都会看到SocketException。 Localhost或IP:127.0.0.1没有问题,测试时防火墙关闭。当我在我的学习和在家时,这个错误就会出现。连接尝试失败,因为连接方没有正确回应后

PS。这是一个用Visual Studio 2013 Ultimate编写的C#控制台应用程序。

====源代码====

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace MasserAfTesting { 
    class Program { 
     static void Main(string[] args) { 
      int port = 2520; 
      string ip = "91.236.210.60"; 

      new Thread(new Manager(port).Run).Start(); 
      Thread.Sleep(200); 
      new Thread(new Client(ip, port).Run).Start(); 
     } 
    } 
} 



using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace MasserAfTesting { 
    class Manager { 
     private int port; 


     public Manager(int port) { 
      this.port = port; 
     } 

     public void Run() { 
      TcpListener listener = new TcpListener(port); 
      listener.Start(); 
      while (true) { 
       new Thread(new Worker(listener.AcceptTcpClient()).Run).Start(); 
      } 
     } 

    } 
} 



using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace MasserAfTesting { 
    class Worker { 
     private TcpClient client; 

     public Worker(TcpClient client) { 
      this.client = client; 
     } 

     public void Run() { 
      NetworkStream stream = client.GetStream(); 
      Console.WriteLine("Worker Online..."); 
     } 

    } 
} 



using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net.Sockets; 
using System.Text; 
using System.Threading.Tasks; 

namespace MasserAfTesting { 
    class Client { 

     TcpClient client; 

     public Client(string ip, int port) { 
      this.client = new TcpClient(ip, port); 
     } 

     public void Run() { 
      NetworkStream stream = client.GetStream(); 
      Console.WriteLine("Client online..."); 
     } 
    } 
} 
+1

套接字异常的细节是什么?任何内部异常?详情?请发布。 – BenjaminPaul 2014-12-01 16:35:47

+1

你忘了问一个问题。当你按下“提问问题”按钮时,你应该问一个问题。 – 2014-12-02 18:16:54

+0

伙计,再次阅读文本。这个问题被嵌入到文本中。 “我得到一个socketexception”,基本上尖叫着“为什么!?!” – JacksonBolter 2014-12-02 22:00:18

回答

-1

发现TcpListener需要端口和IP才能正常工作。

-2

未处理的异常:System.Net.Sockets.SocketException:A连接尝试失败,因为连接的方没有正确一段时间后做出反应,或建立的连接失败,因为连接的主机未能响应188.114.140.7:2520 at System.Net.Sockets.TcpClient..ctor(String hostname,Int32 port) at MasserAfTesting.Client..ctor(String ip,Int32 port)在c:\ Users \ Jacob \ Documents \ Visual Studio 2013 \ Projects \ SWA \ MasserAfTesting \ MasserAfTesting \ Client.cs中:第14行 位于MasserAfTesting.Program.Main(String [] args) \ Jacob \ Documents \ Visual Studio 2013 \ Projects \ SWA \ MasserAfTesting \ MasserAfTesting \ Program.cs:第16行

在IP中的差异的原因是因为此测试是从家里完成,但源代码是与IP从学校。

相关问题