📜  Ruby套接字编程(1)

📅  最后修改于: 2023-12-03 15:19:52.731000             🧑  作者: Mango

Ruby Socket Programming

Socket programming refers to the process of sending and receiving data between two or more computers over a network using sockets. In Ruby, we can use the Socket library to create TCP/IP sockets and communicate over a network.

Creating a Socket

To create a new socket in Ruby, we can use the Socket.new method, which takes two arguments: the address family and the socket type.

require 'socket'

# Create a new TCP/IP socket
server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)

# Create a new UDP socket
client_socket = Socket.new(Socket::AF_INET, Socket::SOCK_DGRAM)

The first argument, Socket::AF_INET, specifies that we are using the IP protocol. The second argument specifies the type of socket we want to create: Socket::SOCK_STREAM for TCP or Socket::SOCK_DGRAM for UDP.

Binding a Socket

Before we can start accepting connections or sending data over a socket, we need to bind it to a specific address and port.

require 'socket'

# Create a new TCP/IP socket
server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)

# Bind the socket to port 3000 on the local machine
server_socket.bind(Socket.sockaddr_in(3000, "127.0.0.1"))

The Socket.sockaddr_in method is used to create a new socket address structure, which consists of an IP address and a port number. In this example, we are binding the socket to port 3000 on the local machine.

Listening for Connections

Once a socket has been bound to a port, we can start listening for incoming connections.

require 'socket'

# Create a new TCP/IP socket
server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)

# Bind the socket to port 3000 on the local machine
server_socket.bind(Socket.sockaddr_in(3000, "127.0.0.1"))

# Listen for incoming connections
server_socket.listen(5)

The listen method tells the socket to listen for incoming connections. The argument specifies the maximum number of pending connections to allow in the socket's queue.

Accepting Connections

When an incoming connection is detected, we can accept it and create a new socket to communicate with the client.

require 'socket'

# Create a new TCP/IP socket
server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)

# Bind the socket to port 3000 on the local machine
server_socket.bind(Socket.sockaddr_in(3000, "127.0.0.1"))

# Listen for incoming connections
server_socket.listen(5)

# Accept incoming connections
client_socket, client_addrinfo = server_socket.accept

The accept method blocks until an incoming connection is detected. It returns a new socket that can be used to communicate with the client, and an AddrInfo object that contains information about the client's address.

Sending Data

To send data over a socket, we can use the send method.

require 'socket'

# Create a new TCP/IP socket
client_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)

# Connect to a server
server_address = Socket.sockaddr_in(3000, "127.0.0.1")
client_socket.connect(server_address)

# Send data to the server
client_socket.send("Hello, world!", 0)

The first argument to send is the data to send, and the second argument is a combination of flags that control the behavior of the send operation.

Receiving Data

To receive data over a socket, we can use the recv method.

require 'socket'

# Create a new TCP/IP socket
server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)

# Bind the socket to port 3000 on the local machine
server_socket.bind(Socket.sockaddr_in(3000, "127.0.0.1"))

# Listen for incoming connections
server_socket.listen(5)

# Accept incoming connections
client_socket, client_addrinfo = server_socket.accept

# Receive data from the client
data = client_socket.recv(1024)

The argument to recv is the maximum number of bytes to receive. The method blocks until data is received or an error occurs.

Closing a Socket

To close a socket, we can use the close method.

require 'socket'

# Create a new TCP/IP socket
server_socket = Socket.new(Socket::AF_INET, Socket::SOCK_STREAM)

# Bind the socket to port 3000 on the local machine
server_socket.bind(Socket.sockaddr_in(3000, "127.0.0.1"))

# Listen for incoming connections
server_socket.listen(5)

# Accept incoming connections
client_socket, client_addrinfo = server_socket.accept

# Receive data from the client
data = client_socket.recv(1024)

# Close the socket
client_socket.close
server_socket.close

It's important to close both the client and server sockets after communication is complete.

Conclusion

Ruby's Socket library provides a powerful and flexible way to create TCP/IP and UDP sockets and communicate over a network. By understanding the basics of socket programming, you can build custom network applications using Ruby.