Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hit a real web server with a TCP client
- Loading branch information
Showing
12 changed files
with
166 additions
and
108 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea/ | ||
/client_recv.txt | ||
/client_send.txt | ||
/server_recv.txt | ||
/server_send.txt | ||
/.env | ||
/vendor/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
AllCops: | ||
NewCops: enable | ||
|
||
Style/Documentation: | ||
Enabled: false | ||
|
||
Lint/AssignmentInCondition: | ||
Enabled: false |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3.2.2 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# frozen_string_literal: true | ||
|
||
source 'https://rubygems.org' | ||
|
||
gem 'dotenv' | ||
gem 'rubocop' |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
ast (2.4.2) | ||
dotenv (2.8.1) | ||
json (2.6.3) | ||
parallel (1.23.0) | ||
parser (3.2.2.3) | ||
ast (~> 2.4.1) | ||
racc | ||
racc (1.7.1) | ||
rainbow (3.1.1) | ||
regexp_parser (2.8.1) | ||
rexml (3.2.5) | ||
rubocop (1.52.1) | ||
json (~> 2.3) | ||
parallel (~> 1.10) | ||
parser (>= 3.2.2.3) | ||
rainbow (>= 2.2.2, < 4.0) | ||
regexp_parser (>= 1.8, < 3.0) | ||
rexml (>= 3.2.5, < 4.0) | ||
rubocop-ast (>= 1.28.0, < 2.0) | ||
ruby-progressbar (~> 1.7) | ||
unicode-display_width (>= 2.4.0, < 3.0) | ||
rubocop-ast (1.29.0) | ||
parser (>= 3.2.1.0) | ||
ruby-progressbar (1.13.0) | ||
unicode-display_width (2.4.2) | ||
|
||
PLATFORMS | ||
x86_64-linux | ||
|
||
DEPENDENCIES | ||
dotenv | ||
rubocop | ||
|
||
BUNDLED WITH | ||
2.4.13 |
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'socket' | ||
require 'dotenv' | ||
Dotenv.load | ||
|
||
# tcpクライエントクラス | ||
class TcpClient | ||
# 初期化 | ||
def initialize | ||
@client = TCPSocket.new(ENV.fetch('WEBSITE', nil), 80) | ||
end | ||
|
||
def run | ||
send | ||
recv | ||
close | ||
end | ||
|
||
private | ||
|
||
def send | ||
request = "GET / HTTP/1.1\r\n" | ||
request += ENV.fetch('HOST', nil) | ||
request += "Connection: close\r\n\r\n" | ||
|
||
@client.puts(request) | ||
end | ||
|
||
def recv | ||
# サーバーからのデータを受信する | ||
File.open('client_recv.txt', 'w') do |f| | ||
while (line = @client.gets) | ||
f.puts(line) | ||
end | ||
end | ||
# @client.puts('0') | ||
end | ||
|
||
def close | ||
@client.close | ||
end | ||
end | ||
|
||
# クライアントの起動 | ||
client = TcpClient.new | ||
client.run |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,59 @@ | ||
require 'socket' | ||
|
||
# tcpサーバークラス | ||
class TcpServer | ||
# 初期化 | ||
def initialize | ||
@server = TCPServer.open(8001) | ||
@client = nil | ||
end | ||
|
||
def run | ||
accept | ||
recv | ||
send("server_send.txt") | ||
close | ||
end | ||
|
||
private | ||
|
||
def accept | ||
puts "waiting for connection..." | ||
# クライアントからの接続を待つ | ||
@client = @server.accept | ||
end | ||
|
||
# クライアントからのデータを受信する | ||
def recv | ||
puts "connected" | ||
# 受け取った内容をsever_recv.txtに書き込む | ||
File.open("server_recv.txt", "w") do |f| | ||
while line = @client.gets | ||
break if line.chomp == "0" | ||
f.write(line) | ||
end | ||
end | ||
end | ||
|
||
# クライアントにデータを送信する | ||
def send(_data) | ||
# server_send.txtの内容をクライアントにデータを送信する | ||
File.open("server_send.txt", "r") do |f| | ||
while line = f.gets | ||
@client.puts(line) | ||
end | ||
end | ||
|
||
end | ||
|
||
def close | ||
# クライアントから0を受け取ったら、クライアントとの接続を切断する | ||
@client.close | ||
end | ||
end | ||
|
||
# サーバーの起動 | ||
server = TcpServer.new | ||
|
||
server.run | ||
# frozen_string_literal: true | ||
|
||
require 'socket' | ||
|
||
# tcpサーバークラス | ||
class TcpServer | ||
# 初期化 | ||
def initialize | ||
@server = TCPServer.open(8001) | ||
@client = nil | ||
end | ||
|
||
def run | ||
accept | ||
recv | ||
send('server_send.txt') | ||
close | ||
end | ||
|
||
private | ||
|
||
def accept | ||
puts 'waiting for connection...' | ||
# クライアントからの接続を待つ | ||
@client = @server.accept | ||
end | ||
|
||
# クライアントからのデータを受信する | ||
def recv | ||
puts 'connected' | ||
# 受け取った内容をsever_recv.txtに書き込む | ||
File.open('server_recv.txt', 'w') do |f| | ||
while (line = @client.gets) | ||
break if line.chomp == '0' | ||
|
||
f.write(line) | ||
end | ||
end | ||
end | ||
|
||
# クライアントにデータを送信する | ||
def send(_data) | ||
# server_send.txtの内容をクライアントにデータを送信する | ||
File.open('server_send.txt', 'r') do |f| | ||
while (line = f.gets) | ||
@client.puts(line) | ||
end | ||
end | ||
end | ||
|
||
def close | ||
puts 'connection closed' | ||
# クライアントから0を受け取ったら、クライアントとの接続を切断する | ||
@client.close | ||
end | ||
end | ||
|
||
server = TcpServer.new | ||
server.run |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.