Socket 函数库(一)

来源:互联网 发布:容忍与自由知乎 编辑:程序博客网 时间:2024/05/17 00:12

accept接受socket连线


相关函数:socket, bind, listen, connect
表头文件:#include <sys/types.h>
               #include <sys/socket.h>
函数定义:int accept(int s, struct sockaddr *addr, int *addrlen)
函数说明:accept()用来接受参数s的socket连线。
参数s的socket必需先经bind()、listen()函数处理过,当有连线进来是accept()会返回一个新的socket处理代码,往后的数据传送与读取就是经由新的socket处理,而原来参数s的socket能继续使用accept()来接受新的连线要求。连线成功时,参数addr所指的结构会被系统填入远程主机的地址数据,参数addrlen为sockaddr的结构长度。
返回值  :成功则返回socket处理代码, 失败则返回-1, 错误原因存于errno中
错误代码:EBADF                      参数s非合法socket处理代码
               EFAULT                    参数addr指针指向无法存取的内存空间
               ENOSTOCK               参数s为一文件描述词,非socket         
               EOPNOTSUPP           指定的socket并非SOCK_STREAM
               EPERM                      防火墙(firewall)拒绝此连线
               ENOBUFS                  系统的缓冲内存不足
               ENOMEM                   核心内存不足

范例:请参listen()

 bind对socket定位


相关函数:socket, accept, connect, listen
表头文件:#include <sys/types.h>
               #include <sys/socket.h>
函数定义:int bind(int sockfd, struct sockaddr *my_addr, int addrlen)
函数说明:bind()用来设置给参数sockfd的socket一个名称。此名称由参数my_addr指向一sockaddr结构, 对于不同的socket domain定义了一个通用的数据结构:

struct  sockaddr{
         unsigned  short  int  sa_family;   /* 为调用socket()的domain参数,即AF_xxxx值 */
         char  sa_data[14];  /* 最多使用14个字符长度  */
};

此sockaddr结构会因使用不同的socket domain而有不同结构的定义,例如使用AF_INET domain, 其sockaddr结构定义便为:
struct socketadd_in
{
     unsigned short int sin_family;   /*  即为sa_family */
     uint16_t  sin_port;                 /* 为使用的port编号 */
     struct  int_addr sin_addr;        /* 为IP地址 */
     unsigned char sin_zero[8];      /* 未用 */
};


struct in_addr
{
      uint32_t  s_addr;
};


参数addrlen为sockaddr的结构长度

返回值   :成功则返回0,  失败则返回-1, 错误原因存于errno中

错误代码:EBADF            参数sockfd非合法socket处理代码
               EACCESS        权限不足
               ENOTSOCK     参数sockfd为一文件描述词,非socket

范例: 参listen()

connect建立socket连线


相关函数:socket, bind, listen
表头文件:#include <sys/types.h>
               #include <sys/socket.h>
函数定义:int connect(int sockfd, struct sockaddr *serv_addr, int addrlen)
函数说明:connect()用来将参数sockfd的socket连到参数serv_addr指定的网络地址。结构sockaddr请参bind()
返回值   :成功则返回0, 失败返回-1, 错误原因存于errno中
错误代码:EBADF        参数sockfd非合法socket处理代码
               EFAULT      参数serv_addr指针指向无法存取的内存空间
               ENOTSOCK 参数sockfd为一文件描述词,非socket
               EISCONN    参数sockfd的socket已是连线状态
               ECONNREFUSED  连线要求被server端拒绝
               ETIMEDOUT        企图连线的操作超过限定时间仍未有响应
               ENETUNREACH     sockaddr结构的sa_family不正确
               EALREADY           socket为不可阻断且先前的连线操作还未完成

范例:
/*
  利用socket的TCP client
   此程序会连线TCP server,并将键盘输入的字符串传送给server
   TCP server范例请参考listen()
*/

#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define  PORT    1234         /* 使用的port号码 */
#define SERVER_IP    "127.0.0.1"   /* SERVER 的IP*/

main()
{
    int s;
    struct sockaddr_in  addr;
    char buffer[256];
    if((s = socket(AF_INET, SOCK_STREAM, 0)) <0) {
        perror("socket");
        exit(1);
    }
   
    /*填写sockaddr_in 结构*/
    bzero(&addr, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT);
    addr.sin_addr.s_addr= inet_addr(SERVER_IP);
   
     /* 尝试连线 */
     if(connect(s, &addr, sizeof(addr))<0) {
           perror("connect");
           exit(1);
      }

     /* 接收由server端传来的信息 */
     recv(s, buffer, sizeof(buffer), 0);
     printf("%s/n", buffer);
     while(1){
             bzero(buffer, sizeof(buffer));
         
             /*  从标准输入设备取得字符串 */     
             read(STDIN_FILENO, buffer, sizeof(buffer));

             /* 将字符串传送给server端 */
             if(send | s, buffer, sizeof(buffer), 0) <0) {
                      perror("send");
                      exit(1);
              }
        }

}

endservent结束网络服务数据的读取


