gpt4 book ai didi

sockets - 如何获得连接的客户端的IP地址?

转载 作者:行者123 更新时间:2023-12-03 11:53:58 24 4
gpt4 key购买 nike

如果我有一个 super 简单线程化TCP服务器,例如:

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-addressdocumentation表示:

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。

然后,它链接到似乎相关的 Address specifiers,但没有明确说明如何从 remote-address获取数据。

如何获得客户的IP地址?

最佳答案

@fede s.在注释中添加了该名称: get 将采用variable并使用get作为其值。

所以我从问题中得到的代码变为:

: 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 ;

关于sockets - 如何获得连接的客户端的IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36512483/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com