Title: 源码剖析 listen | bloodycoder
Open Graph Title: 源码剖析 listen
X Title: 源码剖析 listen
Description: 从内核源码分析服务器创建socket到accept的过程
Open Graph Description: 从内核源码分析服务器创建socket到accept的过程
X Description: 从内核源码分析服务器创建socket到accept的过程
Keywords:
Opengraph URL: https://bloodycoder.github.io/posts/linux_socket/
Domain: bloodycoder.github.io
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Posts","item":"https://bloodycoder.github.io/posts/"},{"@type":"ListItem","position":2,"name":"源码剖析 listen","item":"https://bloodycoder.github.io/posts/linux_socket/"}]}
{"@context":"https://schema.org","@type":"BlogPosting","headline":"源码剖析 listen","name":"源码剖析 listen","description":"从内核源码分析服务器创建socket到accept的过程","keywords":["socket","linux"],"articleBody":" 目录 前言 socket干了啥 bind 干了啥 listen 干了啥 accept 干了啥 前言 内核版本linux-5.0.1. released on 2019年3月4日\n主要源码位于socket.c中\n//创建socket int s = socket(AF_INET, SOCK_STREAM, 0); //绑定 bind(s, ...) //监听 listen(s, ...) //接受客户端连接 int c = accept(s, ...) //接收客户端数据 recv(c, ...); //将数据打印出来 printf(...) 首先根据syscall_64.tbl可以知道bind、listen、accept都是系统调用\n4 4 4 5 1 3 9 0 c c c c o o o o m m m m m m m m o o o o n n n n s a b l o c i i c c n s k e d t e p e t t n _ _ _ _ x _ _ _ 6 _ x x 4 x 6 6 _ 6 4 4 s 4 _ _ y _ s s s s y y _ y s s b s _ _ i _ s a n l o c d i c c s k e t e p e t t n socket干了啥 创建了一个文件,初始化socket结构体,并且实现了文件和socket的互相绑定。最终返回文件的文件描述符.\nstruct socket { socket_state\tstate; short\ttype; unsigned long\tflags; struct socket_wq\t*wq; struct file\t*file; //socket需要和一个文件绑定. struct sock\t*sk; //sockets的网络层表示 const struct proto_ops\t*ops; }; socket_state的枚举含义\n● SS_FREE = 0,\t/* not allocated\t*/ ● SS_UNCONNECTED,\t/* unconnected to any socket\t*/ ● SS_CONNECTING,\t/* in process of connecting\t*/ ● SS_CONNECTED,\t/* connected to socket\t*/ ● SS_DISCONNECTING\t/* in process of disconnecting\t*/ 注意,调用socket的返回值的fd由sock_map_fd返回.\nbind 干了啥 eee\nlisten 干了啥 在listen中的主要任务:\n设置socket状态为TCP_LISTEN 将socket链入半连接队列的Hash表中 s t r u c t s b s t o o r o c u l k c t f * a t r s c e t p q o _ u p c e e h s n e t , c _ k s b _ o o r c o e k l q ( * * s r r t e e r q q u , _ c s t t o s l o e c n k ) * s k , s t r u c t s k _ b u f f * s k b , 这个看着像三次握手的代码.\naccept 干了啥 ","wordCount":"694","inLanguage":"zh","datePublished":"2022-07-19T00:00:00Z","dateModified":"2022-07-19T00:00:00Z","author":{"@type":"Person","name":"bloodycoder"},"mainEntityOfPage":{"@type":"WebPage","@id":"https://bloodycoder.github.io/posts/linux_socket/"},"publisher":{"@type":"Organization","name":"bloodycoder","logo":{"@type":"ImageObject","url":"https://bloodycoder.github.io/favicon.ico"}}}
| None | IE=edge |
| author | bloodycoder |
| theme-color | #2e2e33 |
| msapplication-TileColor | #2e2e33 |
| og:site_name | bloodycoder |
| og:locale | zh_cn |
| og:type | article |
| article:section | posts |
| article:published_time | 2022-07-19T00:00:00+00:00 |
| article:modified_time | 2022-07-19T00:00:00+00:00 |
| article:tag | Linux |
| twitter:card | summary |
Links:
Viewport: width=device-width,initial-scale=1,shrink-to-fit=no
Robots: index, follow