Reusing sockets

Hello,

Question on sockets: Can you reuse them?

Here is what I am trying:

#!/usr/local/bin/ruby

require 'socket’
require ‘fcntl’

s = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM, 0)
s.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK)

@port = 80
ip = “127.0.0.1"
packedip = ip.split(”.").collect { |x| x.to_i }.pack(“CCCC”)
sockaddr = [Socket::AF_INET, @port, packedip, ‘’].pack(‘snA4a8’)

while true
s.connect(sockaddr)
s.close
end

Here is what I get:
./reuse.rb
connected
./reuse.rb:15:in `connect’: closed stream (IOError)
from ./reuse.rb:15

Also, anyone know of a more efficient way to pack the IP?

thanks,
-joe

Question on sockets: Can you reuse them?

To answer my own question, my partner in crime tells me:

a quick perusal of connect(2) reveals:

“Generally, stream sockets may success-fully connect() only once;
datagram sockets may use connect() multiple times to change their
association. Datagram sockets may dissolve the association by
connecting to an invalid address, such as a null address.”

So the answer is no for tcp sockets and yes for udp.

thanks,
-joe

Hi,

while true
s.connect(sockaddr)
s.close
end

Here is what I get:
./reuse.rb
connected
./reuse.rb:15:in `connect’: closed stream (IOError)
from ./reuse.rb:15

Don’t close. You loose the underlying socket connection (the file
descriptor) when you close the socket.

Also, anyone know of a more efficient way to pack the IP?

If you are using 1.7 socket, Socket.sockaddr_in(port, host) is
available.

						matz.
···

In message “Reusing sockets” on 02/07/02, Joseph McDonald joe@vpop.net writes: