7

I'm trying to create a socket in Ruby using

require "socket"
w = UNIXSocket.new("socket")

and I keep running into

No such file or directory - socket (Errno::ENOENT)

This looks completely backwards to me, because new() is supposed to create that missing file. What am I missing?

5
  • permission problem? Or in a directory that doesn't exist yet? Commented Sep 30, 2010 at 2:32
  • @Chuck Vose: Definitely neither of those...that's why it's so perplexing. Commented Sep 30, 2010 at 2:38
  • Oh, haha. UnixSocket.new is an alias for open. Commented Sep 30, 2010 at 2:42
  • Still trying to find an example of how to do it right. I'm seeing the same problem on my system Commented Sep 30, 2010 at 2:44
  • Dunno, maybe we're both cracked. I can't find examples that are more complicated than this. Commented Sep 30, 2010 at 2:49

1 Answer 1

7

This is super old. Please don't try to use it verbatim anymore.

http://blog.antarestrader.com/posts/153

#!/ruby
file = 'path/to/my/socket'
File.unlink if File.exists(file) && File.socket?(file)
server = UNIXServer.new(file)
# return a UNIXSocket once a connection is made 
socket = server.accept
# socket is now ready to communicate.

UnixServer makes the socket, UnixSocket only connects to an existing socket.

Sign up to request clarification or add additional context in comments.

4 Comments

Wow, thank you. It's crazy that this information isn't more easily accessible. Every example I found showed new as creating a new socket.
This code does not work on Ruby 2.0.0 for me. First: File.unlink takes a path. Second, I get the same no such file or directory error as the OP.
This should probably be "File.unlink(file)" in the code
What is the modern way to do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.