type
status
date
slug
summary
tags
category
icon
password
引入
- a typical network application consists of a pair of programs—a client program and a server program—resisting in two different end system
- these processes communicate with each other by reading from, and writing to, sockets
- 有两种网络应用
- 一种是由协议标准中所定义的操作的实现
- 开放:定义其操作的这些规则为人们所共知
- 如果两个程序都遵从同一个规则,则二者可以交互操作
- 另一类网络应用程序是专用的网络应用程序
- 应用层协议没有公开发布
- 应用程序是运行在TCP还是UDP上
- TCP:面向连接,为两个端系统之间的数据流动提供可靠的字节通道
- UDP:无连接的,从一个端系统向另一个端系统发送独立的数据分组
Socket Programming with UDP
要点
each process is analogous to a house and the process’s socket is analogous to a door.
The application resides on one side of the door in the house; the transport-layer protocol resides on the other side of the door in the outside world.
- 使用UDP时,必须先将目的地址附在该分组之上
Before the sending process can push a packet of data out the socket door, when using UDP, it must first attach a destination address to the packet.
- 当packet通过传输层到达接收进程的socket时,接收进程通过socket获得packet
- destination address
- 发送进程为分组附上目的地址
- 包括:
- 接收方的IP地址
- 接收方socket的port number(端口号)
- the sender’s source address
- 该源地址也要附着在packet之上
- 但发送方的源地址不是UDP应用程序代码所为,而是由底层操作系统自动完成的
- 也包括源主机的IP地址和源socket的端口号
because a host may be running many network application processes, each with one or more sockets, it is also necessary to identify the particular socket in the destination host.
When a socket is created, an identifier, called a port number, is assigned to it(分配给它)
编程案例
- The client reads a line of characters (data) from its keyboard and sends the data to the server.
- The server receives the data and converts the characters to uppercase.
- The server sends the modified data to the client.
- The client receives the modified data and displays the line on its screen.
代码解释
UDPClient.py
UDPServer.py
Socket Programming with TCP
要点
- TCP is a connection-oriented protocol
- 创建TCP连接时,先要与client的socket地址以及server的socket地址相关联(包括IP地址和端口号)
- 连接建立以后,当任意一方想发信息给另一方时,只需要将数据通过它的socket扔给TCP connection就行了(不需要像UDP一样在包上附着destination address)
- the client has the job of initiating contact with the server
- First, as in the case of UDP, the TCP server must be running as a process before the client attempts to initiate contact. Second, the server program must have a special door—more precisely, a special socket.
- we will sometimes refer to the client’s initial contact as “knocking on the welcoming door.”
- When the client creates its TCP socket, it specifies the address of the welcoming socket in the server, namely, the IP address of the server host and the port number of the socket.
- After creating its socket, the client initiates a three-way handshake and establishes a TCP connection with the server.
- When the server “hears” the knocking, it creates a new door—more precisely, a new socket that is dedicated to that particular client.
- From the application’s perspective, the client’s socket and the server’s connection socket are directly connected by a pipe
编程案例
代码解释
TCPClient.py
TCPServer.py
- Author:orangec
- URL:orange’s blog | welcome to my blog (clovy.top)/66d4aa38fa044689aa77129305e49d50
- Copyright:All articles in this blog, except for special statements, adopt BY-NC-SA agreement. Please indicate the source!
Relate Posts