ユーザ用ツール

サイト用ツール


alworker:tcp_ipサーバー

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

次のリビジョン
前のリビジョン
次のリビジョン両方とも次のリビジョン
alworker:tcp_ipサーバー [2015/11/22 12:24] – 外部編集 127.0.0.1alworker:tcp_ipサーバー [2020/08/30 14:53] hirohito
行 5: 行 5:
  
 TCPサーバーを作成する機能です。SMTPサーバのような、chat式のサーバ機能を実装しています。 TCPサーバーを作成する機能です。SMTPサーバのような、chat式のサーバ機能を実装しています。
 +
 +{{:alworker:tcp_explain1.png?nolink|}}
  
 ====== サンプル ====== ====== サンプル ======
  
 +サーバー
 <file ruby tcp_server.rb> <file ruby tcp_server.rb>
 require "al_worker_tcp" require "al_worker_tcp"
行 15: 行 18:
   # イニシャライザでTCPを用意する。   # イニシャライザでTCPを用意する。
   def initialize2()   def initialize2()
-    @tcp = Tcp.new( "<any>", 25 ) # リスンアドレス、ポート番号を指定。+    @tcp = Tcp.new( "<any>", 10025 ) # リスンアドレス、ポート番号を指定。
     @tcp.run( self )     @tcp.run( self )
   end   end
行 38: 行 41:
 end end
  
-server = TcpServer.new( "tcp_server" )+server = TcpServer.new("tcp_server"
 +server.parse_option()
 server.daemon() server.daemon()
 </file> </file>
 +
 +
 +クライアント(Aloneライブラリは使用していまません)
 +
 +<file ruby tcp_client.rb>
 +require "socket"
 +
 +sock = TCPSocket.open("localhost", 10025)
 +
 +puts 'SEND "helo"'
 +sock.puts "helo"
 +puts 'RECEIVE ' + sock.gets
 +
 +puts 'SEND "quit"'
 +sock.puts "quit"
 +puts 'RECEIVE ' + sock.gets
 +
 +sock.close
 +</file>
 +
 +
 +===== 実行結果 =====
 +<code>
 +% ruby tcp_client.rb
 +SEND "helo"
 +RECEIVE 250 hello welcome.
 +SEND "quit"
 +RECEIVE 200 OK quit.
 +</code>
 +
  
  
行 111: 行 145:
   end   end
 end end
 +
 +
 +class TcpServer < AlWorker
 +
 +  def initialize2()
 +    @tcp = WebServer.new( "<any>", 10080 ) # リスンアドレス、ポート番号を指定。
 +    @tcp.run( self )
 +  end
 +
 +end
 +
 +server = TcpServer.new( "web_server" )
 +server.daemon()
 </code> </code>
  
alworker/tcp_ipサーバー.txt · 最終更新: 2023/08/19 12:06 by hirohito