如何获得连接的客户端的IP地址? [英] How can I get a connected client's IP address?
问题描述
如果我有一个超级简单的线程式TCP服务器,如:
If I have a Super Simple Threaded TCP Server like:
USING: accessors io io.encodings.utf8 io.servers
io.sockets kernel prettyprint threads ;
: handle-client ( -- )
remote-address . ;
: <my-server> ( -- threaded-server )
utf8 <threaded-server>
"server" >>name
1234 >>insecure
[ handle-client ] >>handler ;
: start-my-server ( -- )
<my-server> [ start-server ] in-thread start-server drop ;
这只会将文本remote-address
打印到客户端,这对非常有用很有帮助.这是因为remote-address
是一个符号...它的值在哪里?
remote-address
的文档说:
This will just print the text remote-address
to the client, which is very helpful. That's because remote-address
is a symbol... where's its value?
The documentation for remote-address
says:
包含当前客户端连接的地址说明符的变量.
Variable holding the address specifier of the current client connection.
<threaded-server>
上的文档说:
线程服务器实例的处理程序插槽应设置为 处理客户端连接的报价.客户端处理程序在 自己的线程,具有以下变量反弹:
The handler slot of a threaded server instance should be set to a quotation which handles client connections. Client handlers are run in their own thread, with the following variables rebound:
•输入流
•输出流
•本地地址
•远程地址
•线程服务器
• input-stream
• output-stream
• local-address
• remote-address
• threaded-server
太好了!这意味着我可以获取客户的IP.
Great! That means I can get at a client's IP.
然后将其链接到似乎相关的地址说明符 ,但并未明确说明如何从remote-address
获取数据.
Then it links to Address specifiers, which seems to be related, but doesn't clearly explain how to get data from remote-address
.
如何获取客户的IP地址?
How can I get a client's IP address?
推荐答案
get
将使用variable
和get
其值.
@fede s. nailed it in the comments: get
will take a variable
and get
its value.
所以我从问题中得到的代码变为:
So my code from the question becomes:
: handle-client ( -- )
remote-address get host>> print flush ;
: <my-server> ( -- threaded-server )
utf8 <threaded-server>
"server" >>name
1234 >>insecure
[ handle-client ] >>handler ;
: start-my-server ( -- )
<my-server> [ start-server ] in-thread start-server drop ;
这篇关于如何获得连接的客户端的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!