W3cubDocs

/Crystal 0.31

module Socket::Server

Direct including types

Defined in:

socket/server.cr

Instance Method Summary

  • #accept : IO

    Accepts an incoming connection and returns the client socket.

  • #accept(&block)

    Accepts an incoming connection and yields the client socket to the block.

  • #accept? : IO?

    Accepts an incoming connection and returns the client socket.

  • #accept?(&block)

    Accepts an incoming connection and yields the client socket to the block.

Instance Method Detail

abstract def accept : IOSource

Accepts an incoming connection and returns the client socket.

require "socket"

server = TCPServer.new(2202)
while true
  socket = server.accept
  socket.puts Time.utc
  socket.close
end

If the server is closed after invoking this method, an IO::Error (closed stream) exception must be raised.

def accept(&block)Source

Accepts an incoming connection and yields the client socket to the block. Eventually closes the connection when the block returns.

Returns the value of the block. If the server is closed after invoking this method, an IO::Error (closed stream) exception will be raised.

require "socket"

server = TCPServer.new(2202)
server.accept do |socket|
  socket.puts Time.utc
end

abstract def accept? : IO?Source

Accepts an incoming connection and returns the client socket.

Returns nil if the server is closed after invoking this method.

require "socket"

server = TCPServer.new(2202)
while socket = server.accept?
  socket.puts Time.utc
  socket.close
end

def accept?(&block)Source

Accepts an incoming connection and yields the client socket to the block. Eventually closes the connection when the block returns.

Returns the value of the block or nil if the server is closed after invoking this method.

require "socket"

server = UNIXServer.new("/tmp/service.sock")
server.accept? do |socket|
  socket.puts Time.utc
end

© 2012–2019 Manas Technology Solutions.
Licensed under the Apache License, Version 2.0.
https://crystal-lang.org/api/0.31.1/Socket/Server.html