如何获得使用 TcpClient 进行连接的基本应用程序?
我正在尝试编写一个基本的应用程序来从端口读取数据。这是我的代码:
static void Main(string[] args)
{
int port = 5600;
string server = "MyDevLaptopName";
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, port);
NetworkStream stream = tcpClient.GetStream();
byte[] data = new byte[256];
stream.Read(data, 0, data.Length);
Console.ReadLine();
}
当我运行上面的应用程序时,我在 tcpClient.Connect
命令上收到此错误:
连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机无法响应 10.90.91.19:5600
我不确定为什么我的计算机不会响应(我对TCP 端口等)。
是否需要通过其他方式进行设置以从 TCP 端口读取数据?
我已经检查过的事项:
- Windows 防火墙已完全关闭。
- 错误中列出的 IP 地址是我的开发计算机(我尝试连接的计算机)的内部 IP 地址。
- 我确实有 Symantec Endpoint Protection,但它由组策略控制,我很难将其关闭(但如果需要,我可以这样做)。
更新:
有一个主机服务器应该向该端口发送 TCP 数据。我不控制该机器/服务器或其发送数据的方法。我只是想读取发送到端口的数据。
更新二:
主机实际上是将数据发送到我公司的虚拟机。然后我使用找到的技术 此处将该 TCP 流量重定向到我机器上的同一端口(我希望我可以在其中读取它)。
更新三:
大型机是客户端,我需要成为服务器!一旦我意识到我的工作正常了。
I am trying to write a basic app that will read data off a port. Here is my code:
static void Main(string[] args)
{
int port = 5600;
string server = "MyDevLaptopName";
TcpClient tcpClient = new TcpClient();
tcpClient.Connect(server, port);
NetworkStream stream = tcpClient.GetStream();
byte[] data = new byte[256];
stream.Read(data, 0, data.Length);
Console.ReadLine();
}
When I run the app above I get this error on the tcpClient.Connect
command:
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 10.90.91.19:5600
I am not sure why my computer will not respond (I am totally new to TCP ports and such).
Is there a different way I need set this up to read data from a TCP port?
Things I have checked:
- Windows Firewall is completely off.
- The IP address listed in the error is the internal IP address of my development machine (the machine I am trying to connect to).
- I do have Symantec Endpoint Protection, but it is controlled by Group Policy and it would be difficult for me to get turned off (but I can do it if need be).
Update:
There is a main frame server that is supposed to be sending TCP data to that port. I do not control that machine/server or the method that it sends the data. I am just trying to read the data that is sent to the port.
Update II:
The main frame is actually sending the data to a VM at my company. I then use the technique found here to redirect that TCP traffic to the same port on my machine (where I hoped I could just read it off).
Update III:
The mainframe is the client and I need to be the server!. Once I realized that I got things working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试使用另一个端口号,你也控制服务器吗?如果是这样,请尝试按照以下步骤操作:
C# 教程 - 简单线程 TCP 服务器
正如您将在那里看到的,您可以用这种方式监听:
并且您可以用这种方式连接:
这看起来微不足道,确实如此,但端口号不是详细信息,因为某些端口不可用或被防火墙阻止等...
I would try with another port number, are you in control of the server as well? if so try to follow these steps:
C# Tutorial - Simple Threaded TCP Server
as you will see there you can listen in this way:
and you can connect in this way:
it seems trivial and it is, but port number is not a detail as some ports are not available or blocked by firewalls or so...
您的代码是“客户端”。您需要在另一台机器上运行一个“服务器”来监听您的传入连接。
Your code is the "client." You need a "server" running on the other machine that is listening for your incoming connection.