Ruby socket programming in CloudFoundry

This part is some work of code analysis of CF.

First, let's look at the ../vcap/common/lib/vcap/common.rb which defines some interesting methods to use.

  def self.grab_ephemeral_port
    socket = TCPServer.new('0.0.0.0', 0)
    socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)
    Socket.do_not_reverse_lookup = true
    port = socket.addr[1]
    socket.close
    return port
  end

As we have had the basic knowledge of socket programming in last post, we can refer everything now from:

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/socket/rdoc/index.html

new([hostname,] port) => tcpserver

Creates a new server socket bound to port.

If hostname is given, the socket is bound to it.

setsockopt(level, optname, optval)

Sets a socket option. 
  
level is an integer, usually one of the SOL_ constants such asSocket::SOL_SOCKET, or a protocol level.

optname is an integer, usually one of the SO_ constants, suchas Socket::SO_REUSEADDR. 

optval is the value of the option, it is passed to theunderlying setsockopt() as a pointer to a certain number of bytes. How this is done depends on the type:

        Fixnum: value is assigned to an int, and a pointer to the int is passed,with length of sizeof(int).

        true or false: 1 or 0 (respectively) is assigned to an int, and the int ispassed as for a Fixnum. Note that false must be passed, notnil.

        String: the string's data and length is passed to the socket.

socketoption is an instance of Socket::Option


do_not_reverse_lookup = true # turn off reverse DNS resolution temporarily

port = socket.addr[1] # This will return the port? That means the addr[0] is host?

Based on the knowledge above. There's also a trick to get server IP and Client IP in ruby on rails:

Write the code in Controller

For Client IP:

request.remote_ip

@remote_ip = request.env["HTTP_X_FORWARDED_FOR"]

For Server IP:

require 'socket'

def local_ip
  orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true  # turn off reverse DNS resolution temporarily

  UDPSocket.open do |s|
    s.connect '64.233.187.99', 1
    s.addr.last
  end
ensure
  Socket.do_not_reverse_lookup = orig
end


That is what CloudFoundry do in method ../common.rb#local_ip

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值