Class: UDPSocket
- Inherits:
-
IPSocket
- Object
- IO
- BasicSocket
- IPSocket
- UDPSocket
- Defined in:
- mrbgems/mruby-socket/mrblib/socket.rb
Instance Attribute Summary
Attributes inherited from BasicSocket
Instance Method Summary collapse
-
#_sockaddr_in(port, host) ⇒ Object
call-seq: udpsocket._sockaddr_in(port, host) -> string.
-
#bind(host, port) ⇒ Object
call-seq: ipsocket.bind(host, port) -> 0.
-
#connect(host, port) ⇒ Object
call-seq: ipsocket.connect(host, port) -> 0.
-
#initialize(af = Socket::AF_INET) ⇒ UDPSocket
constructor
call-seq: UDPSocket.new(af=Socket::AF_INET) -> udpsocket.
-
#recvfrom_nonblock(*args) ⇒ Object
call-seq: udpsocket.recvfrom_nonblock(maxlen, flags=0) -> [data, addrinfo].
-
#send(mesg, flags, host = nil, port = nil) ⇒ Object
call-seq: ipsocket.send(mesg, flags, host=nil, port=nil) -> integer.
Methods inherited from IPSocket
#addr, getaddress, #peeraddr, #recvfrom
Methods inherited from BasicSocket
do_not_reverse_lookup, do_not_reverse_lookup=, for_fd, #local_address, #recv_nonblock, #remote_address
Methods inherited from IO
#each, #each_byte, #each_char, #hash, open, pipe, popen, #pos=, #printf, read, #rewind
Constructor Details
#initialize(af = Socket::AF_INET) ⇒ UDPSocket
call-seq:
UDPSocket.new(af=Socket::AF_INET) -> udpsocket
Creates a new UDP socket for the given address family.
sock = UDPSocket.new
sock = UDPSocket.new(Socket::AF_INET6)
660 661 662 663 664 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 660 def initialize(af=Socket::AF_INET) super(Socket._socket(af, Socket::SOCK_DGRAM, 0), "r+") @af = af self end |
Instance Method Details
#_sockaddr_in(port, host) ⇒ Object
call-seq:
udpsocket._sockaddr_in(port, host) -> string
Internal method to create a sockaddr_in structure for the given port and host. Uses the socket's address family.
738 739 740 741 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 738 def _sockaddr_in(port, host) ai = Addrinfo.getaddrinfo(host, port, @af, Socket::SOCK_DGRAM)[0] ai.to_sockaddr end |
#bind(host, port) ⇒ Object
call-seq:
ipsocket.bind(host, port) -> 0
Binds the socket to the given host and port.
sock.bind("127.0.0.1", 8080)
sock.bind("0.0.0.0", 3000)
675 676 677 678 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 675 def bind(host, port) Socket._bind(self.fileno, _sockaddr_in(port, host)) 0 end |
#connect(host, port) ⇒ Object
call-seq:
ipsocket.connect(host, port) -> 0
Connects the socket to the given host and port.
sock.connect("127.0.0.1", 80)
sock.connect("www.example.com", 443)
689 690 691 692 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 689 def connect(host, port) Socket._connect(self.fileno, _sockaddr_in(port, host)) 0 end |
#recvfrom_nonblock(*args) ⇒ Object
call-seq:
udpsocket.recvfrom_nonblock(maxlen, flags=0) -> [data, addrinfo]
Receives data and sender information without blocking. May raise an exception if no data is available.
data, addr = sock.recvfrom_nonblock(1024)
703 704 705 706 707 708 709 710 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 703 def recvfrom_nonblock(*args) begin self._setnonblock(true) self.recvfrom(*args) ensure self._setnonblock(false) end end |
#send(mesg, flags, host = nil, port = nil) ⇒ Object
call-seq:
ipsocket.send(mesg, flags, host=nil, port=nil) -> integer
Sends data through the socket. Returns the number of bytes sent.
sock.send("Hello", 0)
sock.send("Data", 0, "127.0.0.1", 8080)
721 722 723 724 725 726 727 728 729 |
# File 'mrbgems/mruby-socket/mrblib/socket.rb', line 721 def send(mesg, flags, host=nil, port=nil) if port super(mesg, flags, _sockaddr_in(port, host)) elsif host super(mesg, flags, host) else super(mesg, flags) end end |