欢迎来到天天文库
浏览记录
ID:42595795
大小:134.20 KB
页数:9页
时间:2019-09-18
《linux socket 常用函数》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、linuxsocket常用函数收藏========TCP========TCP_Serversocket()bind()listen()accept()write()read()close()和shutdown()TCP_Clientsocket()connect()send()和recv()close()和shutdown()========UDP========UDP_Serversocket()bind()sendto()和recvfrom()close()和shutdown()UDP_Clientsocket()sendto(
2、)和recvfrom()close()和shutdown()getpeername()gethostname()--------------------------------------------------------------------------------socket()我们使用系统调用socket()来获得文件描述符:#include#includeintsocket(intdomain,inttype,intprotocol);第一个参数domain设置为“AF
3、_INET”。第二个参数是套接口的类型:SOCK_STREAM或SOCK_DGRAM。第三个参数设置为0。系统调用socket()只返回一个套接口描述符,如果出错,则返回-1。--------------------------------------------------------------------------------bind()一旦你有了一个套接口以后,下一步就是把套接口绑定到本地计算机的某一个端口上。但如果你只想使用connect()则无此必要。下面是系统调用bind()的使用方法:#include4、es.h>#includeintbind(intsockfd,structsockaddr*my_addr,intaddrlen);第一个参数sockfd是由socket()调用返回的套接口文件描述符。第二个参数my_addr是指向数据结构sockaddr的指针。数据结构sockaddr中包括了关于你的地址、端口和IP地址的信息。第三个参数addrlen可以设置成sizeof(structsockaddr)。下面是一个例子:viewplaincopytoclipboardprint?#include5、ing.h>#include#include#defineMYPORT3490main(){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);/*dosomeerrorchecking!*/my_addr.sin_family=AF_INET;/*hostbyteorder*/my_addr.sin_port=htons(MYPORT);/*short,networkbyteorder*/6、my_addr.sin_addr.s_addr=inet_addr("132.241.5.10");bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*//*don'tforgetyourerrorcheckingforbind():*/bind(sockfd,(structsockaddr*)&my_addr,sizeof(structsockaddr));...}#include#include#include7、t.h>#defineMYPORT3490main(){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);/*dosomeerrorchecking!*/my_addr.sin_family=AF_INET;/*hostbyteorder*/my_addr.sin_port=htons(MYPORT);/*short,networkbyteorder*/my_addr.sin_addr.s_addr=inet_addr("132.241.5.8、10");bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*//*don'tforgetyourerrorcheckingforbind():*/bind(soc
4、es.h>#includeintbind(intsockfd,structsockaddr*my_addr,intaddrlen);第一个参数sockfd是由socket()调用返回的套接口文件描述符。第二个参数my_addr是指向数据结构sockaddr的指针。数据结构sockaddr中包括了关于你的地址、端口和IP地址的信息。第三个参数addrlen可以设置成sizeof(structsockaddr)。下面是一个例子:viewplaincopytoclipboardprint?#include5、ing.h>#include#include#defineMYPORT3490main(){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);/*dosomeerrorchecking!*/my_addr.sin_family=AF_INET;/*hostbyteorder*/my_addr.sin_port=htons(MYPORT);/*short,networkbyteorder*/6、my_addr.sin_addr.s_addr=inet_addr("132.241.5.10");bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*//*don'tforgetyourerrorcheckingforbind():*/bind(sockfd,(structsockaddr*)&my_addr,sizeof(structsockaddr));...}#include#include#include7、t.h>#defineMYPORT3490main(){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);/*dosomeerrorchecking!*/my_addr.sin_family=AF_INET;/*hostbyteorder*/my_addr.sin_port=htons(MYPORT);/*short,networkbyteorder*/my_addr.sin_addr.s_addr=inet_addr("132.241.5.8、10");bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*//*don'tforgetyourerrorcheckingforbind():*/bind(soc
5、ing.h>#include#include#defineMYPORT3490main(){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);/*dosomeerrorchecking!*/my_addr.sin_family=AF_INET;/*hostbyteorder*/my_addr.sin_port=htons(MYPORT);/*short,networkbyteorder*/
6、my_addr.sin_addr.s_addr=inet_addr("132.241.5.10");bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*//*don'tforgetyourerrorcheckingforbind():*/bind(sockfd,(structsockaddr*)&my_addr,sizeof(structsockaddr));...}#include#include#include7、t.h>#defineMYPORT3490main(){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);/*dosomeerrorchecking!*/my_addr.sin_family=AF_INET;/*hostbyteorder*/my_addr.sin_port=htons(MYPORT);/*short,networkbyteorder*/my_addr.sin_addr.s_addr=inet_addr("132.241.5.8、10");bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*//*don'tforgetyourerrorcheckingforbind():*/bind(soc
7、t.h>#defineMYPORT3490main(){intsockfd;structsockaddr_inmy_addr;sockfd=socket(AF_INET,SOCK_STREAM,0);/*dosomeerrorchecking!*/my_addr.sin_family=AF_INET;/*hostbyteorder*/my_addr.sin_port=htons(MYPORT);/*short,networkbyteorder*/my_addr.sin_addr.s_addr=inet_addr("132.241.5.
8、10");bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*//*don'tforgetyourerrorcheckingforbind():*/bind(soc
此文档下载收益归作者所有