c# - 无法通过 Internet 创建 TCP/IP 连接
问题描述
TCP/IP 和 PORT 转发的问题
- 我禁用了防火墙
- 将DMZ设置为我的本地 IP
- 运行了一个 C# 控制台应用程序,它侦听来自端口 8659(服务器)的传入连接:
static void Main(string[] args)
{
// port to listen to
int connPort = 8659;
// the local endPoint
var localEP = new IPEndPoint(IPAddress.Any, connPort);
// creating the socket
using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
// binding the socket to the local endPoint
socket.Bind(localEP);
// listening for incoming connections
socket.Listen(10);
// logging
Debug.WriteLine("waiting for client ...");
// grab the first client
var client = socket.Accept();
// logging
Debug.WriteLine("client connected !");
var jsonContent = "this is json test";
// sending the json content as a json response
client.Send(Encoding.ASCII.GetBytes("HTTP/1.1 200 OK\n" +
"Content-Type: text/plain\n" +
$"Content-Length: {jsonContent.Length}\n" +
"\n" + jsonContent));
} // end of using
Console.ReadLine();
} // end of main
- 尝试使用公共 IP 与另一个应用程序(客户端)连接到服务器:
static void Main(string[] args)
{
var host = Dns.GetHostEntry(Dns.GetHostName());
// the ip address of the interface connected to internet
var ip = host.AddressList[1];
// my public IP ( dynamic ) not yet static, just a test
var ServerIP = IPAddress.Parse("197.119.200.25");
// the server endpoint
var serverEP = new IPEndPoint(ServerIP, 8659);
// the endpoint to use to connect to the server
var localEP = new IPEndPoint(ip, 8658);
// creating the socket
var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
// bind the socket to the endpoint
socket.Bind(localEP);
// connect to the server endpoint
socket.Connect(serverEP);
} // end of mainc
客户抛出:
System.Net.Sockets.SocketException: '连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立连接失败,因为连接主机没有响应 197.119.200.25:8659'
所以我的问题是为什么机器的公共 IP 无法访问我的服务器应用程序?
我不知道是我的 ISP 阻塞了端口,还是我有另一个我不知道的问题!
解决方案
推荐阅读
- string - bash 文件子字符串仅在匹配项中添加/替换
- html - 如何慢慢改变单词颜色?
- python - 有什么方法可以在 OpenCV 的轮廓检测中只检测矩形,而忽略其他文本检测?
- c++ - 如何从字符串文字和附加的 int 或 long 值有效地创建 const c 字符串
- sql - 直接引用 Pivot 列允许 ISNULL
- reactjs - 在带有 JSX 的锚标记内使用图像标记
- javascript - 仅将对象数组的值推入另一个数组
- r - 将列内的字符串拆分为固定长度的子字符串
- python - Django migrate --fake-initial 报告关系已经存在
- three.js - 如何在 Autodesk Forge 中动态更新 THREE.js PointCloud 覆盖