sockethtml
http://man.lupaworld.com/content/develop/joyfire/system/9.htmlredis
socket |
咱们使用系统调用socket()来得到文件描述符:
#include<sys/types.h>
#include<sys/socket.h>
int socket(int domain,int type,int protocol);
第一个参数domain设置为“AF_INET”。
第二个参数是套接口的类型:SOCK_STREAM或
SOCK_DGRAM。第三个参数设置为0。
系统调用socket()只返回一个套接口描述符,若是出错,则返回-1。数据结构
一旦你有了一个套接口之后,下一步就是把套接口绑定到本地计算机的某一个端口上。但若是你只想使用connect()则无此必要。
下面是系统调用bind()的使用方法:
#include<sys/types.h>
#include<sys/socket.h>
intbind(int sockfd,struct sockaddr*my_addr,int addrlen);
第一个参数sockfd是由socket()调用返回的套接口文件描述符。
第二个参数my_addr是指向数据结构sockaddr的指针。数据结构sockaddr中包括了关于你的地址、端口和IP地址的信息。
第三个参数addrlen能够设置成sizeof(structsockaddr)。
下面是一个例子:
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#defineMYPORT3490
main()
{
int sockfd;
struct sockaddr_inmy_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);/*do someerror checking!*/
my_addr.sin_family=AF_INET;/*hostbyteorder*/
my_addr.sin_port=htons(MYPORT);/*short,network byte order*/
my_addr.sin_addr.s_addr=inet_addr("132.241.5.10");
bzero(&(my_addr.sin_zero),8);/*zero the rest of the struct*/
/*don't forget your error checking for bind():*/
bind(sockfd,(struct sockaddr*)&my_addr,sizeof(struct sockaddr));
...
若是出错,bind()也返回-1。
若是你使用connect()系统调用,那么你没必要知道你使用的端口号。当你调用connect()时,它检查套接口是否已经绑定,若是没有,它将会分配一个空闲的端口。dom
系统调用connect()的用法以下:
#include<sys/types.h>
#include<sys/socket.h>
int connect(int sockfd,struct sockaddr* serv_addr,int addrlen);
第一个参数仍是套接口文件描述符,它是由系统调用socket()返回的。
第二个参数是serv_addr是指向数据结构sockaddr的指针,其中包括目的端口和IP地址。
第三个参数可使用sizeof(structsockaddr)而得到。
下面是一个例子:
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#defineDEST_IP"132.241.5.10"
#defineDEST_PORT23
main()
{
intsockfd;
structsockaddr_indest_addr;/*will hold the destination addr*/
sockfd=socket(AF_INET,SOCK_STREAM,0);/*do some error checking!*/
dest_addr.sin_family=AF_INET;/*hostbyteorder*/
dest_addr.sin_port=htons(DEST_PORT);/*short,network byte order*/
dest_addr.sin_addr.s_addr=inet_addr(DEST_IP);
bzero(&(dest_addr.sin_zero),8);/*zero the rest of the struct*/
/*don'tforgettoerrorchecktheconnect()!*/
connect(sockfd,(structsockaddr*)&dest_addr,sizeof(struct sockaddr));
...
一样,若是出错,connect()将会返回-1。socket
若是你但愿不链接到远程的主机,也就是说你但愿等待一个进入的链接请求,而后再处理它们。这样,你经过首先调用listen(),而后再调用accept()来实现。
系统调用listen()的形式以下:
intl isten(int sockfd,int backlog);
第一个参数是系统调用socket()返回的套接口文件描述符。
第二个参数是进入队列中容许的链接的个数。进入的链接请求在使用系统调用accept()应答以前要在进入队列中等待。这个值是队列中最多能够拥有的请求的个数。大多数系统的缺省设置为20。你能够设置为5或者10。当出错时,listen()将会返回-1值。
固然,在使用系统调用listen()以前,咱们须要调用bind()绑定到须要的端口,不然系统内核将会让咱们监听一个随机的端口。因此,若是你但愿监听一个端口,下面是应该使用的系统调用的顺序:
socket();
bind();
listen();
/*accept()goeshere*/spa
系统调用accept()比较起来有点复杂。在远程的主机可能试图使用connect()链接你使用
listen()正在监听的端口。但此链接将会在队列中等待,直到使用accept()处理它。调用accept()
以后,将会返回一个全新的套接口文件描述符来处理这个单个的链接。这样,对于同一个链接
来讲,你就有了两个文件描述符。原先的一个文件描述符正在监听你指定的端口,新的文件描
述符能够用来调用send()和recv()。
调用的例子以下:
#include<sys/socket.h>
intaccept(intsockfd,void*addr,int*addrlen);
第一个参数是正在监听端口的套接口文件描述符。第二个参数addr是指向本地的数据结构
sockaddr_in的指针。调用connect()中的信息将存储在这里。经过它你能够了解哪一个主机在哪一个
端口呼叫你。第三个参数一样可使用sizeof(structsockaddr_in)来得到。
若是出错,accept()也将返回-1。下面是一个简单的例子:
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#defineMYPORT3490/*theportuserswillbeconnectingto*/
#defineBACKLOG10/*howmanypendingconnectionsqueuewillhold*/
main()
{
intsockfd,new_fd;/*listenonsock_fd,newconnectiononnew_fd*/
structsockaddr_inmy_addr;/*myaddressinformation*/
structsockaddr_intheir_addr;/*connector'saddressinformation*/
intsin_size;
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=INADDR_ANY;/*auto-fillwithmyIP*/
bzero(&(my_addr.sin_zero),8);/*zerotherestofthestruct*/
/*don'tforgetyourerrorcheckingforthesecalls:*/
bind(sockfd,(structsockaddr*)&my_addr,sizeof(structsockaddr));
listen(sockfd,BACKLOG);
sin_size=sizeof(structsockaddr_in);
new_fd=accept(sockfd,&their_addr,&sin_size);
...
下面,咱们将可使用新建立的套接口文件描述符new_fd来调用send()和recv()。指针
系统调用send()的用法以下:
int send(int sockfd,const void* msg,int len,int flags);
第一个参数是你但愿给发送数据的套接口文件描述符。它能够是你经过socket()系统调用返回的,也能够是经过accept()系统调用获得的。
第二个参数是指向你但愿发送的数据的指针。
第三个参数是数据的字节长度。第四个参数标志设置为0。
下面是一个简单的例子:
char*msg="Beejwashere!";
intlen,bytes_sent;
..
len=strlen(msg);
bytes_sent=send(sockfd,msg,len,0);
...
系统调用send()返回实际发送的字节数,这可能比你实际想要发送的字节数少。若是返回的字节数比要发送的字节数少,你在之后必须发送剩下的数据。当send()出错时,将返回-1。
系统调用recv()的使用方法和send()相似:
int recv(int sockfd,void* buf,int len,unsigned int flags);
第一个参数是要读取的套接口文件描述符。
第二个参数是保存读入信息的地址。
第三个参数是缓冲区的最大长度。第四个参数设置为0。
系统调用recv()返回实际读取到缓冲区的字节数,若是出错则返回-1。
这样使用上面的系统调用,你能够经过数据流套接口来发送和接受信息。rest
由于数据报套接口并不链接到远程的主机上,因此在发送数据包以前,咱们必须首先给出目的地址,请看:
int sendto(int sockfd,const void* msg,int len,unsigned int flags,
conststruct sockaddr*to,inttolen);
除了两个参数之外,其余的参数和系统调用send()时相同。
参数to是指向包含目的IP地址和端口号的数据结构sockaddr的指针。
参数tolen能够设置为sizeof(structsockaddr)。
系统调用sendto()返回实际发送的字节数,若是出错则返回-1。
系统调用recvfrom()的使用方法也和recv()的十分近似:
int recvfrom(int sockfd,void* buf,int len,unsigned int flags
struct sockaddr* from,int* fromlen);
参数from是指向本地计算机中包含源IP地址和端口号的数据结构sockaddr的指针。
参数fromlen设置为sizeof(struct sockaddr)。
系统调用recvfrom()返回接收到的字节数,若是出错则返回-1。orm
你可使用close()调用关闭链接的套接口文件描述符:
close(sockfd);
这样就不能再对此套接口作任何的读写操做了。
使用系统调用shutdown(),可有更多的控制权。它容许你在某一个方向切断通讯,或者切断双方的通讯:
int shutdown(int sockfd,int how);
第一个参数是你但愿切断通讯的套接口文件描述符。第二个参数how值以下:
0—Furtherreceivesaredisallowed
1—Furthersendsaredisallowed
2—Furthersendsandreceivesaredisallowed(likeclose())
shutdown()若是成功则返回0,若是失败则返回-1。htm
这个系统的调用十分简单。它将告诉你是谁在链接的另外一端:
#include<sys/socket.h>
int getpeername(int sockfd,struct sockaddr* addr,int* addrlen);
第一个参数是链接的数据流套接口文件描述符。
第二个参数是指向包含另外一端的信息的数据结构sockaddr的指针。
第三个参数能够设置为sizeof(structsockaddr)。
若是出错,系统调用将返回-1。
一旦你得到了它们的地址,你可使用inet_ntoa()或者gethostbyaddr()来获得更多的信息。
系统调用gethostname()比系统调用getpeername()还简单。它返回程序正在运行的计算机的名字。系统调用gethostbyname()可使用这个名字来决定你的机器的IP地址。
下面是一个例子:
#include<unistd.h>
int gethostname(char*hostname,size_tsize);
若是成功,gethostname将返回0。若是失败,它将返回-1。