我在这里有一个问题。每次启动我的测试程序时,我都会看到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...");
}
}
}
套接字异常的细节是什么?任何内部异常?详情?请发布。 – BenjaminPaul 2014-12-01 16:35:47
你忘了问一个问题。当你按下“提问问题”按钮时,你应该问一个问题。 – 2014-12-02 18:16:54
伙计,再次阅读文本。这个问题被嵌入到文本中。 “我得到一个socketexception”,基本上尖叫着“为什么!?!” – JacksonBolter 2014-12-02 22:00:18