简体   繁体   English

C#/ winforms IP地址问题中的客户端/服务器聊天程序

[英]client/server chat program in C#/winforms IP address issue

I am trying to make a winforms client/server chat application. 我正在尝试创建一个winforms客户端/服务器聊天应用程序。 I already have a client and a server, both are working fine, i can log in, send messages, etc .. The problem is that it only works if i connect to my local IP. 我已经有一个客户端和一个服务器,两者都工作正常,我可以登录,发送消息等。问题是它只有在我连接到我的本地IP时才有效。 When i try to run the program with my external IP, i get the following error: 当我尝试使用我的外部IP运行程序时,我收到以下错误:

"The requested address is not valid in its context".

In other words, i can run the server on my own computer, start up (for example) 3 clients and let them connect to the running server. 换句话说,我可以在我自己的计算机上运行服务器,启动(例如)3个客户端并让它们连接到正在运行的服务器。 This basically means that i'm talking to myself. 这基本上意味着我在和自己说话。 What im trying to do is to run the chat server on my computer, and let other people run the client-program and connect to the server through the internet. 我想要做的是在我的计算机上运行聊天服务器,让其他人运行客户端程序并通过互联网连接到服务器。

this is the part where i get an error when i enter my external IP: 这是我输入外部IP时出错的部分:

public void StartListening()
    {

        // Get the IP of the first network device, however this can prove unreliable on certain configurations
        IPAddress ipaLocal = ipAddress;

        // Create the TCP listener object using the IP of the server and the specified port
        tlsClient = new TcpListener(ipaLocal, 50702);

        // Start the TCP listener and listen for connections
        tlsClient.Start(); <--- THINGS GO SOUTH HERE

        // The while loop will check for true in this before checking for connections
        ServRunning = true;

        // Start the new tread that hosts the listener
        thrListener = new Thread(KeepListening);
        thrListener.Start();
    }

im not sure if what i'm trying to do is possible? 我不确定我想做什么是可能的? i can't imagine it is, but i kinda dont know how to proceed with this. 我无法想象它,但我有点不知道如何继续这一点。 I'm new to network programming so any help will be appreciated. 我是网络编程的新手,所以任何帮助都将受到赞赏。

kind regards, Jane 亲切的问候,简

Jane, 简,

I think your issue is with your IP address setup. 我认为您的问题在于您的IP地址设置。 This is a network connection problem. 这是一个网络连接问题。 You need external IP address so that outside clients can contact your PC. 您需要外部IP地址,以便外部客户可以联系您的PC。 You need more advanced networking. 您需要更高级的网络。 The IP address provided from you ISP is used for domestic uses. 您提供的IP地址用于家庭用途。 You need specialized public IP address so that clients can find you outside of the firewall. 您需要专门的公共IP地址,以便客户端可以在防火墙之外找到您。 This is networking/ISP/external IP issue. 这是网络/ ISP /外部IP问题。

Your server application needs to listen to incoming connections on a specified port, on a specified locally installed NIC. 您的服务器应用程序需要侦听指定端口上指定本地安装的NIC上的传入连接。 That is why TcpListener always needs to be created using a local IP address: because it only cares which NIC (if you have multiple installed) it should use. 这就是为什么总是需要使用本地 IP地址创建TcpListener原因:因为它只关心哪个NIC(如果你有多个安装)应该使用它。

The MSDN page for TcpListener also states it explicitly: TcpListener的MSDN页面也明确说明了它:

TcpListener Constructor (IPAddress, Int32) Initializes a new instance of the TcpListener class that listens for incoming connection attempts on the specified local IP address and port number. TcpListener构造函数(IPAddress,Int32)初始化TcpListener类的新实例,该实例侦听指定本地 IP地址和端口号上的传入连接尝试。

External IP address is completely irrelevant to a TCP/IP server. 外部IP地址与TCP / IP服务器完全无关。 You can have numerous routers and network devices along the way, which can then forward incoming connections to your machine. 您可以沿途拥有众多路由器和网络设备,然后可以将传入连接转发到您的计算机。

Just do make sure that your firewall and router are configured properly to allow incoming connections on a specified port. 只需确保正确配置防火墙和路由器以允许指定端口上的传入连接。 To do that, start your TCP/IP server to open the port, and then use a service like CanYouSeeMe to see if server can be reached from outside. 为此,启动TCP / IP服务器以打开端口,然后使用CanYouSeeMe之类的服务查看是否可以从外部访问服务器。

Regarding your comment ( this can prove unreliable on certain configurations ), it's obviously "unreliable" when you think about it: a laptop can easily have an Ethernet network controller with a completely different IP address than a Wifi network adapter. 关于你的评论( 这在某些配置上可能证明是不可靠的 ),当你想到它时,它显然是“不可靠的”:笔记本电脑可以很容易地拥有一个与Wifi网络适配器完全不同的IP地址的以太网网络控制器。 Your server app should allow the user to select which IP address to use, instead of picking the first address it gets. 您的服务器应用程序应该允许用户选择要使用的IP地址,而不是选择它获得的第一个地址。

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

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