简体   繁体   English

在运行时ip服务器的地址

[英]ip adress of a server at runtime

I developed a client task in C and a server task in C. The client needs the server IP address to connect to the server. 我在C中开发了一个客户端任务,在C中开发了一个服务器任务。客户端需要服务器IP地址才能连接到服务器。 The problem is: I don't know in advance which machine will run the client and which machine will run the server so I can know the ip address of the server only at runtime My question: how at run time the client will know the ip address of the server? 问题是:我事先不知道哪台机器将运行客户端以及哪台机器将运行服务器所以我只能在运行时知道服务器的IP地址我的问题:如何在运行时客户端知道ip服务器的地址?

As a commenter noted, you might just use DNS. 正如评论者所说,您可能只使用DNS。 If you are not able to obtain a DNS-entry for whatever reasons, or the server and the clients will run on the same network, you might consider using some kind of mDNS, with wich you wont need a DNS-Server with your record up and running. 如果由于某种原因无法获得DNS条目,或者服务器和客户端将在同一网络上运行,您可能会考虑使用某种mDNS,因为您不需要使用DNS-Server进行记录并运行。 Visit http://en.wikipedia.org/wiki/MDNS#Multicast_DNS for a introductionary overview 访问http://en.wikipedia.org/wiki/MDNS#Multicast_DNS以获取介绍性概述

Use a third-party broker server, that both the client and server knows about in advance. 使用客户端和服务器提前知道的第三方代理服务器。

Just set up a server anywhere with a known IP address. 只需在任何具有已知IP地址的服务器上设置即可。 (Using a cloud server is a cheap and quick option.) (使用云服务器是一种便宜且快速的选择。)

When the server starts, it sends its IP address to the broker server, and when the client starts, it querys the broker server for a server to connect to. 当服务器启动时,它会将其IP地址发送到代理服务器,当客户端启动时,它会查询代理服务器以获取要连接的服务器。

Of course you have to write a protocol for this, but it can be extremely simple. 当然,你必须为此编写一个协议,但它可以非常简单。

first of all you must remember how big the network you want to search? 首先,您必须记住要搜索的网络有多大? and if it will be a bit problem for you then you can use the DNS its really useful..... 如果它对您来说有点问题,那么您可以使用它真正有用的DNS .....

如果您的服务器和客户端将在本地网络上运行,请使用这个漂亮的MAC地址:FF-FF-FF-FF-FF-FF ...;)但如果名称保持不变,DNS将是最好的。

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

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