network program

J

Jeff Davis

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
 
B

Bill Kelly

Hi,

From: "Jeff Davis said:
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
 
R

Robert Klemme

Jeff Davis said:
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
 
J

James Edward Gray II

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.

James Edward Gray II
 
J

James Edward Gray II

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. ;) 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
 
J

James Edward Gray II

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.

I meant MultiPLEXing here, not multicasting. Sorry.

James Edward Gray II
 
J

James Edward Gray II

I would say the simplest way would be to download a library that
handles this for you. ;) 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 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top