Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:35:26+00:00 2026-05-27T17:35:26+00:00

This is my sever require ‘rubygems’ require ‘benchmark’ require ‘eventmachine’ class Handler < EventMachine::Connection

  • 0

This is my sever

require 'rubygems'
require 'benchmark'
require 'eventmachine'
class Handler  < EventMachine::Connection
  def initialize(*args)
    super
  end

  def receive_data(data)
        @state = :processing
    EventMachine.defer(method(:do_something), method(:callback))
        #EM.defer(operation, callback)
  rescue Exception => ex
    LOGGER.error "#{ex.class}: #{ex.message}\n#{ex.backtrace.join("\n")}"
  ensure
    close_connection_after_writing unless @state == :processing
  end

  def do_something
    #simulate a long running request
        for i in 1..1000
            a << rand(1000)
            a.sort!
        end 
      return "response from server"
  end

  def callback(msg)
    self.send_data msg
    @state = :closing
  end

  def unbind
    close_connection_after_writing unless @status == :process 
  end

end

EventMachine::run {
  EventMachine.epoll
  EventMachine::start_server("0.0.0.0", 8080, Handler)
  puts "Listening..."
}

This is my client

require 'rubygems'
require 'benchmark'
require 'socket'
require 'logger'
Benchmark.bm do |x|
  logger = Logger.new('test.log', 10, 1024000) 
  logger.datetime_format = "%Y-%m-%d %H:%M:%S"
  x.report("times:") do
    for i in 1..10
      #Thread.new do
        TCPSocket.open "127.0.0.1", 8080 do |s|
          s.send "#{i}th sending\n", 0  
          if result = s.recv(100)  
            logger.info result
          end
          puts "#{i}th sending"
        #end
      end
    end
  end
end

When i run my client , the server can not receive any data, so i change my server as follow

require 'rubygems'
require 'benchmark'
require 'eventmachine'
class Handler  < EventMachine::Connection
  def initialize(*args)
    super
  end

  def receive_data(data)
        operation = proc do
            # simulate a long running request
            a = []
            for i in 1..1000
                a << rand(1000)
                a.sort!
            end
        end

    # Callback block to execute once the request is fulfilled
    callback = proc do |res|
        send_data "data from server"
    end

        puts data
        EM.defer(operation, callback)
  end
end

EventMachine::run {
  EventMachine.epoll
  EventMachine::start_server("0.0.0.0", 8080, Handler)
  puts "Listening..."
}

It works, i want to know why my first server can not work correctly

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:35:27+00:00Added an answer on May 27, 2026 at 5:35 pm

    The problem is in the first server: you never defined a = [], so an exception was thrown. That exception will, essentially, terminate the thread processing. The callback will never get executed and the server will never respond.

    Since EM.defer works in a thread, the rescue statement in received_data will have no effect. You need a rescue in the do_something method to catch any exceptions that happen while processing.

    The ensure block in receive_data will also have no effect since the EM.defer will return immediately and that block of code will complete. The @state will never be set to anything other then :processing at that point.

    You’ll want to move the close_connection_after_writing into the callback method itself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code to download a file, but on sourceforge.net sever there is
My swing app has a main window which has sever buttons. This JFrame is
why do i get exception on (redirect/get) in this program #lang web-server (require web-server/formlets
My Ubuntu server has Apache and Subversion installed. I use this server as a
In C# you can use verbatim strings like this: @\\server\share\file.txt Is there something similar
When I send an email with the PHP Swift mailer to this server: smtp.exchange.example.com
I need to post some text to a remote server over HTTP, this server
We currently have a website at somedomain.net/codefest. We do not own this server (or
This SQL Server 2005 T-SQL code: DECLARE @Test1 varchar; SET @Test1 = 'dog'; DECLARE
So I run this Windows Server 2008 security update and this code block is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.