本文共 3538 字,大约阅读时间需要 11 分钟。
ruby元编程
The main purpose of the socket is to allow the client and server to communicate with each other while socket programming is a mechanism to create a communication between two nodes (sockets).
套接字的主要目的是允许客户端和服务器相互通信,而套接字编程是一种在两个节点(套接字)之间创建通信的机制。
Two levels are allowed in Ruby to access network services. Basic Socket support is permitted in level one which allows you to make use of client and server under both connectionless and connection-oriented protocols (support by your Operating System) whereas level two provides you access to the predefined libraries which helps you to access application-level network protocols namely HTTP, FTP and so on.
Ruby中允许两个级别来访问网络服务。 一级允许使用基本套接字支持,该级别允许您在无连接和面向连接的协议(操作系统支持)下使用客户端和服务器,而第二级则提供对预定义库的访问,这有助于您访问应用程序-级别的网络协议,即HTTP,FTP等。
Let us understand how socket programming is done in Ruby with the help of an example given below:
让我们借助下面给出的示例来了解如何在Ruby中完成套接字编程 :
require 'socket' server = TCPServer.open(2000) loop { client = server.accept client.puts "Hello. Greetings from Includehelp.com" client.puts "Hope, you all are enjoying the day" client.puts "This is an example of socket programming." client.close }
The above program is a simple client program which will open a connection with the provided port address. We are utilizing a loop under which we are taking an instance client. The client is accepting all the connections made to port 2000 and sending data to the client using the Socket network. The data sent here are strings. At last, we are closing the socket.
上面的程序是一个简单的客户端程序,它将使用提供的端口地址打开一个连接。 我们正在利用一个循环,在该循环下我们将使用一个实例客户端。 客户端接受到端口2000的所有连接,并使用套接字网络将数据发送到客户端。 此处发送的数据是字符串。 最后,我们要关闭套接字。
We saw how to send data over a socket? Now, let us see how to fetch data from the server over the socket. Refer the code given below:
我们看到了如何通过套接字发送数据? 现在,让我们看看如何通过套接字从服务器获取数据。 请参考下面给出的代码:
require 'socket' hostname = 'localhost' port = 2000 skt = TCPSocket.open(hostname, port) while ln = skt.gets puts ln.chomp end skt.close
In the above code, we are including a pre-installed module named Socket. We are then assigning values to localhost and port. After that, we are using 'skt' to open TCPSocket along with hostname and port. Use while loop to fetch all the data sent over the network. In the end, close the socket.
在上面的代码中,我们包含一个名为Socket的预安装模块。 然后,我们将值分配给localhost和port 。 之后,我们使用'skt'打开TCPSocket以及主机名和端口 。 使用while循环可获取通过网络发送的所有数据。 最后,关闭插座。
The codes we read above allow one client and one server at a time. But we may need a code which allows multiple clients to get connected with a single server at a time. In such a case, the following code may provide some help:
我们上面阅读的代码一次允许一个客户端和一台服务器。 但是我们可能需要一个代码,该代码允许多个客户端一次与一台服务器建立连接。 在这种情况下,以下代码可能会提供一些帮助:
require 'socket' server = TCPServer.open(2009) loop { Thread.start(server.accept) do |client| client.puts "Hey Client! you are connected with the server." client.puts "Sending...........................!" client.puts "Its time to close the connection. See you super soon!" client.close end}
Thread class defined in Ruby library helps in creating a multithreaded network. Thread class a new execution thread to process the connection instantly while permitting the main class to anticipate more connections. In the above program, we are making use of Thread class to connect with multiple clients.
Ruby库中定义的线程类有助于创建多线程网络。 线程类是一个新的执行线程,可以立即处理连接,同时允许主类预期更多连接。 在上面的程序中,我们利用Thread类连接多个客户端。
翻译自:
ruby元编程
转载地址:http://jfvzd.baihongyu.com/