gpt4 book ai didi

.net - 在同一台计算机上调试TcpClient和TcpListener

转载 作者:行者123 更新时间:2023-12-03 12:06:27 28 4
gpt4 key购买 nike

这里是网络编程的新手。

我有一个两部分的应用程序。我正在尝试在本地调试它。
服务在new IPEndPoint(IPAddress.Any, 3000)上监听连接。
调用tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 3000))会引发No connection could be made because the target machine actively refused it 127.0.0.1:3000。 Windows防火墙已关闭。

我在做一些根本上愚蠢的事情吗?

最佳答案

您应该在TcpListener上调用Start方法以使其正常工作,否则它将不接受任何连接。

我已经测试过了,此代码段有效:)

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

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int port = 5124;

var myListener = new TcpListener(new IPEndPoint(IPAddress.Any, port));
myListener.Start();

var tcpClient = new TcpClient();
tcpClient.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), port));

tcpClient.Close();

}
}
}

关于.net - 在同一台计算机上调试TcpClient和TcpListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7519091/

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