Network program

I am writing an application and my design is for a non blocking tcp server. I have written non blocking network apps in C before.

I am having trouble digging through the standard library. I have done this before in C with no problems. Are there some basic wrappers for the normal C functions and constants? I couldn't even find the F_SETFL constant to change the socket descriptor to non blocking. The constants seem to be all over the place (some in Socket, but I also need File::NONBLOCK, which is named differently than O_NONBLOCK...). When reading the documentation it's asking for a packed sockaddr to be passed to connect.

Is this really the simplest, ruby-like way to build a non-blocking network app? It seems like this is harder and messier than C. It's also hard to find the constants and functions even if they do exist.

I really like the Ruby language and this would be my first real Ruby project (of course I'm still in the prototype stage). However, it seems like even the basic parts of the standard library could either use some work or some better documentation.

Regards,
  Jeff Davis

Hi,

I am writing an application and my design is for a non blocking tcp
server. I have written non blocking network apps in C before.

I am having trouble digging through the standard library. I have done
this before in C with no problems. Are there some basic wrappers for the
normal C functions and constants? I couldn't even find the F_SETFL
constant to change the socket descriptor to non blocking. The constants
seem to be all over the place (some in Socket, but I also need
File::NONBLOCK, which is named differently than O_NONBLOCK...). When
reading the documentation it's asking for a packed sockaddr to be passed
to connect.

I use, for ex.

require 'fcntl'

# ...

  sock.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)
  sent = sock.send(str, 0)

# ...

HTH,

Regards,

Bill

···

From: "Jeff Davis" <jdavis-list@empires.org>

"Jeff Davis" <jdavis-list@empires.org> schrieb im Newsbeitrag
news:420C371D.4060604@empires.org...

I am writing an application and my design is for a non blocking tcp
server. I have written non blocking network apps in C before.

I am having trouble digging through the standard library. I have done
this before in C with no problems. Are there some basic wrappers for the
normal C functions and constants? I couldn't even find the F_SETFL
constant to change the socket descriptor to non blocking. The constants
seem to be all over the place (some in Socket, but I also need
File::NONBLOCK, which is named differently than O_NONBLOCK...). When
reading the documentation it's asking for a packed sockaddr to be passed
to connect.

Is this really the simplest, ruby-like way to build a non-blocking
network app? It seems like this is harder and messier than C. It's also
hard to find the constants and functions even if they do exist.

AFAIK Ruby uses nonblocking IO interenally. The easiest is typically to
use threads to cope with multiple concurrent IO. Example

require 'socket'
port = (ARGV[0] || 80).to_i
server = TCPServer.new('localhost', port)
while (session = server.accept)
  Thread.new(session) do |sock|
    begin
      # use sock
    ensure
      sock.close
    end
  end
end

Alternatively you can set up a fixed number of threads that handle
requests.

require 'socket'
require 'thread'

queue = Queue.new

threads = (1..10).map do
  Thread.new(queue) do |q|
    while ( sock = q.deq )
      begin
        # use sock
      rescue Exception => e
        $stderr.puts e
      ensure
        sock.close
      end
    end
  end
end

port = (ARGV[0] || 80).to_i
server = TCPServer.new('localhost', port)
while (session = server.accept)
  queue.enq session
end

Kind regards

    robert

I would say the simplest way would be to download a library that handles this for you. :wink: I know there was one announced here not too terribly long ago, though a quick search didn't turn it up for me. Maybe the author will notice this thread and speak up...

James Edward Gray II

···

On Feb 10, 2005, at 10:40 PM, Jeff Davis wrote:

Is this really the simplest, ruby-like way to build a non-blocking network app? It seems like this is harder and messier than C. It's also hard to find the constants and functions even if they do exist.

I believe Ruby's internal Thread system use Multicasting, but not non-blocking IO. That's why we occasionally see posts pointing out that large write operations still block. Please correct me if I'm wrong though.

James Edward Gray II

···

On Feb 11, 2005, at 3:45 AM, Robert Klemme wrote:

AFAIK Ruby uses nonblocking IO interenally. The easiest is typically to
use threads to cope with multiple concurrent IO.

I finally remembered what it was called and used that to find it. Here's a link to the announcement:

http://groups.google.co.nz/groups?th=662c6276e5df1660

James Edward Gray II

···

On Feb 11, 2005, at 8:39 AM, James Edward Gray II wrote:

On Feb 10, 2005, at 10:40 PM, Jeff Davis wrote:

Is this really the simplest, ruby-like way to build a non-blocking network app? It seems like this is harder and messier than C. It's also hard to find the constants and functions even if they do exist.

I would say the simplest way would be to download a library that handles this for you. :wink: I know there was one announced here not too terribly long ago, though a quick search didn't turn it up for me. Maybe the author will notice this thread and speak up...

I meant MultiPLEXing here, not multicasting. Sorry.

James Edward Gray II

···

On Feb 11, 2005, at 8:34 AM, James Edward Gray II wrote:

On Feb 11, 2005, at 3:45 AM, Robert Klemme wrote:

AFAIK Ruby uses nonblocking IO interenally. The easiest is typically to
use threads to cope with multiple concurrent IO.

I believe Ruby's internal Thread system use Multicasting, but not non-blocking IO. That's why we occasionally see posts pointing out that large write operations still block. Please correct me if I'm wrong though.