Question

Je suis en train d'utiliser les sockets unix et SOCK_DGRAM en rubis, mais ai un temps vraiment du mal à comprendre comment le faire. Jusqu'à présent, je l'ai essayé des choses comme ceci:

sock_path = 'test.socket'
s1 = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
s1.bind(Socket.pack_sockaddr_un(sock_path))

s2 = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
s2.bind(Socket.pack_sockaddr_un(sock_path))

s1.send("HELLO")
s2.recv(5) # should equal "HELLO"

Quelqu'un at-il une expérience avec cela?

Était-ce utile?

La solution

Dans le cas où vous avez besoin d'utiliser commune connect et bind pour les sockets client et serveur, vous devez donc deux adresses différentes pour la liaison

require 'socket'

sock_path = 'test.socket'
sock_path2 = 'test2.socket'

s1 = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
s1.bind(Socket.pack_sockaddr_un(sock_path))

s2 = Socket.new(Socket::AF_UNIX, Socket::SOCK_DGRAM, 0)
s2.bind(Socket.pack_sockaddr_un(sock_path2))
s2.connect(Socket.pack_sockaddr_un(sock_path))

s1.connect(Socket.pack_sockaddr_un(sock_path2))
s1.send("HELLO", 0)
puts s2.recv(5)

=> HELLO
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top