SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Socket网络编程
大纲
•   TCP/UDP
•   Socket
•   HTTP
•   Unix IO 模型
TCP-UDP 对比
UDP
• User Datagram Protocol
TCP
• Transmission Control Protocol
TCP——连接建立
• Before a client attempts to connect with a server, the server must first
  bind to and listen at a port to open it up for connections: this is called a
  passive open. Once the passive open is established, a client may initiate an
  active open.

    – SYN: The active open is performed by the client sending a SYN to the server.
      The client sets the segment's sequence number to a random value A.
    – SYN-ACK: In response, the server replies with a SYN-ACK. The
      acknowledgment number is set to one more than the received sequence
      number (A + 1), and the sequence number that the server chooses for the
      packet is another random number, B.
    – ACK: Finally, the client sends an ACK back to the server. The sequence number
      is set to the received acknowledgement value i.e. A + 1, and the
      acknowledgement number is set to one more than the received sequence
      number i.e. B + 1.
TCP——连接关闭
• The connection termination phase uses a four-way handshake, with
  each side of the connection terminating independently.
• When an endpoint wishes to stop its half of the connection, it
  transmits a FIN packet, which the other end acknowledges with an
  ACK.
• Therefore, a typical tear-down requires a pair of FIN and ACK
  segments from each TCP endpoint.
• After both FIN/ACK exchanges are concluded, the side which sent
  the first FIN before receiving one waits for a timeout before finally
  closing the connection, during which time the local port is
  unavailable for new connections; this prevents confusion due to
  delayed packets being delivered during subsequent connections.
TCP——连接关闭
TCP——整个过程
TCP——netstat
•   LISTEN
•   (server) represents waiting for a connection request from any remote TCP and port.
•   SYN-SENT
•   (client) represents waiting for a matching connection request after having sent a
    connection request.
•   SYN-RECEIVED
•   (server) represents waiting for a confirming connection request acknowledgment
    after having both received and sent a connection request.
•   ESTABLISHED
•   (both server and client) represents an open connection, data received can be
    delivered to the user. The normal state for the data transfer phase of the
    connection.
TCP——netstat
•   FIN-WAIT-1
•   (both server and client) represents waiting for a connection termination request from the remote TCP, or an
    acknowledgment of the connection termination request previously sent.
•   FIN-WAIT-2
•   (both server and client) represents waiting for a connection termination request from the remote TCP.
•   CLOSE-WAIT
•   (both server and client) represents waiting for a connection termination request from the local user.
•   CLOSING
•   (both server and client) represents waiting for a connection termination request acknowledgment from the
    remote TCP.
•   LAST-ACK
•   (both server and client) represents waiting for an acknowledgment of the connection termination request
    previously sent to the remote TCP (which includes an acknowledgment of its connection termination request).
•   TIME-WAIT
•   (either server or client) represents waiting for enough time to pass to be sure the remote TCP received the
    acknowledgment of its connection termination request.
•   CLOSED
•   (both server and client) represents no connection state at all.
Socket
• A network socket is an endpoint of an inter-process communication
  flow across a computer network. Today, most communication
  between computers is based on the Internet Protocol; therefore
  most network sockets are Internet sockets.
• A socket API is an application programming interface (API), usually
  provided by the operating system, that allows application programs
  to control and use network sockets. Internet socket APIs are usually
  based on the Berkeley sockets standard.
• A socket address is the combination of an IP address and a port
  number, much like one end of a telephone connection is the
  combination of a phone number and a particular extension. Based
  on this address, internet sockets deliver incoming data packets to
  the appropriate application process or thread.
Socket——基于UDP编程流程
Socket——基于TCP编程流程
Socket——选项
•   SO_DEBUG
•   SO_DONTROUTE
•   SO_KEEPALIVE
•   SO_DONTLINER
•   SO_LINGER
•   SO_RCVBUF
•   SO_SNDBUF
•   SO_REUSERADDR
•   SO_RCVTIMEO
•   SO_SNDTIMEO
•   TCP_NODELAY(Nagle)
HTTP
• Hypertext Transfer Protocol
• HTTP functions as a request-response protocol
  in the client-server computing model.
HTTP——Request
• Request methods
  – HEAD/GET/POST/PUT/DELETE/TRACE/OPTIONS/C
    ONNECT/PATCH
HTTP——GET POST
• GET 参数

• POST 参数
 – application/x-www-form-urlencoded
 – multipart/form-data(non-ASCII data, and binary
   data)
HTTP——POST multipart/form-data
HTTP——Request Header
HTTP——Request Header
HTTP——Request Header
HTTP——Response
• Status codes
  – 1xx Informational/2xx Success/3xx
    Redirection/4xx Client Error/5xx Server Error
