Module: CNNTActionHelper
- Includes:
- Socket::Constants
- Defined in:
- lib/action/module/cnnt_action.rb
Instance Method Summary collapse
-
#cnnt_close(socket) ⇒ Object
关闭连接 用于长连接操作.
-
#cnnt_connect(ip, port) ⇒ Object
建立连接 用于长连接操作.
-
#cnnt_create_practical_server(port, cont) ⇒ Object
创建一个server 接收到的数据被放到cont数组中 该函数仅支持一个线程的连接请求.
-
#cnnt_create_server(port) ⇒ Object
}.
-
#cnnt_direct_send(ip, port, cont) ⇒ Object
发送一次数据 用于短连接操作.
-
#cnnt_send(socket, cont) ⇒ Object
发送数据 用于长连接操作.
-
#cnnt_stop_server(svr) ⇒ Object
停止服务线程.
Instance Method Details
#cnnt_close(socket) ⇒ Object
关闭连接 用于长连接操作
参数
socket 用于操作的socket句柄
Example
cnnt_close socket
51 52 53 |
# File 'lib/action/module/cnnt_action.rb', line 51 def cnnt_close socket socket.close end |
#cnnt_connect(ip, port) ⇒ Object
建立连接 用于长连接操作
参数
-
ip
-
port
返回
socekt 句柄
Example:
socket = cnnt_connect 127.0.0.1, 1900
22 23 24 25 26 27 |
# File 'lib/action/module/cnnt_action.rb', line 22 def cnnt_connect ip, port socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( port, ip ) socket.connect( sockaddr ) return socket end |
#cnnt_create_practical_server(port, cont) ⇒ Object
创建一个server 接收到的数据被放到cont数组中
该函数仅支持一个线程的连接请求
参数
port 端口cont 接收数据用的数组
返回
服务器线程句柄
Example
cont=[] cnnt_create_practical_server 1999, cont cnnt_direct_send “127.0.0.1”, 1999, “dddd” p cont
127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/action/module/cnnt_action.rb', line 127 def cnnt_create_practical_server port, cont svr_t = Thread.new { socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( port, 'localhost' ) socket.bind( sockaddr ) socket.listen( 5 ) client, client_sockaddr = socket.accept loop do cont << client.recvfrom(1024)[0] end socket.close } return svr_t end |
#cnnt_create_server(port) ⇒ Object
}
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/action/module/cnnt_action.rb', line 93 def cnnt_create_server port svr_t = Thread.new { socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( port, 'localhost' ) socket.bind( sockaddr ) socket.listen( 5 ) loop do client, client_sockaddr = socket.accept Thread.new{ loop do yield client end } end socket.close } return svr_t end |
#cnnt_direct_send(ip, port, cont) ⇒ Object
发送一次数据 用于短连接操作
参数
ip ip port 端口cont 要发送的内容
返回
发送的数据长度
Example
cnnt_short_send “127.0.0.1”, 1900, “dddd”
67 68 69 70 71 72 73 74 |
# File 'lib/action/module/cnnt_action.rb', line 67 def cnnt_direct_send ip, port, cont socket = Socket.new( AF_INET, SOCK_STREAM, 0 ) sockaddr = Socket.pack_sockaddr_in( port, ip ) socket.connect( sockaddr ) ret = socket.send cont, 0 socket.close return ret end |
#cnnt_send(socket, cont) ⇒ Object
发送数据 用于长连接操作
参数
socket 用于发送的socket句柄cont 发送的内容
返回
发送的数据长度
Example
cnnt_send socket, “dddd”
40 41 42 |
# File 'lib/action/module/cnnt_action.rb', line 40 def cnnt_send socket, cont socket.send cont, 0 end |
#cnnt_stop_server(svr) ⇒ Object
停止服务线程
参数
svr 服务器线程句柄
Example
cnnt_stop_server svr
149 150 151 |
# File 'lib/action/module/cnnt_action.rb', line 149 def cnnt_stop_server svr svr.exit end |