0
[2013-01-29 09:17:50] INFO  WEBrick 1.3.1
[2013-01-29 09:17:50] INFO  ruby 1.8.7 (2012-10-12) [i386-linux]
[2013-01-29 09:17:50] WARN  TCPServer Error: Address already in use - bind(2)
[2013-01-29 09:17:50] INFO  WEBrick::HTTPServer#start: pid=4107 port=8080

当我在 linux 中运行下面附加的文件时,我得到了描述的错误。我在网上尝试了所有可能的命令和策略来监听进程(包括流氓)并杀死它们。我在很多港口都这样做过。没运气。

只要我在 Mac OS 中运行脚本并且它就可以工作。尽管如此,我必须将它安装在服务器上,并且客户端必须与之通信。它发生在亚马逊 ec2 和 heroku 的每个实例上。我已经多次看到这个错误并花了很多时间试图修复它。我配置了ec2实例的安全组,还是不行。我已经绝望了。此时我不得不认为问题一定是WEBrick本身或我的代码中的某些东西。

    require 'webrick'
    require 'uri'
    require 'net/http'

    $own_address = 8080


    class AuctionInfo
        # The representation is a hash mapping item names to [highest_bidder, highest_bid, end_time]
        def initialize
            @data = {}
        end
        def new_item(item, endTime)
            @data[item] = ["UNKNOWN", 0, endTime]
        end
        def bid(item, bid, client)
            if @data.has_key?(item)
                endTime = @data[item][2]
                if @data[item][1].to_i < bid.to_i and Time.new.to_i < endTime.to_i
                    @data[item] = [client, bid, endTime]
                end
            end
        end
        def get_status(item)
            if @data.has_key?(item)
                return @data[item][0]
            end
        end
        def winner(item)
            if @data.has_key?(item)
                if @data[item][2].to_i + 1 <= Time.new.to_i
                    return @data[item][0]
                else return "UNKNOWN"
                end
            end
        end
        def reset
            @data = {}
        end
        def has_item(item)
            return @data.has_key?(item)
        end
        def get_data
            return {}.replace(@data) 
        end
    end

class StartAuctionServlet < WEBrick::HTTPServlet::AbstractServlet

    def initialize(server, data)
        @data = data
    end

    def do_POST(request, response)
        if request.query['name'] and request.query['end_time']
            @data.new_item(request.query['name'], request.query['end_time'].to_i)
        end
        response.status = 200
    end
    alias_method :do_GET, :do_POST
end

class BidServlet < WEBrick::HTTPServlet::AbstractServlet

    def initialize(server, data)
        @data = data
    end

    def do_POST(request, response)
        if request.query['name'] and request.query['client'] and request.query['bid']
            @data.bid(request.query['name'], request.query['bid'].to_i, request.query['client'])
        end
        response.status = 200
    end
    alias_method :do_GET, :do_POST
end

class StatusServlet < WEBrick::HTTPServlet::AbstractServlet

    def initialize(server, data)
        @data = data
    end

    def do_GET(request, response)

        if request.query['name']
            response.body = @data.get_status(request.query['name'])
        end
        response.status = 200
    end
    alias_method :do_POST, :do_GET
end

class WinnerServlet < WEBrick::HTTPServlet::AbstractServlet

    def initialize(server, data)
        @data = data
    end

    def do_GET(request, response)
        if request.query['name']
            response.body = @data.winner(request.query['name'])
        end
        response.status = 200
    end
    alias_method :do_POST, :do_GET
end

class ResetServlet < WEBrick::HTTPServlet::AbstractServlet

    def initialize(server, data)
        @data = data
    end
    def do_POST(request, response)
        @data.reset
        response.status = 200
    end
    alias_method :do_GET, :do_POST
end

class RandomServlet < WEBrick::HTTPServlet::AbstractServlet
    def initialize(server, data)
        @data = data
    end
    def do_GET(request, response)
        response.status = 200
        response.body = @data.get_data.to_s
    end
    alias_method :do_POST, :do_GET
end




data = AuctionInfo.new
server = WEBrick::HTTPServer.new(:Port => $own_address)
server.mount '/start_auction', StartAuctionServlet, data
server.mount '/bid', BidServlet, data
server.mount '/status', StatusServlet, data
server.mount '/winner', WinnerServlet, data
server.mount '/rst', ResetServlet, data
server.mount '/', RandomServlet, data
trap("INT") { server.shutdown }
server.start
4

1 回答 1

2

您是否检查过 linux 服务器是否正在运行 apache、tomcat、trinidad 或任何其他 Web 服务器?奇怪的是其中之一已经在服务器的端口 8080 上运行。

lsof 是一个有用的命令。试试看lsof | grep 8080有没有出现

于 2013-01-29T13:01:05.487 回答