Linux??Socket???
???????????? ???????[ 2015/11/10 11:21:05 ] ??????????????
????????Socket
????Socket?????TCP/IP?????API??Socket???????????????????????????????????????????TCP/IP?????????ó?????Internet???TCP/IP????????????????Socket????
????Socket???????????????????Unix??????????????????Unix??????????????????????????Socket???????? Socket?????????????????I/O??Socket?????????????????Socket???????????????????????????Socket()???ú????? ??????????Socket?????????????????????????????????????????Socket??????????Socket??????????????Socket ??SOCK_STREAM??????????Socket??SOCK_DGRAM?????????????????????Socket????????????????TCP???????????? ???Socket????????????Socket??????????????UDP??????á?
????Socket????
??????????Socket????????????Socket???????ú???????????????????????????????socket??????????
????int socket(int domain?? int type?? int protocol);
????domain?????????Э???壬????PF_INET???????????Э???壨TCP/IPЭ???壩??type???????socket??????? SOCK_STREAM ??SOCK_DGRAM??Socket????????????Socket??SOCK_RAW????????????????Э?飻protocol??????"0"?? Socket()???÷??????????socket?????????????????????????????
????Socket????????????????????????????????????????????????Socket???????socket????彫???????Socket???????"???????Socket"??ζ??????Socket?????????洢???Socket????????????????????
?????????????????????????????????????????????Э?顢????Э?????????????????????????????????Э?????Socket??????а??????????????
????Socket????
???????socket???÷??????socket?????????????socket???????紫????????????????socket???????????socket???????? ????Connect??????socket??????б??汾?????????????????socket??????????????????????socket????????????? bind??????????????????
????Bind??????socket????????????????????????????????????????????Bind??????????
????int bind(int sockfd??struct sockaddr *my_addr?? int addrlen);
????Sockfd?????socket?????????socket????????my_addr????????????б???IP???????????????sockaddr????????addrlen?????????sizeof(struct sockaddr)??
????struct sockaddr????????????????socket??????
????struct sockaddr {
????unsigned short sa_family; /* ????壬 AF_xxx */
????char sa_data[14]; /* 14 ????Э???? */
????};
????sa_family????AF_INET??????Internet??TCP/IP??????壻sa_data???????socket??IP?????????
???????????????????
????struct sockaddr_in {
????short int sin_family; /* ????? */
????unsigned short int sin_port; /* ???? */
????struct in_addr sin_addr; /* IP??? */
????unsigned char sin_zero[8]; /* ???0 ???????struct sockaddr?????С */
????};
?????????????????á?sin_zero??????sockaddr_in???????struct sockaddr???????????????bzero()??memset()????????????????sockaddr_in ?????????sockaddr???????????????????ζ?????????????????????????sockaddr??????????????????????????? sockaddr_in????????????sockaddr?????????????
???????bind????????????????????????????????IP????????????????б?????????
????my_addr.sin_port = 0; /* ???????????δ????????? */
????my_addr.sin_addr.s_addr = INADDR_ANY; /* ??????IP??? */
?????????my_addr.sin_port???0????????????????????δ??????????á???????????my_addr.sin_addr.s_addr???INADDR_ANY???????????????IP?????
????????????bind???????????sin_port??sin_addr??????????????????????sin_addr??????????
?????????????洢??????????????????λ?????????λ????????Internet?????????λ??????????????????????????????????????λ??????????洢????????????Internet???????????????????????????????????????
????????????????????????????
????·htonl()????32λ??????????????????????????
????·htons()????16λ??????????????????????????
????·ntohl()????32λ??????????????????????????
????·ntohs()????16λ??????????????????????????
????Bind()???????????????????0??????????????"-1"????errno????????????????????????????bind??????????????????С??1024????????1??1024????????????????????1024?е??κ??????б?????????
???????????
??????????????????????Connect??????????socket???????????????????TCP??????亯????????
????int connect(int sockfd?? struct sockaddr *serv_addr??int addrlen);
????Sockfd ??socket?????????socket????????serv_addr????????????IP????????????addrlen??????????????? Connect?????????????????-1??????????errno??????????????п?????????????????bind()?????????????????????????? ??IP?????????????????????????????????????????????socket?????????????????????δ???????????????????????????? ?????
????Connect???????????????????????????????????????????????socket??????????socket?????????????????????Э?????????????????????????????????????????????????????????Э????????????????
????Listen?????socket?????????????????????socket?????????????????У????????????????????????У???????????????
????int listen(int sockfd?? int backlog);
????Sockfd ??Socket?????÷????socket ????????backlog???????????????????????????????????????????????е??accept()??????ο????????Backlog??????е?? ???????????????????????????????????20????????????????????????????????????socket?????????????????????????????????
??????????????listen????????-1???????????errno??????
????accept()?????÷??????????????????????????????????к???????????accept?????????????????????????????
????int accept(int sockfd?? void *addr?? int *addrlen);
????sockfd?????????socket????????addr???????????sockaddr_in???????????????????????????????????????????????? ????????????????????????addrten????????????sizeof(struct sockaddr_in)??????????????????????accept????????-1?????????errno???
???????????accept????????? socket??????????????socket????彫????????μ?socket??????彫?????socket????????????????????????????????????? ???socket??????????????? socket????????????????μ?socket???????????????????????
???????????
????Send()??recv()???????????????????????socket????????????
????Send()??????????
????int send(int sockfd?? const void *msg?? int len?? int flags);
????Sockfd???????????????????socket????????msg?????????????????????Len??????????λ???????????flags???????????0??????ò??????÷??????man?????
????Send()???????????????????????????????????????????????????????????y?send()???????????????????????б?????send()???????len??????????????????????д????
????char *msg = "Hello!";
????int len?? bytes_sent;
????……
????len = strlen(msg);
????bytes_sent = send(sockfd?? msg??len??0);
????……
????recv()??????????
????int recv(int sockfd??void *buf??int len??unsigned int flags);
????Sockfd??????????socket????????buf ???????????????????len??????????Flags??????0??Recv()????????????????????????????????????-1?????????errno???
????Sendto()??recvfrom()??????????????????socket??????????????????????socket???????????????????????????????????????????????
????sendto()??????????
????int sendto(int sockfd?? const void *msg??int len??unsigned int flags??const struct sockaddr *to?? int tolen);
?????ú?????send()??????????????????to?????????IP???????????????tolen??????????sizeof (struct sockaddr)??Sendto ???????????????????????????????????????????-1??
????Recvfrom()??????????
????int recvfrom(int sockfd??void *buf??int len??unsigned int flags??struct sockaddr *from??int *fromlen);
????from?????struct sockaddr????????????????????????IP??????????fromlen?????sizeof (struct sockaddr)????recvfrom()???????fromlen??????????from?е????????????Recvfrom()????????????????????? ??????????????-1???????????errno??
???????????????socket??????connect()??????????????????send()??recv()???????????????socket??????????socket???????????????UDP?????????????????????????????????????????????????
????????????
?????????е???????????????????????close()??????????socket??????????socket????κ??????????
????close(sockfd);
??????????????shutdown()??????????socket???ú????????????????????????????????????????????????????????С???????????socket??д????????????????socket??????????????????????????
????int shutdown(int sockfd??int how);
????Sockfd?????????socket?????????????? how?????shutdown??????????????????
????·0-------?????????????????
????·1-------?????????????????
????·2-------??????????????????????
????·????????????close ()
????shutdown?????????????0???????????????-1???????errno??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11