相关函数:getservent, getservbyname, getservbyport, setservent
表头文件:#include <netdb.h>
函数定义:void endservent(void)
函数说明:endservent()用来关闭由getservent()所打开的文件
返回值   :无
范例:参getservent()

endprotoent结束网络协议数据的读取


相关函数:getprotoent, getprotobyname, getprotobynumber, setprotoent
表头文件:#include <netdb.h>
函数定义:void endprotoent(void)
函数说明:endprotoent()用来关闭由getprotoent()所打开的文件
返回值   :无
范例       :参getprotoent()

gethostbyname由主机名称取得网络数据


相关函数:gethostbyaddr, sethostent
表头文件:#include <netdb.h>
函数定义:struct hostent  *gethostbyname(const char *name)
函数说明:gethostbyname()会返回一个hostent结构,参数name可以为一个主机名或IPv4/IPv6的IP地址。
struct  hostent
{
    char *h_name;           /*正式的主机名称*/
    char **h_aliases;        /* 指向主机名称的其他别名 */
    int h_addrtype;          /* 地址的型态, 通常是AF_INET */
    int h_length;             /*地址的长度*/
    char **h_addr_list;     /* 从域名服务器取得该主机的所有地址 */
};

返回值 :成功返回hostent结构指针,失败则返回NULL指针, 错误原因存于h_errno变量中
错误代码:
                HOST_NOT_FOUND                找不到指定的主机
                NO_ADDRESS                        该主机有名称却无IP地址
                NO_RECOVERY                      域名服务器有错误发生
                TRY_AGAIN                           请再调用一次
范例:
/*得用gethostbyname()的简单反查IP程序*/           
#include <stdio.h>
#include <netdb.h>
#include <netinet/in.h>
#include  <arpa/inet.h>

int main(int argc, char *argv[])
{
     struct hostent *hp;
     struct in_addr  in;
     struct sockaddr_in  local_addr;
     
     if(argc<2) return;
     if(!(hp=gethostbyname(argv[1]))) {
             fprintf(stderr, "Can't resolve host./n");
             exit(1);
     }
     
     memcpy(&local_addr.sin_addr.s_addr, hp->h_addr, 4);
     in.s_addr= local_addr.sin_add.s_addr;
     printf("Domain Name:%s /n", argv[1]);
     printf("IP Address: %s/n", inet_ntoa(in));

}

gethostbyaddr由IP地址取得网络数据


相关函数:gethostbyname
表头文件:#inlcude <netdb.h>
函数定义:struct  hostent  *gethostbyaddr(const char *addr, int len, int type)
函数说明:gethostbyaddr()会返回一个hostent结构,参数addr可以为IPv4或IPv6的IP地址,参数len为参数addr的长度,参数type为AF_INET。结构hostent定义请参gethostbyname()
返回值   :成功则返回hostent结构指针, 失败则返回NULL指针,错误原因存于h_errno变量
错误代码:HOST_NOT_FOUND    找不到指定的主机
               NO_ADDRESS             该主机有名称却无IP地址
               NO_RECOVERY           域名服务器有错误发生
               TRY_AGAIN               请再调用一次

范例:
#include <netdb.h>
#include <sys/socket.h>

main(int argc, char *argv[])
{
    struct hostent  *host;
    if(argc<2) return;
    host = gethostbyaddr(argv[1], sizeof(argv[1]), AF_INET);
    if(host == (struct hostent * ) NULL)
         herror("gethostbyaddr");
    else{
           printf("name :%s/n",  host->h_name);
           printf("type  :%s/n", host->h_addrtype);
           printf("addr  :%s/n", host->h_addr_list[0]);
    }
         
}

getprotobyname由网络协议名称取得协议数据


相关函数:getprotobynumber, getprotoent, setprotoent, endprotoent
表头文件:#include <netdb.h>
函数定义:struct protoent *getprotobyname(const char *name)
函数说明:getprotobyname()会返回一个protoent结构,参数name为欲查询的网络协议名称。此函数会从/etc/protocols中查找各个符合条件的数据并由结构protoent返回。结构构protoent定义参getprotoent()
返回值   :成功返回protoent结构指针, 失败或找不到符合的数据则返回NULL指针
范例:
/*取得icmp协议数据*/
#include <netdb.h>
main()
{
      struct protoent *protocol;
      protocol = getprotobyname("icmp");
      printf("protocol name :%s/n", protocol->p_name);
      printf("protocol number: %d/n", protocol->p_proto);
      printf("protocol alias : %s/n", protocol->p_aliases[0]);
}

getprotobynumber由网络协议号取得协议数据


相关函数:getprotobyname, getprotoent, setprotoent, endprotoent
表头文件:#include <netdb.h>
函数定义:struct protoent *getprotobynumber(int proto)
函数说明:getprotobynumber()会返回一个protoent结构,参数proto为欲查询的网络协议编号。此函数会从/etc/protocols中查找符合条件的数据并由结构protoent返回。 结构protoent定义请参getprotoent()
返回值   :成功则返回protoent结构指印,若有错误或找不到各个符合的数据则返回NULL指针

