问题描述
嘿,
我第一次尝试编写客户端 - 服务器程序。它在LAN网络上运行良好,但我不能让它在互联网上工作。
我可以使用以下代码轻松地将数据发送到服务器程序:
Hey,
I am trying to write a client-server program for the first time. It works fine on LAN networks but I can't get it to work over internet.
I can easily send data to the server program with the following code:
public static bool sendTo(IPEndPoint ip, String data)
{
using (TcpClient c = new TcpClient())
{
try
{
c.Connect(ip);
NetworkStream clientStream = c.GetStream();
ASCIIEncoding encoder = new ASCIIEncoding();
byte[] buffer = encoder.GetBytes(data);
clientStream.Write(buffer, 0, buffer.Length);
clientStream.Flush();
c.Close();
}
catch
{
c.Close();
return false;
}
}
return true;
}
然后服务器必须使用相同的代码响应客户端,但它无法提供以下内容错误:
由于目标机器主动拒绝它,因此无法建立连接* IP *:* PORT *
我知道问题可以通过端口转发和更改路由器的设置来解决。但我可能会尝试尽快制作软件,用户应该能够以最少的配置使用该软件。所以...我想知道是否有人可以帮助我...
提前致谢,
eLe
Then server has to respond to the client using the same code but it fails giving the following error:
No connection could be made because the target machine actively refused it *IP*:*PORT*
I know the problem can be fixed with port-forwarding and changing the settings of router. But I'll probably try to make a software out of this soon and users should be able to use the software with minimal configurations. So... I was wondering if anyone can help me here...
Thanks in advance,
eLe
推荐答案
这篇关于客户端 - 服务器程序:客户端可以轻松连接到服务器,但服务器无法响应,导致目标机器主动拒绝连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!