Ruby Socket
Lightweight Introduction
Ruby Socket Class Hierarchy
To know the socket hierarchy in ruby here a simple tree explains it.
I'll verbosely mention some of Socket::Constants
here since I didn't find an obvious reference listing it except Programming Ruby1.9 The Pragmatic Programmers' Guide; Otherwise you've to ri Socket::Constants
from command line which is a good way to get the description of each constant.
Socket Types
SOCK_RAW
SOCK_PACKET
SOCK_STREAM
SOCK_DRAM
SOCK_RDM
SOCK_SEQPACKET
Address Families(Socket Domains)
AF_APPLETALK
AF_ATM
AF_AX25
AF_CCITT
AF_CHAOS
AF_CNT
AF_COIP
AF_DATAKIT
AF_DEC
AF_DLI
AF_E164
AF_ECMA
AF_HYLINK
AF_IMPLINK
AF_INET(IPv4)
AF_INET6(IPv6)
AF_IPX
AF_ISDN
AF_ISO
AF_LAT
AF_LINK
AF_LOCAL(UNIX)
AF_MAX
AF_NATM
AF_NDRV
AF_NETBIOS
AF_NETGRAPH
AF_NS
AF_OSI
AF_PACKET
AF_PPP
AF_PUP
AF_ROUTE
AF_SIP
AF_SNA
AF_SYSTEM
AF_UNIX
AF_UNSPEC
Socket Protocol
IPPROTO_SCTP
IPPROTO_TCP
IPPROTO_UDP
Protocol Families
PF_APPLETALK
PF_ATM
PF_AX25
PF_CCITT
PF_CHAOS
PF_CNT
PF_COIP
PF_DATAKIT
PF_DEC
PF_DLI
PF_ECMA
PF_HYLINK
PF_IMPLINK
PF_INET
PF_INET6
PF_IPX
PF_ISDN
PF_ISO
PF_KEY
PF_LAT
PF_LINK
PF_LOCAL
PF_MAX
PF_NATM
PF_NDRV
PF_NETBIOS
PF_NETGRAPH
PF_NS
PF_OSI
PF_PACKET
PF_PIP
PF_PPP
PF_PUP
PF_ROUTE
PF_RTIP
PF_SIP
PF_SNA
PF_SYSTEM
PF_UNIX
PF_UNSPEC
PF_XTP
Socket options
SO_ACCEPTCONN
SO_ACCEPTFILTER
SO_ALLZONES
SO_ATTACH_FILTER
SO_BINDTODEVICE
SO_BINTIME
SO_BROADCAST
SO_DEBUG
SO_DETACH_FILTER
SO_DONTROUTE
SO_DONTTRUNC
SO_ERROR
SO_KEEPALIVE
SO_LINGER
SO_MAC_EXEMPT
SO_NKE
SO_NOSIGPIPE
SO_NO_CHECK
SO_NREAD
SO_OOBINLINE
SO_PASSCRED
SO_PEERCRED
SO_PEERNAME
SO_PRIORITY
SO_RCVBUF
SO_RCVLOWAT
SO_RCVTIMEO
SO_RECVUCRED
SO_REUSEADDR
SO_REUSEPORT
SO_SECURITY_AUTHENTICATION
SO_SECURITY_ENCRYPTION_NETWORK
SO_SECURITY_ENCRYPTION_TRANSPORT
SO_SNDBUF
SO_SNDLOWAT
SO_SNDTIMEO
SO_TIMESTAMP
SO_TIMESTAMPNS
SO_TYPE
SO_USELOOPBACK
SO_WANTMORE
SO_WANTOOBFLAG
Creating Socket Template
domain(Address\/Protocol Families): like AF_INET, PF_PACKET, etc
socktype: like SOCK_RAW, SOCK_STREAM
protocol: by default, it's 0
m it should be a protocol defined (we'll manipulate that later)
TCP Socket
Server\/Client life cycle
General Socket usage
Get List of local IPaddreses
Get Hostname
TCP Server
Here we'll represent an absolute TCP server. This server will access connect from one client and send a message to it once connected then close the client and server connection
Note: if you want to list on unused and random port, set to port 0, ruby will find vacancy port then use it. ex.
TCP Client
You can put timeout/time interval for current connection in-case the server's response get delayed and the socket is still open.
There are some alternatives for puts
and gets
methods.You can see the difference and its classes using method method in Pry interpreter console
UDP Socket
UDP Server
UDP Client
There alternative for sending and receiving too, figure it out, RubyDoc.
GServer
GServer standard library implements a generic server, featuring thread pool management, simple logging, and multi-server management. Any kind of application-level server can be implemented using this class:
It accepts multiple simultaneous connections from clients
Several services (i.e. one service per TCP port)
can be run simultaneously,
can be stopped at any time through the class method
GServer.stop(port)
All the threading issues are handled
All events are optionally logged
Very basic GServer
Port knocking
Simple port knocker using the socket standard library.
Ref.:
Last updated