zoukankan      html  css  js  c++  java
  • socket 2.草稿。

    字序转换。
    htonl
    htons
    ntohl
    ntohs

    struck sockaddr
    {
    sa_family_t sa_family;
    char sa_data[];
    }

    struck in_addr
    {
    in_addr_t s_addr;
    }

    struck sockaddr_in
    {
    sa_family_t sa_family;
    in_port_t sin_port;
    struct in_addr sin_addr;
    }

    二进制和点分转换。
    inet_ntop inet_pton
    #include <arpa/inet.h>
    const char *inet_ntop(int domain, const void *restrict addr,char *restrict str, socklen_t size);
    Returns: pointer to address string on success, NULL on error

    int inet_pton(int domain, const char *restrict str,void *restrict addr);
    Returns: 1 on success, 0 if the format is invalid, or −1 on error

    地址查询。
    struct hostent {
    char *h_name; /* name of host */
    char **h_aliases; /* pointer to alternate host name array */
    int h_addrtype; /* address type */
    int h_length; /* length in bytes of address */
    char **h_addr_list; /* pointer to array of network addresses */
    .
    .
    .
    };


    #include <netdb.h>
    struct hostent *gethostent(void);
    Returns: pointer if OK, NULL on error
    void sethostent(int stayopen);
    void endhostent(void);


    早期函数:gethostbyname and gethostbyaddr


    struct netent {
    char *n_name; /* network name */
    char **n_aliases; /* alternate network name array pointer */
    int n_addrtype; /* address type */
    uint32_t n_net; /* network number */
    .
    .
    .
    };

    struct netent *getnetbyaddr(uint32_t net, int type);
    struct netent *getnetbyname(const char *name);
    struct netent *getnetent(void);
    All return: pointer if OK, NULL on error
    void setnetent(int stayopen);
    void endnetent(void);


    服务和名字之间的转换。
    struct protoent {
    char *p_name; /* protocol name */
    char **p_aliases; /* pointer to alternate protocol name array */
    int p_proto; /* protocol number */
    .
    .
    .
    };

    #include <netdb.h>
    struct protoent *getprotobyname(const char *name);
    struct protoent *getprotobynumber(int proto);
    struct protoent *getprotoent(void);
    All return: pointer if OK, NULL on error
    void setprotoent(int stayopen);
    void endprotoent(void);


    查询服务。
    struct servent {
    char *s_name; /* service name */
    char **s_aliases; /* pointer to alternate service name array */
    int s_port; /* port number */
    char *s_proto; /* name of protocol */
    .
    .
    .
    };


    #include <netdb.h>
    struct servent *getservbyname(const char *name, const char *proto);
    struct servent *getservbyport(int port, const char *proto);
    struct servent *getservent(void);
    All return: pointer if OK, NULL on error
    void setservent(int stayopen);
    void endservent(void);


    //根据服务和主机名,返回一个节点数据结构是assrinfo的链表。
    #include <sys/socket.h>
    #include <netdb.h>

    struct addrinfo {
    int ai_flags; /* customize behavior */
    int ai_family; /* address family */
    int ai_socktype; /* socket type */
    int ai_protocol; /* protocol */
    socklen_t ai_addrlen; /* length in bytes of address */
    struct sockaddr *ai_addr; /* address */
    char *ai_canonname; /* canonical name of host */
    struct addrinfo *ai_next; /* next in list */
    .
    .
    .
    };

    int getaddrinfo(const char *restrict host,
    const char *restrict service,
    const struct addrinfo *restrict hint,
    struct addrinfo **restrict res);
    Returns: 0 if OK, nonzero error code on error
    void freeaddrinfo(struct addrinfo *ai);

    //将地址转换为主机名和服务名
    #include <sys/socket.h>
    #include <netdb.h>
    int getnameinfo(const struct sockaddr *restrict addr, socklen_t alen,
    char *restrict host, socklen_t hostlen,
    char *restrict service, socklen_t servlen, int flags);
    Returns: 0 if OK, nonzero on error


    //通过socket得到address。
    #include <sys/socket.h>
    int getsockname(int sockfd, struct sockaddr *restrict addr,
    socklen_t *restrict alenp);
    Returns: 0 if OK, −1 on error

    //如果已经连接。还可以通过socket得到对端address。

    #include <sys/socket.h>
    int getpeername(int sockfd, struct sockaddr *restrict addr,
    socklen_t *restrict alenp);
    Returns: 0 if OK, −1 on error


    //连接。
    #include <sys/socket.h>
    int connect(int sockfd, const struct sockaddr *addr, socklen_t len);
    Returns: 0 if OK, −1 on error


    //监听。
    //#include <sys/socket.h>
    int listen(int sockfd, int backlog);
    Returns: 0 if OK, −1 on error


    //接受。
    #include <sys/socket.h>
    int accept(int sockfd, struct sockaddr *restrict addr,
    socklen_t *restrict len);
    Returns: file (socket) descriptor if OK, −1 on error

    //数据传输。
    #include <sys/socket.h>
    ssize_t send(int sockfd, const void *buf, size_t nbytes, int flags);
    Returns: number of bytes sent if OK, −1 on error


    //数据传输。非可靠连接
    #include <sys/socket.h>
    ssize_t sendto(int sockfd, const void *buf, size_t nbytes, int flags,
    const struct sockaddr *destaddr, socklen_t destlen);
    Returns: number of bytes sent if OK, −1 on error

    //更多选择。
    #include <sys/socket.h>
    ssize_t sendmsg(int sockfd, const struct msghdr *msg, int flags);
    Returns: number of bytes sent if OK, −1 on error

    //接受数据。
    #include <sys/socket.h>
    ssize_t recv(int sockfd, void *buf, size_t nbytes, int flags);
    Returns: length of message in bytes,
    0 if no messages are available and peer has done an orderly shutdown,

    //更多选项。
    #include <sys/socket.h>
    ssize_t recvfrom(int sockfd, void *restrict buf, size_t len, int flags,
    struct sockaddr *restrict addr,
    socklen_t *restrict addrlen);
    Returns: length of message in bytes,
    0 if no messages are available and peer has done an orderly shutdown,
    or −1 on error
    or −1 on error

    //
    #include <sys/socket.h>
    ssize_t recvmsg(int sockfd, struct msghdr *msg, int flags);
    Returns: length of message in bytes,
    0 if no messages are available and peer has done an orderly shutdown,

    //socket option

    #include <sys/socket.h>
    int setsockopt(int sockfd, int level, int option, const void *val,
    socklen_t len);
    Returns: 0 if OK, −1 on error

    //带外数据。

    #include <sys/socket.h>
    int sockatmark(int sockfd);
    Returns: 1 if at mark, 0 if not at mark, −1 on error

    //非阻塞和异步。

  • 相关阅读:
    对于“机器视觉(computer version)”的反思
    图像视觉的调试
    对自主标定的实现
    使用dll,将image库开放出去
    使用liner、feather、multiband对已经拼接的数据进行融合(下)
    使用liner、feather、multiband对已经拼接的数据进行融合
    模版多匹配
    Metasploit AFP爆破模块afp_login
    SQLite中SELECT基本形式
    Nmap 7.70新增功能——扫描主机所有IP
  • 原文地址:https://www.cnblogs.com/lsfv/p/6350331.html
Copyright © 2011-2022 走看看