如何从打开的TCPConn *中检索IP地址? [英] How can you retrieve the IP address from an open TCPConn*?

查看:74
本文介绍了如何从打开的TCPConn *中检索IP地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从TCPListener.acceptTCP接受的TCPConn *.我只想获取IP地址(无端口信息)作为字符串或IP.什么是正确的方法(即最少的转换/字符串操作)?

I have a TCPConn* that I've accepted from a TCPListener.acceptTCP. I'd like to get only the IP address (no port information) as a string or IP. What is the correct (i.e. minimal casting/string manipulation) way to accomplish that?

推荐答案

要获取IP作为IP对象,最直接的方法应该是

To get the IP, as an IP object, the most straightforward way should be

tcpconn.RemoteAddr().(*net.TCPAddr).IP

使用类型断言没有错,在这种情况下,这实际上是预期的.

There is nothing wrong with using type assertions, and in cases like this it's actually expected.

这篇关于如何从打开的TCPConn *中检索IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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