Networking and Sockets (3)

7 篇文章 0 订阅

The Domain Name System (DNS)

#include <sys/socket.h>
#include <netdb.h>
/* Structure to contain information about address of a service provider.  */
struct addrinfo
{
  int ai_flags;            /* Input flags.  */
  int ai_family;        /* Protocol family for socket.  */
  int ai_socktype;        /* Socket type.  */
  int ai_protocol;        /* Protocol for socket.  */
  socklen_t ai_addrlen;        /* Length of socket address.  */
  struct sockaddr *ai_addr;    /* Socket address for socket.  */
  char *ai_canonname;        /* Canonical name for service location.  */
  struct addrinfo *ai_next;    /* Pointer to next in list.  */
};

getaddrinfo - get socket-address information

int getaddrinfo(

  const char* nodename,  /* can be a name to be looked up using DNS server,

                                                  a name defined in the local machine's /etc/hosts file,

                                                  an IPv4 address in dotted notation, or IPv6 in colon notation  */

  const char* servname,

  const struct addrinfo *hint,

  const addrinfo **infop    /* OUT */

);

const char* gai_strerror( int code /* error code return by getaddrinfo/getnameinfo */ );

void freeaddrinfo( struct addrinfo *info);

#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char** argv)
{
    struct addrinfo *infop = NULL, hint;

    memset(&hint, 0 , sizeof(hint));
    hint.ai_family = AF_INET;
    hint.ai_socktype = SOCK_STREAM;

    getaddrinfo("www.yahoo.com", "80", &hint, &infop);
    for (; infop != NULL; infop = infop->ai_next)
    {
        struct sockaddr_in *sa = (struct sockaddr_in*)infop->ai_addr;

        printf("%s port: %d protocol: %d\n", inet_ntoa(sa->sin_addr),
                ntohs(sa->sin_port), infop->ai_protocol);
    }

    return 0;
}
gethostname - get the name of its own host

#include <unistd.h>

int gethostname( char* name,  size_t namelen);


Network Database Functions

Host Functions

#include <netdb.h>

/* Description of data base entry for a single host.  */
struct hostent
{
  char *h_name;			/* Official name of host.  */
  char **h_aliases;		/* Alias list.  */
  int h_addrtype;		/* Host address type.  */
  int h_length;			/* Length of address.  */
  char **h_addr_list;		/* List of addresses from name server.  */
}
Scanning the host database
static void hostdb()
{
    struct hostent *h;

    sethostent(true);
    while ((h = gethostent()) != NULL)
        display_hostent(h);
    endhostent();
}
-----
name: localhost;
aliases: 127.0.0.1
type: 2;
len: 4
-----
look up host by name

struct hostent* gethostbyname( const char *nodename);
-----
gethostbyname("www.yahoo.com");
-----
look up host by address
struct hostent* gethostbyaddr(     const void *addr,    socklent_t len,    int family);
-----
struct hostent *h;
in_addr_t a;
a = inet_addr("98.137.149.56");
h = gethostbyaddr(&a, sizeof(a), AF_INET);
-----
Network Functions
Protocol Functions
Service Functions

Network Interface Functions


Miscellaneous System calls

send and recv - behave exactly like write and read, except the allow you to spedify flags, e.g., MSG_OOB for both, MSG_PEEK, MSG_WAITALL for recv.
ssize_t send(
        int socket_fd,
        const void *data,
        size_t length,
        int flags
);
ssize_t recv(
        int socket_fd,
        void *buffer,
        size_t length,
        int flags
);

getsockname - get socket address that a sockethas been bound to with a previous call to bind.
int getsockname(
        int socket_fd,
        struct sockaddr *sa,
        socklen_t *sa_len
);

getpeername - get the socket address of the socketconnected to a socket
int getpeername(
        int socket_fd,
        struct sockaddr *sa,
        socklen_t *sa_len
);

socketpair - create pair of sockets, as with bidirectional FIFOs
int socketpair(
        int domain,         // AF_UNIX, AF_INET, etc.
        int type,           // SOCK_STREAM, SOCK_DGRAM
        int protocol,       // usually zero
        int socket_vector[2] // return
);

shutdown - shut down socket send and/or receive operations
int shutdown(
        int socket_fd,
        int how        // SHUT_RD, SSHUT_WR, SHUT_RDWR
);




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值