SlideShare a Scribd company logo
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

More Related Content

PPT
Socket programming
PPT
Features of tcp (part 2) .68
PPT
Ports & sockets
PPT
Eshcol tech solutions pvt ltd
PPT
Np unit iii
PPTX
TCPLS presentation @ietf 109
PDF
Meeting 6 : ftp
PDF
TCP - Transmission Control Protocol
Socket programming
Features of tcp (part 2) .68
Ports & sockets
Eshcol tech solutions pvt ltd
Np unit iii
TCPLS presentation @ietf 109
Meeting 6 : ftp
TCP - Transmission Control Protocol

What's hot (20)

DOCX
Surviving The Stump The Chump Interview Questions
PDF
Port numbers
PDF
Dhcp Snooping
PDF
CCNA 1 Chapter 7 v5.0 2014
PPTX
Client server examples for tcp abnormal conditions
PPT
PPTX
Part 7 : HTTP/2, UDP and TCP
PPT
Chap 18 telnet
DOCX
Network interview questions
PPTX
Part 6 : Internet applications
PDF
IMS Session Flow
PDF
Solar windsportrequirements
PPTX
Well known protocols port numbers
PPTX
Part 12 : Local Area Networks
PPT
IP/MAC Address Translation
PPTX
Part 4 : reliable transport and sharing resources
PPTX
Transmission Control Protocol (TCP)
PPT
Traffic Characterization
PPT
Initial LTE call Setup Flow
PPTX
TELNET and SSH by MUSTAFA SAKHAI
Surviving The Stump The Chump Interview Questions
Port numbers
Dhcp Snooping
CCNA 1 Chapter 7 v5.0 2014
Client server examples for tcp abnormal conditions
Part 7 : HTTP/2, UDP and TCP
Chap 18 telnet
Network interview questions
Part 6 : Internet applications
IMS Session Flow
Solar windsportrequirements
Well known protocols port numbers
Part 12 : Local Area Networks
IP/MAC Address Translation
Part 4 : reliable transport and sharing resources
Transmission Control Protocol (TCP)
Traffic Characterization
Initial LTE call Setup Flow
TELNET and SSH by MUSTAFA SAKHAI
Ad

Viewers also liked (8)

PPT
Course on TCP Dynamic Performance
PPT
TCP Model
PDF
TCP with delayed ack for wireless networks
PPT
Lecture 5
PDF
CS4344 09/10 Lecture 10: Transport Protocol for Networked Games
PPT
Flow & Error Control
PPTX
4 transport-sharing
PPT
2 reliability-network
Course on TCP Dynamic Performance
TCP Model
TCP with delayed ack for wireless networks
Lecture 5
CS4344 09/10 Lecture 10: Transport Protocol for Networked Games
Flow & Error Control
4 transport-sharing
2 reliability-network
Ad

Similar to Socket网络编程 (20)

PPTX
ITN6_Instructor_Materials_Chapter9git.pptx
PPTX
tcp-ippresentation-150614172243-lva1-app6892.pptx
PDF
JavaSockets-Session10 New York university.pdf
PPTX
CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9
PDF
TCP - IP Presentation
PPTX
Tcp ip presentation
PPTX
Java socket programming
PPTX
lecturer3.pptx
PPTX
Unit V computer network notes for study.
PPTX
Unit-4 (1).pptx
PPT
TCP/IP(networking)
PPT
Np unit iv ii
PPTX
Transmission Control Protocol_ Computer Networks
PPTX
08 coms 525 tcpip - tcp 1
PPTX
PPTX
Distributed Computing - API for Internet Protocols
PPT
Data Transmission flow using TCP protocol
PDF
materi uas jarkom tahun ajaraan 2022.pdf
ITN6_Instructor_Materials_Chapter9git.pptx
tcp-ippresentation-150614172243-lva1-app6892.pptx
JavaSockets-Session10 New York university.pdf
CCNA (R & S) Module 01 - Introduction to Networks - Chapter 9
TCP - IP Presentation
Tcp ip presentation
Java socket programming
lecturer3.pptx
Unit V computer network notes for study.
Unit-4 (1).pptx
TCP/IP(networking)
Np unit iv ii
Transmission Control Protocol_ Computer Networks
08 coms 525 tcpip - tcp 1
Distributed Computing - API for Internet Protocols
Data Transmission flow using TCP protocol
materi uas jarkom tahun ajaraan 2022.pdf

Recently uploaded (20)

PDF
cuic standard and advanced reporting.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Machine learning based COVID-19 study performance prediction
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Big Data Technologies - Introduction.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
cuic standard and advanced reporting.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine learning based COVID-19 study performance prediction
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Spectroscopy.pptx food analysis technology
sap open course for s4hana steps from ECC to s4
Programs and apps: productivity, graphics, security and other tools
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
MYSQL Presentation for SQL database connectivity
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Unlocking AI with Model Context Protocol (MCP)
Big Data Technologies - Introduction.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
NewMind AI Weekly Chronicles - August'25 Week I
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars

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