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!
====== 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),服务器将使用此描述符与客户端通信 。
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)