HTTP——Response Header
HTTP——Response Header
HTTP——Response Header
HTTP——Response Header
HTTP——Persistent connections
• Persistent connections
  – In HTTP/0.9 and 1.0, the connection is closed after
    a single request/response pair. In HTTP/1.1 a
    keep-alive-mechanism was introduced, where a
    connection could be reused for more than one
    request.
Unix IO 模型
• Unix下共有五种I/O模型

  – 阻塞I/O

  – 非阻塞I/O

  – I/O复用(select和poll)

  – 信号驱动I/O(SIGIO)

  – 异步I/O(Posix.1的aio_系列函数)
Unix IO 模型——阻塞IO
Unix IO 模型——非阻塞IO
Unix IO 模型——IO复用
Unix IO 模型——信号驱动IO
Unix IO 模型——异步IO
Unix IO 模型——对比
Unix IO 模型——异步IO
END

Weitere ähnliche Inhalte

Was ist angesagt?

Surviving The Stump The Chump Interview Questions
Surviving The Stump The Chump Interview QuestionsSurviving The Stump The Chump Interview Questions
Surviving The Stump The Chump Interview QuestionsDuane Bodle
 
Client server examples for tcp abnormal conditions
Client server examples for tcp abnormal conditionsClient server examples for tcp abnormal conditions
Client server examples for tcp abnormal conditionsCEC Landran
 
Network interview questions
Network interview questionsNetwork interview questions
Network interview questionsrajasekar1712
 
Solar windsportrequirements
Solar windsportrequirementsSolar windsportrequirements
Solar windsportrequirementsnasac
 
IP/MAC Address Translation
IP/MAC Address TranslationIP/MAC Address Translation
IP/MAC Address TranslationIsmail Mukiibi
 
Part 4 : reliable transport and sharing resources
Part 4 : reliable transport and sharing resourcesPart 4 : reliable transport and sharing resources
Part 4 : reliable transport and sharing resourcesOlivier Bonaventure
 
Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)k33a
 
Traffic Characterization
Traffic CharacterizationTraffic Characterization
Traffic CharacterizationIsmail Mukiibi
 
Initial LTE call Setup Flow
Initial LTE call Setup FlowInitial LTE call Setup Flow
Initial LTE call Setup Flowassinha
 
TELNET and SSH by MUSTAFA SAKHAI
TELNET and SSH by MUSTAFA SAKHAITELNET and SSH by MUSTAFA SAKHAI
TELNET and SSH by MUSTAFA SAKHAIMUSTAFA SAKHAI
 

Was ist angesagt? (20)

Surviving The Stump The Chump Interview Questions
Surviving The Stump The Chump Interview QuestionsSurviving The Stump The Chump Interview Questions
Surviving The Stump The Chump Interview Questions
 
Port numbers
Port numbersPort numbers
Port numbers
 
Dhcp Snooping
Dhcp SnoopingDhcp Snooping
Dhcp Snooping
 
CCNA 1 Chapter 7 v5.0 2014
CCNA 1 Chapter 7 v5.0 2014CCNA 1 Chapter 7 v5.0 2014
CCNA 1 Chapter 7 v5.0 2014
 
Client server examples for tcp abnormal conditions
Client server examples for tcp abnormal conditionsClient server examples for tcp abnormal conditions
Client server examples for tcp abnormal conditions
 
6 app-tcp
6 app-tcp6 app-tcp
6 app-tcp
 
Part 7 : HTTP/2, UDP and TCP
Part 7 : HTTP/2, UDP and TCPPart 7 : HTTP/2, UDP and TCP
Part 7 : HTTP/2, UDP and TCP
 
Chap 18 telnet
Chap 18 telnetChap 18 telnet
Chap 18 telnet
 
Network interview questions
Network interview questionsNetwork interview questions
Network interview questions
 
Part 6 : Internet applications
Part 6 : Internet applicationsPart 6 : Internet applications
Part 6 : Internet applications
 
IMS Session Flow
IMS Session FlowIMS Session Flow
IMS Session Flow
 
Solar windsportrequirements
Solar windsportrequirementsSolar windsportrequirements
Solar windsportrequirements
 
Well known protocols port numbers
Well known  protocols port numbersWell known  protocols port numbers
Well known protocols port numbers
 
Part 12 : Local Area Networks
Part 12 : Local Area Networks Part 12 : Local Area Networks
Part 12 : Local Area Networks
 
IP/MAC Address Translation
IP/MAC Address TranslationIP/MAC Address Translation
IP/MAC Address Translation
 
