简体   繁体   English

C# 控制台应用程序通过互联网配对

[英]C# Console-Application paired over internet

I want to build a C# chat like app, where you can enter a string into the Client console and receive it in the Server console.我想构建一个类似应用程序的 C# 聊天,您可以在其中在客户端控制台中输入一个字符串并在服务器控制台中接收它。

What I mean: User B executes the Client Program on his Computer, which is anywhere else than at User A. The Client Program connects over TCP to the Server Program, which has User A opened on his Computer.我的意思是:用户 B 在他的计算机上执行客户端程序,它在用户 A 之外的任何地方。客户端程序通过 TCP 连接到服务器程序,用户 A 在他的计算机上打开。 When User B enters a string in the Program and press enter, User A should receive the string over Internet.当用户 B 在程序中输入一个字符串并按回车键时,用户 A 应通过 Internet 接收该字符串。

I have something like that:我有这样的事情:

Server:服务器:

static void Main(string[] args)
{

    Console.WriteLine("Start Server ...");


    IPAddress b = IPAddress.Any;
    TcpListener Server = new TcpListener(b, 5550);
    Server.Start();
    Console.WriteLine("Server started.");

    Console.WriteLine("Wait for Connection...");
    TcpClient Client = Server.AcceptTcpClient();
    Console.WriteLine("Succeeded");

    Stream MessageStream = Client.GetStream();

    while (true)
    {
        byte[] message = new byte[4096];
        int bytesRead;
        try
        {
            bytesRead = MessageStream.Read(message, 0, 4096);

            ASCIIEncoding encoder = new ASCIIEncoding();
            Console.WriteLine(encoder.GetString(message, 0, bytesRead));
        }

        catch (IOException)
        {
            break;
        }
    }

    Client.Close();
    Server.Stop();
}

Client:客户:

static void Main(string[] args)
{
    IPAddress IP = IPAddress.Parse("192.168.2.140");
    TcpClient Client = new TcpClient();
    Client.Connect(IP, 5550);

    Stream MessageStream = Client.GetStream();
    Console.WriteLine("Success");

    ASCIIEncoding encoder = new ASCIIEncoding();

    Console.WriteLine("Enter Text here");

    var input = Console.ReadLine();
    if(input != null)
    {
        byte[] buffer = encoder.GetBytes(input);

        MessageStream.Write(buffer, 0, buffer.Length);
        MessageStream.Flush();
    }

    Console.ReadLine();
}

When I execute the Client on my PC and the Server on my Laptop, it works fine, but how does it work with outside-of-my-local-network-Computers?当我在我的 PC 上执行客户端和我的笔记本电脑上的服务器时,它工作正常,但它如何与我的本地网络计算机之外的计算机一起工作?

When I replaced当我更换
IPAddress IP = IPAddress.Parse("192.168.2.140"); with the WAN IP, which I get from canihazip.com, it doesn't want to connect.使用我从 canihazip.com 获得的 WAN IP,它不想连接。

EDIT:编辑:

Port-Forwarding is activated i guess我猜端口转发已激活转发端口

The Problem is: It works in the local Network at my home as i want, it connects, i can write a text, which shows up in the server-application.问题是:它可以在我家的本地网络中按照我的需要工作,它可以连接,我可以编写文本,该文本显示在服务器应用程序中。 But when i ask some of my friends to try this, they cant connect to my server..但是当我让我的一些朋友尝试这个时,他们无法连接到我的服务器..

You need to go to your router's admin interface, and forward connections on certain ports from the internet to the IP address of the system behind the network.您需要转到路由器的管理界面,并将某些端口上的连接从 Internet 转发到网络后面系统的 IP 地址。 That will be entirely dependent on your router and quick google search will tell you what you need to know.这将完全取决于您的路由器,快速的谷歌搜索会告诉您需要了解的内容。

Update from OP edit:从 OP 编辑​​更新:

So from there, you access it via the public ip with the public port, and set your system to listen on the LAN port.因此,从那里,您可以使用公共端口通过公共 ip 访问它,并将系统设置为侦听 LAN 端口。 Another effective thing to help will be to use a free dynamic dns service, such as duckdns.com whom I've used before and found to be good and easy enough, to get an actual name that will always map to your public ip address which - if you're not purchasing a static ip address from your ISP - is dynamic and will changein the future.另一个有效的帮助是使用免费的动态 dns 服务,例如我以前使用过的duckdns.com,我发现它很好也很容易,以获得一个始终映射到您的公共 IP 地址的实际名称- 如果您不是从您的 ISP 购买静态 IP 地址 - 是动态的,将来会发生变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM