- up: ðããã°ã©ãã³ã°
- refs.
ãœã±ããããã°ã©ãã³ã°ãšã¯
ãœã±ãã(Socket)
å®è¡äžã®ããã°ã©ã éã§ããŒã¿ã®éåä¿¡ãè¡ãããã®æšæºçãªããã°ã©ãã³ã°ã€ã³ã¿ãŒãã§ãŒã¹ (API) ã®äžã€.
TCP/IP ã¢ããªã±ãŒã·ã§ã³ãäœæããããã®æœè±¡åãããã€ã³ã¿ãŒãã§ãŒã¹
ref. ãœã±ãã (BSD) - Wikipedia
é信端ç¹, éä¿¡ã®åºå ¥ãå£ã«ãªããããªãã¢ã®æŠå¿µ.
EndPoints
端æ«.äžæã«å®ãŸãéä¿¡å .
Socket 㯠IP Address ãš PortNo.ãã€ãã£ãŠ, éä¿¡å ã®ãœã±ãããç¹å®ãã. ãããã¯ãŒã¯ã«æ¥ç¶ãããããœã³ã³ã PDA, æºåž¯é»è©±ãªã©ã®ãããã¯ãŒã¯ç«¯æ«ã®ç·ç§°. ãšã³ããã€ã³ãã« Socket ããã€ã³ãããã.
Loopbacks
IP ã¢ãã¬ã¹ã¯å¿ ããããªã¢ãŒããã¹ããšé¢é£ä»ããããŠããå¿ èŠã¯ãªã. ãšãã«éçºç°å¢ã§ã¯, ããŒã«ã«ãª IP ã¢ãã¬ã¹ã圹ã«ç«ã€.
èªç«¯æ«ã«å¯ŸããŠéä¿¡ããããšã LoopBack ãšãã. LoopBack ã§æå®ããã¢ãã¬ã¹ã Loopback Address ãšãã.
- ããŒããŠã§ã¢ãšé¢ä¿ãªã, è«ççãªã€ã³ã¿ãã§ãŒã¹.
- Linux ã§ã¯, localhost ãšèšãããŠãã. IP 㯠127.0.0.1
ref. ã«ãŒãããã¯ãšã¯ ã ããŒã«ã«ã«ãŒããã㯠ã
Ports
ããŒããã©ã¯ãŒãã£ã³ã°
ããŒã«ã«ã³ã³ãã¥ãŒã¿ã®ç¹å®ã®ããŒãã«éãããŠããããŒã¿ãå¥ã«çšæããéä¿¡çµè·¯ãçšããŠãªã¢ãŒãã³ã³ãã¥ãŒã¿ã®ç¹å®ããŒãã«éä¿¡ããããš.
ref. ããŒããã©ã¯ãŒãã£ã³ã°ãšã¯ ã port forwarding ã
ããšãã°, Windows ãã¡ã€ã«ãµãŒãã«,SSH çµç±ã§ (CIFS, port 445/tcp ã§) ã¢ã¯ã»ã¹ãã (ãã).
Stream
ããŒã¿ã®å ¥åãŸãã¯åºåã®æ©èœãæäŸããæœè±¡ããŒã¿å. ã¡ãã»ãŒãžã®å¢çãšããå¢çããªã. TCP/IP ãããã³ã«ã¯ã¹ããªãŒã ããŒã¹ã®èšèš.
ref. ã¹ããªãŒã (ããã°ã©ãã³ã°) - Wikipedia
Blocking/Non-Blocking
ããããã³ã°
éåä¿¡ã®å®äºãåŸ ã£ãŠããä»ã®åŠçãéå§ããéä¿¡æ¹æ³.
require 'socket'
server = TCPServer.new (2803)
while client = server.accept
input = client.readline
client.write "You said: #{input}"
client.close
end
ãã³ã»ããããã³ã°
ããŒã¿ã®éåä¿¡ãè¡ãéã«, éåä¿¡ã®å®äºãåŸ ããä»ã®åŠçãéå§ããéä¿¡æ¹æ³.
ref. ãã³ããããã³ã°éä¿¡ãšã¯ ã ãã³ããããã³ã°ã¢ãŒã ã
require 'socket'
server = TCPServer.new (2803)
loop do
Thread.new (server.accept){ |client|
input = client.readline
client.write "You said: #{input}"
client.close
}
end
Example Ruby
Server Lifecycle
- create ãœã±ããã®çæ
- bind
- ãœã±ãããå©çšãã Port ã«ãã€ã³ã.
- User ã§ãã€ã³ããã Port 㯠1025-48999
- ãœã±ãããéä¿¡ãèš±å¯ãã IP ãæå®.
- 0.0.0.0: ãã¹ãŠèš±å¯
- 127.0.0.1: èªç«¯æ«ã®ã¿èš±å¯
- xxx.xxx.xxx.xxx: ãã端æ«ã®ã¿èš±å¯
- ãœã±ãããå©çšãã Port ã«ãã€ã³ã.
- listen éä¿¡å ãœã±ããããã®éä¿¡ããŸã€
- accept éä¿¡å ãœã±ããããã®éä¿¡ãåãåã, éä¿¡è·¯ ããã®ã€ã©çæ (connection)
- close éä¿¡å ãœã±ããã®æ¶æ» .
# coding: utf-8
require 'socket'
# 1. create
server = Socket.new (:INET, :STREAM)
# 2. bind
addr = Socket.pack_sockaddr_in (4481, "0.0.0.0")
server.bind (addr)
# 3. listen
server.listen (5)
# 4. accept
loop do
connection, _ = server.accept
## send message from client.
# ehco "Hello" | nc localhost 4481
p connection
# it should be closed each connection
connection.close
end
# 5. close
server.close
TCPServer
Ruby ã§ã¯, 以äžã®ãããªç³è¡£æ§æããã.
ref. class TCPServer
server = TCPServer.new (4481)
# =>
# server = Socket.new (:INET, :STREAM)
# addr = Socket.pack_sockaddr_in (4481, "0.0.0.0")
# server.bind (addr)
# server.listen (5)
Client LifeCycle
- create ãœã±ããã®çæ
(2. bind )
- connect ãµãŒããšæ¥ç¶
- close ãœã±ããã®ç Žæ£
require 'socket'
# 1. create
socket = Socket.new (:INET, :STREAM)
# 3. connect
remote_addr = Socket.pack_sockaddr_in (80, 'google.com')
socket.connect (remote_addr)
# 4. close
socket.close
TCPSocket
Ruby ã§ã¯, 以äžã®ãããªç³è¡£æ§æããã.
ref. class TCPSocket
socket = TCPSocket.new ('google.com', 80)
# =>
# socket = Socket.new (:INET, :STREAM)
# remote_addr = Socket.pack_sockaddr_in (80, 'google.com')
# socket.connect (remote_addr)
Simple Read
以äžã®äŸã§ã¯, æ°žé ã«ãµãŒã㯠Read ãåŸ ã¡ç¶ããŠ, ãã®å ãé²ãŸãªã.
require 'socket'
Socket.tcp_server_loop (4481) do |connection|
puts connection.read
connection.close
end
Socket ã¯æå®ãããããŒã¿é·ã®ããŒã¿ãå°çãããŸã§åŸ ã¡ç¶ãã. ãããããã¯ãåé¿ããããã«ã¯,
- Client åŽã§ ããŒã¿ã®æåŸã§ EOF ãéä¿¡ãã.(EOF event)
- Server åŽã§äžåºŠã«èªã¿èŸŒãããŒã¿é·ãå°ãããã. (partial read)
Client ã®ãœã±ããã close ã¡ãœãããå®è¡ãããš, ãã®å»¶é·ã§ EOF ãéç¥ããã.
ðéä¿¡ã¿ã€ã ã¢ãŠã
Socket Timeout/Connection Timeout/Connection Request Timeout
Javaã®Apache HttpClentã®äœ¿çšã ãããããªèšèªå®è£ ã§ç»å Žããã®ã§ãŸãšããŠãã. èšå®å¯èœãªïŒã€ã®å±æ§ã®æå³ã¯ãããã以äžã®éã.
- socket timeout: ãœã±ããéä¿¡ã®ã¹ã¿ãŒãããçµäºãŸã§.
- connection timeout: ã¯ã©ã€ã¢ã³ããããµãŒããžã®æ¥ç¶ç¢ºç«(3way-shake)ãŸã§
- connection-request-timeout: æ¥ç¶ç¢ºç«åŸããã¬ã¹ãã³ã¹ãè¿ããŸã§.
ref. Javaã®HttpClientã«ããã3ã€ã®Timeoutã®éãã«é¢ã㊠- Qiita
ãšã©ãŒãçºçãããšãã®é害解æã§å€§äºãªã®ã¯, èªåãæªãã®ããµãŒããæªãã®ããšããåé¡.
Connection Timeoutã¯ãµãŒããžã®éä¿¡ãå±ããŠãªãã®ã§ãµãŒãã¯æªããªã. Socket Timeoutã¯ãµãŒãããã®éä¿¡ãåããã®ã§ãµãŒããæªããã.
connection timeoutã¯3ã®åæ°
TCPã®ä»æ§ã§3ç§ããšã«ç¶æ³ç¢ºèªãããããã§, 3ã®åæ°ã§èšå®ããã®ããããšã®ããš.
ãPythonãrequestsã䜿ããšãã¯å¿ ãtimeoutãèšå®ããã¹ã | Cosnomi Blog
Broken pipe
ããsshæ¥ç¶ãšãã§èŠããããã€. ããã°ã©ã ãåãããŠããŠéä¿¡ãšã©ãŒã§ãã§ã.
packet_write_wait: Connection to port 22: Broken pipe
Broken pipeãšåºåãããåå ã¯ã¯ã©ã€ã¢ã³ããããµãŒã(sshæ¥ç¶å )ã«å¯ŸããŠäžå®æéç¡æäœããã¿ã€ã ã¢ãŠãã«ããåæããããšæãããŸã.
ããã¯ã¯ã©ã€ã¢ã³ãåŽã被çç®æã®å¯èœæ§. ãµãŒãããã®å¿çå®äºãåŸ ããã«æ¥ç¶çµäº. ãããå¿çãåŸ ã£ãŠããã®ãæåŸ æéå€ããã°ãµãŒãã被çç®æã®å¯èœæ§ããã. ãããã«ãããªãã©ã€ã«ãã£ãŠææžãè©Šã¿ãããšããã.