Part 4 : reliable transport and sharing resources
Part 4 : reliable transport and sharing resourcesPart 4 : reliable transport and sharing resources
Part 4 : reliable transport and sharing resources
 
Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)Transmission Control Protocol (TCP)
Transmission Control Protocol (TCP)
 
Traffic Characterization
Traffic CharacterizationTraffic Characterization
Traffic Characterization
 
Initial LTE call Setup Flow
Initial LTE call Setup FlowInitial LTE call Setup Flow
Initial LTE call Setup Flow
 
TELNET and SSH by MUSTAFA SAKHAI
TELNET and SSH by MUSTAFA SAKHAITELNET and SSH by MUSTAFA SAKHAI
TELNET and SSH by MUSTAFA SAKHAI
 

Andere mochten auch

Course on TCP Dynamic Performance
Course on TCP Dynamic PerformanceCourse on TCP Dynamic Performance
Course on TCP Dynamic PerformanceJavier Arauz
 
TCP with delayed ack for wireless networks
TCP with delayed ack for wireless networksTCP with delayed ack for wireless networks
TCP with delayed ack for wireless networksambitlicksolutions
 
Lecture 5
Lecture 5Lecture 5
Lecture 5ntpc08
 
CS4344 09/10 Lecture 10: Transport Protocol for Networked Games
CS4344 09/10 Lecture 10: Transport Protocol for Networked GamesCS4344 09/10 Lecture 10: Transport Protocol for Networked Games
CS4344 09/10 Lecture 10: Transport Protocol for Networked GamesWei Tsang Ooi
 
Flow & Error Control
Flow & Error ControlFlow & Error Control
Flow & Error Controltameemyousaf
 

Andere mochten auch (8)

Course on TCP Dynamic Performance
Course on TCP Dynamic PerformanceCourse on TCP Dynamic Performance
Course on TCP Dynamic Performance
 
TCP Model
TCP ModelTCP Model
TCP Model
 
TCP with delayed ack for wireless networks
TCP with delayed ack for wireless networksTCP with delayed ack for wireless networks
TCP with delayed ack for wireless networks
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
CS4344 09/10 Lecture 10: Transport Protocol for Networked Games
CS4344 09/10 Lecture 10: Transport Protocol for Networked GamesCS4344 09/10 Lecture 10: Transport Protocol for Networked Games
CS4344 09/10 Lecture 10: Transport Protocol for Networked Games
 
Flow & Error Control
Flow & Error ControlFlow & Error Control
Flow & Error Control
 
4 transport-sharing
4 transport-sharing4 transport-sharing
4 transport-sharing
 
2 reliability-network
2 reliability-network2 reliability-network
2 reliability-network
 

Ähnlich wie Socket网络编程概述

Ähnlich wie Socket网络编程概述 (20)

TCP/IP
TCP/IPTCP/IP
TCP/IP
 
tcp-ippresentation-150614172243-lva1-app6892.pptx
tcp-ippresentation-150614172243-lva1-app6892.pptxtcp-ippresentation-150614172243-lva1-app6892.pptx
tcp-ippresentation-150614172243-lva1-app6892.pptx
 
CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9
CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9
CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9
 
TCP - IP Presentation
TCP - IP PresentationTCP - IP Presentation
TCP - IP Presentation
 
Tcp ip presentation
Tcp ip presentationTcp ip presentation
Tcp ip presentation
 
Java socket programming
Java socket programmingJava socket programming
Java socket programming
 
lecturer3.pptx
lecturer3.pptxlecturer3.pptx
lecturer3.pptx
 
Unit-4 (1).pptx
Unit-4 (1).pptxUnit-4 (1).pptx
Unit-4 (1).pptx
 
TCP/IP(networking)
TCP/IP(networking)TCP/IP(networking)
TCP/IP(networking)
 
Np unit iv ii
Np unit iv iiNp unit iv ii
Np unit iv ii
 
08 coms 525 tcpip - tcp 1
08   coms 525 tcpip - tcp 108   coms 525 tcpip - tcp 1
08 coms 525 tcpip - tcp 1
 
TCP /IP
TCP /IPTCP /IP
TCP /IP
 
Tcp
TcpTcp
Tcp
 
Lecture set 7
Lecture set 7Lecture set 7
Lecture set 7
 
Byte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptxByte Ordering - Unit 2.pptx
Byte Ordering - Unit 2.pptx
 
Java_Socket_Programming (2).ppt
Java_Socket_Programming (2).pptJava_Socket_Programming (2).ppt
Java_Socket_Programming (2).ppt
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Network performance overview
Network  performance overviewNetwork  performance overview
Network performance overview
 
Flow control
Flow controlFlow control
Flow control
 
Transport layer
Transport layerTransport layer
Transport layer
 

