This is not very clean but this is a way you can get the port you declared when calling :
rails server -p portnumber
wherever you want in your application (for rails 3).
Here is my scripts/rails.rb file :
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails"
# with Rails 3 gems installed from the root of your application.
ENV['PORT']=ARGV[3]
APP_PATH = File.expand_path('../../config/application', __FILE__)
require File.expand_path('../../config/boot', __FILE__)
require 'rails/commands'
Then whenever you want to get the port number of your server, all you need to do is call ENV['PORT']
.