客户端 - 服务器程序:客户端可以轻松连接到服务器,但服务器无法响应,导致目标机器主动拒绝连接 [英] Client-Server Program: Client can connect to server easily but server can't respond cause target machine actively refuses connections

查看:98
本文介绍了客户端 - 服务器程序:客户端可以轻松连接到服务器,但服务器无法响应,导致目标机器主动拒绝连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,

我第一次尝试编写客户端 - 服务器程序。它在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

推荐答案

您需要重新设计您的架构。对于服务器,使用某种Web服务,例如, WCF。然后,您的客户端将向该服务发送请求并接收答案作为某种返回值。

这样,服务器就不需要连接到客户端。
You'll need to re-design your architecture. For the server, use some kind of a Web Service, e.g. WCF. Your client will then send requests to that service and receive an answer as some kind of return value.
That way, there is no need for the server to connect to the client.


这篇关于客户端 - 服务器程序:客户端可以轻松连接到服务器,但服务器无法响应,导致目标机器主动拒绝连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