Kürzlich hochgeladen

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 

Kürzlich hochgeladen (20)

"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 

Socket网络编程概述

  • 2. 大纲 • TCP/UDP • Socket • HTTP • Unix IO 模型
  • 6. TCP——连接建立 • Before a client attempts to connect with a server, the server must first bind to and listen at a port to open it up for connections: this is called a passive open. Once the passive open is established, a client may initiate an active open. – SYN: The active open is performed by the client sending a SYN to the server. The client sets the segment's sequence number to a random value A. – SYN-ACK: In response, the server replies with a SYN-ACK. The acknowledgment number is set to one more than the received sequence number (A + 1), and the sequence number that the server chooses for the packet is another random number, B. – ACK: Finally, the client sends an ACK back to the server. The sequence number is set to the received acknowledgement value i.e. A + 1, and the acknowledgement number is set to one more than the received sequence number i.e. B + 1.
  • 7. TCP——连接关闭 • The connection termination phase uses a four-way handshake, with each side of the connection terminating independently. • When an endpoint wishes to stop its half of the connection, it transmits a FIN packet, which the other end acknowledges with an ACK. • Therefore, a typical tear-down requires a pair of FIN and ACK segments from each TCP endpoint. • After both FIN/ACK exchanges are concluded, the side which sent the first FIN before receiving one waits for a timeout before finally closing the connection, during which time the local port is unavailable for new connections; this prevents confusion due to delayed packets being delivered during subsequent connections.
  • 10. TCP——netstat • LISTEN • (server) represents waiting for a connection request from any remote TCP and port. • SYN-SENT • (client) represents waiting for a matching connection request after having sent a connection request. • SYN-RECEIVED • (server) represents waiting for a confirming connection request acknowledgment after having both received and sent a connection request. • ESTABLISHED • (both server and client) represents an open connection, data received can be delivered to the user. The normal state for the data transfer phase of the connection.
  • 11. TCP——netstat • FIN-WAIT-1 • (both server and client) represents waiting for a connection termination request from the remote TCP, or an acknowledgment of the connection termination request previously sent. • FIN-WAIT-2 • (both server and client) represents waiting for a connection termination request from the remote TCP. • CLOSE-WAIT • (both server and client) represents waiting for a connection termination request from the local user. • CLOSING • (both server and client) represents waiting for a connection termination request acknowledgment from the remote TCP. • LAST-ACK • (both server and client) represents waiting for an acknowledgment of the connection termination request previously sent to the remote TCP (which includes an acknowledgment of its connection termination request). • TIME-WAIT • (either server or client) represents waiting for enough time to pass to be sure the remote TCP received the acknowledgment of its connection termination request. • CLOSED • (both server and client) represents no connection state at all.
  • 12. Socket • A network socket is an endpoint of an inter-process communication flow across a computer network. Today, most communication between computers is based on the Internet Protocol; therefore most network sockets are Internet sockets. • A socket API is an application programming interface (API), usually provided by the operating system, that allows application programs to control and use network sockets. Internet socket APIs are usually based on the Berkeley sockets standard. • A socket address is the combination of an IP address and a port number, much like one end of a telephone connection is the combination of a phone number and a particular extension. Based on this address, internet sockets deliver incoming data packets to the appropriate application process or thread.
  • 15. Socket——选项 • SO_DEBUG • SO_DONTROUTE • SO_KEEPALIVE • SO_DONTLINER • SO_LINGER • SO_RCVBUF • SO_SNDBUF • SO_REUSERADDR • SO_RCVTIMEO • SO_SNDTIMEO • TCP_NODELAY(Nagle)
  • 16. HTTP • Hypertext Transfer Protocol • HTTP functions as a request-response protocol in the client-server computing model.
  • 17. HTTP——Request • Request methods – HEAD/GET/POST/PUT/DELETE/TRACE/OPTIONS/C ONNECT/PATCH
  • 18. HTTP——GET POST • GET 参数 • POST 参数 – application/x-www-form-urlencoded – multipart/form-data(non-ASCII data, and binary data)
  • 23. HTTP——Response • Status codes – 1xx Informational/2xx Success/3xx Redirection/4xx Client Error/5xx Server Error
  • 28. HTTP——Persistent connections • Persistent connections – In HTTP/0.9 and 1.0, the connection is closed after a single request/response pair. In HTTP/1.1 a keep-alive-mechanism was introduced, where a connection could be reused for more than one request.
  • 29. Unix IO 模型 • Unix下共有五种I/O模型 – 阻塞I/O – 非阻塞I/O – I/O复用(select和poll) – 信号驱动I/O(SIGIO) – 异步I/O(Posix.1的aio_系列函数)
  • 37. END