How can I get a connected client's IP address?

岁酱吖の 提交于 2019-12-10 23:28:58

问题


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 ;

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.

And the docs on <threaded-server> say:

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

Great! That means I can get at a client's IP.

Then it links to Address specifiers, which seems to be related, but doesn't clearly explain how to get data from remote-address.

How can I get a client's IP address?


回答1:


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


来源:https://stackoverflow.com/questions/36512483/how-can-i-get-a-connected-clients-ip-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!