范例:
/*取得协议编号0到4的协议数据*/

#include <netdb.h>
main()
{
    int number;
    struct protoent *protocol;
    for(number=0; number<5; number++)
   {
       protocol = getprotobynumber(number);
       if(protocol == (struct protoent * ) NULL) continue;
       printf("%2d: %-10s: %-10s/n", protocol->p_proto, protocol->p_name, protocol->p_aliases[0]);
   }
}

getservbyname依名称取得网络服务的数据


相关函数:getservent, getservbyport, setservent, endservent
表头文件:#include <netdb.h>
函数定义:struct servent *getservbyname(const char *name, const char *proto)
函数说明:getservbyname()会返回一个servent结构,参数name可以为一个网络服务的名称,参数proto则为该服务所使用的协议。此函数会从/etc/services中查找符合条件的数据并由结构servent返回。结构servent的定义参getservent()
返回值:成功则返回servent结构指针, 失败则返回NULL指针
范例:
#include <netdb.h>

main()
{
    struct servent  *s;
    s=getservbyname("telnet", "tcp");
    printf("%s %d/%s  /n", s->s_name, ntohs(s->s_port), s->s_proto);
}

getservbyport依port号码取得网服务的数据


相关函数:getservent, getservbyname, setservent, endservent
表头文件:#include <netdb.h>
函数定义:struct servent *getservbyport(int prot, const char *proto)
函数说明:getservbyport()会返回一个servent结构,参数port可以为一个port号码,参数proto则为该服务所使用的协议。此函数会从/etc/services中查找符合条件的数据,并由结构servent返回。结构servent的定义请参考getservent()
附加说明:参数port必须先由htons()转换
返回值  :成功则返回servent结构指针, 若有错误则返回NULL指针
范例:
#include <netdb.h>
main()
{
    struct servent *s;
    s=getservbyport(htons(23), "tcp");
    printf("%s %d/%s/n", s->s_name, ntohs(s->s_port), s->s_proto);
}

getservent取得主机网络服务的数据


相关函数:getservbyname, getservbyport, setservent, endservent
表头文件:#include <netdb.h>
函数定义:struct servent *getservent(void)
函数说明:getservent()会打开/etc/services, 然后读取一行数据后由结构servent返回。之后再调用此函数则会继续读取下项数据,除非已到文件尾时返回NULL指针。
struct servent
{
     char  *s_name;     /*服务名称*/
     char  **s_aliases;  /*别名列表*/
     int     s_port;       /*所使用的port号码*/
     char   *s_proto;   /*使用的协议名称*/
};

返回值  :成功则返回servent结构指针, 若有错误或读到了文件尾则返回NULL指针
范例:
/*列出主机所有网络服务的数据*/

#include <netdb.h>
main()
{   
     struct servent *s;
     while((s = getservent())
     {
           printf("%s %d/%s/n", s->s_name, ntobs(s->s_port), s->s_proto);
     }
     endservent();
}

getsockopt取得socket状态


相关函数:setsockopt
表头文件:#include <sys/types.h>
               #include <sys/socket.h>
函数定义:int getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen)
函数说明:getsockopt()会将参数s所指定的socket状态返回。
参数optname代表欲取得何种选项状态
参数optval则指向欲保存结果的内存地址,
参数optlen则为该空间的大小
参数level、optname请参setsockopt()
错误代码:EBADF           参数s并非合法的socket处理代码
               ENOTSOCK     参数s为一文件描述词,非socket
               ENOPROTOOPT 参数optname指定的选项不正确
               EFAULT            参数optval指针指向无法存取的内存空间
范例:
include <sys/types.h>
inlcude <sys/socket.h>

main()
{
    int  s, optval, optlen=sizeof(int);
    if((s=socket(AF_INET, SOCK_STREAM, 0)) <0)
             perror("socket");
    getsockopt(s, SOL_SOCKET, SO_TYPE, &optval, &optlen);
    printf("optval = %d/n", optval);
    close(s);
}

getprotoent取得网络协议数据


相关函数:getprotobyname, getprotobynumber, setprotoent, endprotoent
表头文件:#include <netdb.h>
函数定义:struct protoent *getprotoent(void)
函数说明:getprotoent()会打开/etc/protoclos,然后读取一行数据后由结构protoent返回。之后再调用此函数则会继续读取下一项数据,除非已到文件尾时返回NULL指针
结构protoent定义
struct protoent
{
    char *p_name;    /*official protocol name*/
    char **p_aliases; /*alias list*/
    char p_proto;     /*protocol number*/
};


返回值  :成功则返回protoent结构指针,有错或找不到符合的数据则返回NULL指针
范例:
/*取得所有协议数据*/
#include <netdb.h>

main()
{
    struct protoent *p;
    while(p=getprotoent())
     {
          printf("%s %s %d/n", p->p_name, p->aliases[0], p->p_proto);
     }   
     endprotoent();
}
转自 http://zxjgoodboy.blog.sohu.com