Help us understand the problem. What is going on with this article?

socketライブラリを使ってwebサーバーを作ってみる

More than 5 years have passed since last update.

カヤックさんの新卒研修を読んで telnetnc -l を使ってwebサーバ/ブラウザごっこをやってみたのですが、1人でやってると役割の切り替えがダルくなってきたので、本当の基礎からのwebアプリケーション入門を見ながらrubyで簡単なwebサーバを作ってみました。

参考
http://www.geekpage.jp/programming/ruby-network/
http://drascal.blog133.fc2.com/blog-entry-219.html

  • v1 TCPクライアントからの接続を受け付けられるようにする
tcp_server.rb
require "socket"

server = TCPServer.open(8001)

while true
  socket = server.accept

  while buffer = socket.gets
    p buffer
  end

  socket.close
end

server.close

  • v2 レスポンスを返すようにする、アクセス元の情報(peeraddr)を取得する
tcp_server.rb
require "socket"

port = 8001
server = TCPServer.open(port)

while true
  socket = server.accept

  # IPSockt#peeraddr
  # 接続相手先ソケットの情報を表す配列を返す
  # [アドレスファミリ, port番号, ホストを表す文字列, IPアドレス]
  # アドレスファミリについて
  # http://www.geekpage.jp/programming/winsock/addressfamily.php
  p socket.peeraddr

  while buffer = socket.gets
    p buffer

    # response
    socket.puts "200"
  end

  socket.close
end

server.close

  • v3 マルチスレッド化して複数のクライアントからのレスポンスを返せるようにする
tcp_server.rb
require "socket"

port = 8001
server = TCPServer.open(port)

while true
  Thread.start(server.accept) do |socket|
    p socket.peeraddr

    while buffer = socket.gets
      p buffer
      socket.puts "200"
    end

    socket.close
  end
end

server.close

  • v4 index.htmlを返す
tcp_server.rb
require "socket"

port = 8001
server = TCPServer.open(port)

def read_html
  File.open("index.html") do |f|
    f.read
  end
end

while true
  Thread.start(server.accept) do |socket|
    p socket.peeraddr

    request = socket.gets
    p request

    if request.include? "GET"
      content = read_html
      socket.write <<-EOF
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Server: rserver
Connection: close

#{content}
      EOF
    end

    socket.close
  end
end

server.close
index.html
<html>
  <head>
    <title>server</title>
  </head>
  <body>
    <h1>うごいてるよ!</h1>
  </body>
</html>

  • ブラウザから

v5.png

onigra
旧nekogeruge_987
http://onigra.github.io/
classi
学校の先生・生徒・保護者向けのB2B2Cの学習支援Webサービス「Classi(クラッシー)」 を開発・運営している会社です。
https://classi.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした