Last active
August 29, 2015 14:06
-
-
Save thash/7c131d18bc4270a6d33d to your computer and use it in GitHub Desktop.
AllAbout 020 Network
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File.ancestors | |
=> [File, IO, File::Constants, Enumerable, Object, Kernel, BasicObject] | |
TCPSocket.ancestors | |
=> [TCPSocket, IPSocket, BasicSocket, IO, File::Constants, Enumerable, Object, Kernel, BasicObject] | |
TCPServer.ancestors | |
=> [TCPServer, TCPSocket, IPSocket, BasicSocket, IO, File::Constants, Enumerable, Object, Kernel, BasicObject] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
socket = TCPSocket.open(*ARGV) | |
while line = STDIN.gets | |
socket.puts line | |
socket.flush | |
puts socket.gets | |
end | |
socket.close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby server.rb | |
# 別コンソールで | |
$ ruby client.rb localhost 2000 | |
waiwai | |
waiwai :D |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
require 'uri' | |
# $ ruby http.rb http://allabout.co.jp/gm/gc/446050/ | |
uri = URI(ARGV[0]) | |
# puts uri.host => "allabout.co.jp" | |
# puts uri.port => 80 | |
# puts uri.path => "/gm/gc/446050/" | |
socket = TCPSocket.new(uri.host, uri.port) | |
socket.puts("GET #{uri.path} HTTP/1.0\r\n") | |
socket.puts("Host: #{uri.host}\r\n") | |
socket.puts("\r\n") | |
# socket.read はレスポンス全体でheaderとbodyを含むため, headerだけ切り落としbodyを出力 | |
puts socket.read.split("\r\n\r\n").drop(1) | |
socket.close |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby http.rb http://allabout.co.jp/gm/gc/446050/ | |
<!DOCTYPE HTML> | |
<html lang="ja" id="ptm-form" class="r_itdigital"> | |
<head> | |
<link rel="alternate" media="handheld" href="http://m.allabout.co.jp/gm/gc/446050/"> | |
<link rel="alternate" media="only screen and (max-width: 640px)" href="http://touch.allabout.co.jp/gm/gc/446050/"> | |
<link rel="canonical" href="http://allabout.co.jp/gm/gc/446050/"> | |
<link rel="next" href="http://allabout.co.jp/gm/gc/446050/2/"> | |
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'socket' | |
server = TCPServer.new 2000 | |
loop do | |
# クライアントの接続を待つ | |
socket = server.accept # #<TCPSocket:fd 9> など | |
while data = socket.gets | |
socket.puts data.chomp + ' :D' | |
end | |
socket.close | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby server.rb | |
# 別のコンソールからtelnetで接続する | |
$ telnet localhost 2000 | |
Trying ::1... | |
telnet: connect to address ::1: Connection refused | |
Trying 127.0.0.1... | |
Connected to localhost. | |
Escape character is '^]'. | |
hoge | |
hoge :D |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment