gpt4 book ai didi

c# - 无法建立连接,因为目标机器主动拒绝它 - 使用 Socket 或 TcpClient

转载 作者:行者123 更新时间:2023-11-30 13:28:45 32 4
gpt4 key购买 nike

很多人都有同样的问题,但是每个人的实现都不一样。

我在实现方面需要帮助。

void sendUsingTcp() 
{
try
{
Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(172.16.8.200), 8000);
sock.Connect(endPoint);
// code past this never hits because the line above fails
}
catch (SocketException err)
{
MessageBox.Show(err.Message);
}
}

我也直接尝试了 TCP Client 也出现了同样的错误结果:

void sendUsingTcp() 
{
try
{
using (TcpClient client = new TcpClient())
{
IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(172.16.8.200), 8000);
client.Connect(endPoint);
// code past this never hits because the line above fails
}
}
catch (SocketException err)
{
MessageBox.Show(err.Message);
}
}

我电脑的 IP 地址是 172.16.11.144

最佳答案

结果对我来说,我的服务器只监听来自同一 IP 的传入连接。我必须更改代码才能从任何 IP 监听。

之前和错误:

IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse(172.16.8.200), 8000);

之后问题已解决:

IPEndPoint endPoint = new IPEndPoint(IPAddress.Any, 8000);

关于c# - 无法建立连接,因为目标机器主动拒绝它 - 使用 Socket 或 TcpClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4166167/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com