Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Openai/692451a2-4ecc-800b-b601-8d7b09eea462
(section)
Add languages
Page
Discussion
English
Read
Edit
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== 学习内容 ==== ===== 《Head First C 嗨翻 C 语言》 第11章 ===== ====== 1. 网络程序概述 ====== * '''组成部分:''' 网络程序通常由'''服务器'''和'''客户端'''两部分组成 。 * '''通信基础:''' 不同计算机上的程序需要对话 。网络通信使用一种新的数据流——'''套接字(Socket)''',用于读写字节 。 * '''协议(Protocol):''' 客户端与服务器之间展开一段'''结构化对话''',这被称为协议 。 * '''低层协议:''' 如IP (Internet Protocol,网际协议) 。 * '''高层协议:''' 如HTTP (Hypertext Transfer Protocol,超文本传输协议) 或自定义协议(如IKKP) 。 ====== 2. 服务器编程:BLAB四部曲 ====== 服务器在使用套接字与客户端通信前,需要经历四个阶段,缩写为 '''BLAB''':'''绑定 (Bind)'''、'''监听 (Listen)'''、'''接受 (Accept)'''和'''开始 (Begin)''' 。 # '''创建套接字(Socket):''' * 使用 socket(PF_INET, SOCK_STREAM, 0) 系统调用创建互联网套接字描述符(listener_d)。 # '''绑定端口(Bind):''' * '''端口(Port):''' 类似于电视频道,用于防止不同对话混淆 。常见的端口有网页(80)、Email(25)、聊天(5222)等 。 * 服务器需要将套接字绑定到特定的端口,通常建议使用 '''1024号以上的端口''' 。 * 使用 bind() 系统调用将套接字描述符和套接字名(表示“互联网IP地址和端口号”的结构体 struct sockaddr_in)进行绑定 。 # '''监听(Listen):''' * 使用 listen(listener_d, 10) 系统调用设置客户端连接的等待'''队列长度'''(例如最多10个客户端排队等待连接)。 # '''接受连接(Accept):''' * 使用 accept() 系统调用等待客户端连接 。当有客户端连接时,accept() 会返回一个'''新的套接字描述符'''(connect_d),服务器将使用此描述符与客户端通信 。 ====== 3. 套接字数据传输 ====== * '''套接字特性:''' 套接字是'''双向'''的,不能使用 fprintf() 和 fscanf() 等传统I/O函数进行通信 。 * '''发送数据:''' 使用 send() 函数向套接字输出数据 。 * '''读取数据:''' 使用 recv() 函数从套接字读取数据 。 * recv() 返回读取的字节数,错误返回-1,客户端关闭连接返回0 。 * recv() 不一定能一次接收到所有字符,可能需要多次调用 。 ====== 4. 服务器高级话题与错误处理 ====== * '''错误检查:''' 必须始终检查所有系统调用的返回值(如 socket、bind、listen、accept、send),以处理可能发生的网络错误 。 * '''端口重用:''' 当服务器关闭后立即重启时,由于'''绑定端口有延时'''(约30秒),bind() 调用可能会失败("Address already in use") 。 * '''解决方法:''' 在调用 bind() 前,使用 setsockopt(listener_d, SOL_SOCKET, SO_REUSEADDR, ...) 设置套接字的 '''SO\_REUSEADDR 选项''',允许重新使用已绑定的端口 。 * '''处理多客户端:''' * 单个进程的服务器在处理一个客户端时,其他客户端会被阻塞 。 * '''解决方案:''' 使用 fork() 系统调用,在接收到连接后,'''克隆一个独立的子进程'''来处理与该客户端的对话 。 * 分派后,父进程关闭用于对话的副套接字(connect_d),子进程关闭主监听套接字(listener_d)。 ====== 5. 客户端编程 ====== 客户端的主动权在于它自己,只需两步即可与服务器建立通信:'''连接远程端口'''和'''开始通信''' 。 # '''解析地址(getaddrinfo):''' * 为了使用域名(如www.oreilly.com)而非IP地址,客户端需要使用 '''DNS(域名系统)''' 将域名转化为IP地址 。 * 使用 getaddrinfo(host, port, &hints, &res) 函数,给定域名和端口号,获取地址信息结构 addrinfo 。 # '''连接远程套接字(Connect):''' * 利用 getaddrinfo 获得的地址信息,首先使用 socket() 创建套接字,然后使用 connect() 系统调用连接远程套接字 。 * 连接后,同样使用 send() 和 recv() 进行数据读写 。
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)