alworker:tcp_ipサーバー
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン次のリビジョン両方とも次のリビジョン | ||
alworker:tcp_ipサーバー [2019/05/30 16:09] – [chat以外のプロトコルを使用] hirohito | alworker:tcp_ipサーバー [2020/08/29 11:15] – hirohito | ||
---|---|---|---|
行 5: | 行 5: | ||
TCPサーバーを作成する機能です。SMTPサーバのような、chat式のサーバ機能を実装しています。 | TCPサーバーを作成する機能です。SMTPサーバのような、chat式のサーバ機能を実装しています。 | ||
+ | |||
+ | {{: | ||
====== サンプル ====== | ====== サンプル ====== | ||
+ | サーバー | ||
<file ruby tcp_server.rb> | <file ruby tcp_server.rb> | ||
require " | require " | ||
行 15: | 行 18: | ||
# イニシャライザでTCPを用意する。 | # イニシャライザでTCPを用意する。 | ||
def initialize2() | def initialize2() | ||
- | @tcp = Tcp.new( "< | + | @tcp = Tcp.new( "< |
@tcp.run( self ) | @tcp.run( self ) | ||
end | end | ||
行 41: | 行 44: | ||
server.daemon() | server.daemon() | ||
</ | </ | ||
+ | |||
+ | |||
+ | クライアント(Aloneライブラリは使用していまません) | ||
+ | |||
+ | <file ruby tcp_client.rb> | ||
+ | require " | ||
+ | |||
+ | sock = TCPSocket.open(" | ||
+ | |||
+ | puts 'SEND " | ||
+ | sock.puts " | ||
+ | puts ' | ||
+ | |||
+ | puts 'SEND " | ||
+ | sock.puts " | ||
+ | puts ' | ||
+ | |||
+ | sock.close | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== 実行結果 ===== | ||
+ | < | ||
+ | % ruby tcp_client.rb | ||
+ | SEND " | ||
+ | RECEIVE 250 hello welcome. | ||
+ | SEND " | ||
+ | RECEIVE 200 OK quit. | ||
+ | </ | ||
+ | |||
alworker/tcp_ipサーバー.txt · 最終更新: 2023/08/19 12:06 by hirohito