如何在应该动态生成的文本框中显示ip地址和端口号

发布于 2024-08-18 03:59:04 字数 79 浏览 2 评论 0原文

有没有办法在动态生成的文本框中显示系统IP地址和端口号???

我希望系统根据机器将IP地址放入文本框中。

悉达多

Is there a way to display the system ip address and port number in a text box that is generated dynamically???

I want the system to put the ip address into a text box according to the machine.

Siddharth

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

缪败 2024-08-25 03:59:04

既然你提到了一个文本框,我只能假设你正在谈论一个网络浏览器,在这种情况下,99.9% 的时间你正在谈论 http,然后 99.999% 的时间你正在谈论 TCP 连接。这意味着您的连接将具有由源 ip:port 和目标 ip:port 组成的 4 元组。在大多数情况下,目的地(客户端)的端口号是相当标准的 (80)。

然后你会遇到 NAT 等非常常见的问题,所以我再次认为你需要澄清你想要什么类型的 IP 地址。公共可路由的 IP 地址是从服务器端获取的,LAN 地址将从本地主机获取。

对于更有趣的情况(公共可路由 ip),我只使用服务器端脚本(python、PHP、C 等)来读取传入的 ip 地址,然后使用一点 ajax 来设置文本框的值。我为一个项目做了类似的事情,效果非常好。我们的客户端程序是用 Python 和 C 编写的,但这会给您一个想法...

# Returns the client's public IP address (past any NATs)
def get_public_ip():
        return urllib.urlopen('http://ddih.org/ip.php').read().strip()

我想从该网页设置内部 html...

希望这会有所帮助。

Since you mentioned a text box, I can only postulate that you are talking about a web browser, and in that case 99.9% of the time you are talking about http and then 99.999% of the time a TCP connection. This means that your connection will have a 4-Tuple consisting of the source ip:port and the destination ip:port. In most cases the port numbers are fairly standard (80) for the destination (client).

Then you get into the very common issues of NAT and the like, so again I think you need to clarify what type of ip address you want. The publicly routable ip address is obtained server side and the LAN address will be obtained from the localhost.

For the more interesting case (publicly routable ip) I would just use a server side script (python, PHP, C, etc...) to read the incoming ip address and then use a little ajax to set the value of the text box. I did something similar for a project and it worked really well. Our client program was written in Python and C but this will give you an idea...

# Returns the client's public IP address (past any NATs)
def get_public_ip():
        return urllib.urlopen('http://ddih.org/ip.php').read().strip()

I think something like set the inner html... from that webpage...

Hope this helps.

过潦 2024-08-25 03:59:04

您的系统没有端口端口号。端口号是一个软件概念,用于区分可能想要侦听您的 IP 地址上的连接的不同 IP 或 UDP 应用程序。

此外,很可能拥有多个 IP 地址。事实上,如果算上环回地址 (127.0.0.1),您的系统几乎总是有两个。即使您现在不这样做,许多消费电脑也有多个以太网插孔。

您没有说您使用的是 Win32,所以我不知道它对您有用,但这是我曾经编写的一些代码,它将所有本地 IP 地址(环回除外)放入 MFC CComboBox 中。它比我现在希望看到的更 C 风格,但就是这样。

size_t const Max_Expected_Addresses = 20; // Something rediculous
unsigned long IPADDRTBL_Size = sizeof(DWORD) + sizeof(MIB_IPADDRROW) * Max_Expected_Addresses;
PMIB_IPADDRTABLE IP_Address_Table = (PMIB_IPADDRTABLE) malloc (IPADDRTBL_Size);
if (GetIpAddrTable (IP_Address_Table, &IPADDRTBL_Size, TRUE) == NO_ERROR) {
    for (DWORD i = 0; i < IP_Address_Table->dwNumEntries; i++) {
        // Skip the loopback.
        if (IP_Address_Table->table[i].dwAddr == 0x0100007f) continue;

        if (m_IP_Address == "") m_IP_Address = String_Address(IP_Address_Table->table[i].dwAddr);
        m_IP_Address_List.AddString (String_Address(IP_Address_Table->table[i].dwAddr));
    };
}

m_IP_Address_List 是一个 MFC 控件,定义为 CComboBox,由此代码段填充。
m_IP_Address 是一个与 MFC 文本框控件 (IIRC) 绑定的 CString,我用它来存储当前选择的(或启动时首次找到的)IP 地址。

Your system does not have a port number. Port numbers are a software concept to differentiate different IP or UDP applications that might want to listen for connections on your IP address.

Also, it is quite possible to have more than one IP address. In fact, your system almost always has two if you count the loopback address (127.0.0.1). Even if you don't these days even many consumer PC's have multiple ethernet jacks.

You didn't say you were using Win32 so I don't know that it will be useful to you, but here's some code I wrote once that puts all local IP addresses (loopback excepted) into a an MFC CComboBox. It's a bit more C-ish than I'd like to see these days, but here it is.

size_t const Max_Expected_Addresses = 20; // Something rediculous
unsigned long IPADDRTBL_Size = sizeof(DWORD) + sizeof(MIB_IPADDRROW) * Max_Expected_Addresses;
PMIB_IPADDRTABLE IP_Address_Table = (PMIB_IPADDRTABLE) malloc (IPADDRTBL_Size);
if (GetIpAddrTable (IP_Address_Table, &IPADDRTBL_Size, TRUE) == NO_ERROR) {
    for (DWORD i = 0; i < IP_Address_Table->dwNumEntries; i++) {
        // Skip the loopback.
        if (IP_Address_Table->table[i].dwAddr == 0x0100007f) continue;

        if (m_IP_Address == "") m_IP_Address = String_Address(IP_Address_Table->table[i].dwAddr);
        m_IP_Address_List.AddString (String_Address(IP_Address_Table->table[i].dwAddr));
    };
}

m_IP_Address_List is an MFC control defined as a CComboBox which gets filled in by this snippet.
m_IP_Address is a CString tied to an MFC textbox control (IIRC) which I use to store the currently selected (or first found on startup) IP address.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文