10、Linux环境下的##连接符与args...混合使用

    前面【1】中叙述了#,##的使用方法,【2】中叙述了va_list的使用方法。

1】 http://www.cnblogs.com/mydomain/archive/2010/09/25/1834917.html

2】 http://www.cnblogs.com/mydomain/archive/2010/12/06/1898187.html       

Linux下,还有一种使用形式,如下:

#define  NO_DATA(fmt, args...) \

{\

fprintf(stdout, fmt, ##args);\

}

 

int main(int argc, char* argv[])

{

char* pc = new char[14];

strncpy(pc, "1234", 5);

NO_DATA("%d %s", 2, pc);

delete[] pc;

return 1;

}

output 是2 1234

11、strtok

char *strtok(char *s, const char *delim);

    The strtok() function parses a string into a sequence of tokens. On the first call to strtok() the string to be parsed should be specified in s. In each subsequent call that should parse the same string, s should be NULL.

只要在s字符串中发现与字符集delim中相符的任一个字符,则将s字符串中该字符换成a null character。也就是说,在调用的过程中,字串s被改变了。

该函数返回从s开头开始的一个个被delim字符集中字符分割的串。当没有被分割的串时则返回NULL

   token = strtok( string, seps );

   while( token != NULL )

   {

      /* While there are tokens in "string" */

      printf( " %s\n", token );

      /* Get next token: */

      token = strtok( NULL, seps );

   }

1】 http://linux.die.net/man/3/strtok

2】 http://baike.baidu.com/view/1028553.htm

12、strdup

#include <string.h>

char *strdup(const char *s);

    The strdup() function returns a pointer to a new string which is a duplicate of the string s.所需空间由malloc()分配,且可以(必须)free()释放。

    char *s="Golden Global View";

    char *d;

    d=strdup(s);

    printf("%s",d);

    free(d);

1】 http://baike.baidu.com/view/1028541.htm

2】 http://linux.die.net/man/3/strdup

12、inet_ntop

const char *inet_ntop(int af, const void *src,char *dst, socklen_t cnt);

    converts the network address structure src in the af address family into a character string, which is copied to a character buffer dst, which is cnt bytes long.

int inet_pton(int af, const char *src, void *dst);

    This function converts the character string src into a network address structure in the af address family, then copies the network address structure to dst.

    将该地址转换为in_addr的结构体,并复制在*dst中。

    char ip_dot_dec[20];

    struct in_addr na;

    cout << "Input IP addr: ";

    cin >> ip_dot_dec;

    inet_pton(AF_INET, ip_dot_dec, (void *)&na);

    cout << "inet_pton: 0x" << hex << na.s_addr << endl;

    inet_ntop(AF_INET, (void *)&na, ip_dot_dec, 16);

    cout << "inet_ntop: " << ip_dot_dec << endl;

1】 http://linux.die.net/man/3/inet_ntop

2】 百度百科

13、socket

int socket(int domain, int type, int protocol);

    socket() creates an endpoint for communication and returns a descriptor.

    On success, a file descriptor for the new socket is returned. On error, -1 is returned, and errno is set appropriately.

int sock_fd = socket(PF_INET, SOCK_STREAM, 0);

1】 http://linux.die.net/man/2/socket

14、bind

int bind(int sockfd, const struct sockaddr *my_addr, socklen_t addrlen);

    bind() gives the socket sockfd the local address my_addr. my_addr is addrlen bytes long. Traditionally, this is called "assigning a name to a socket."

    On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

int sfd;

struct sockaddr_un addr;

 

sfd = socket(AF_UNIX, SOCK_STREAM, 0);

if (sfd == -1)

{

    perror("socket");

    exit(EXIT_FAILURE);

}

 

/* Clear structure */

memset(&addr, 0, sizeof(struct sockaddr_un));

addr.sun_family = AF_UNIX;

strncpy(addr.sun_path, MY_SOCK_PATH, sizeof(addr.sun_path) - 1);

if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1)

{

    perror("bind");

    exit(EXIT_FAILURE);

}

1】 http://linux.die.net/man/2/bind

15、listen

int listen(int sockfd, int backlog);

    The listen() call applies only to sockets of type SOCK_STREAM or SOCK_SEQPACKET.

The backlog parameter defines the maximum length the queue of pending connections may grow to.

int ret = listen(_socket_fd, 32);

1】 http://linux.die.net/man/2/listen

16、accept

int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);

    创建新的套接字,并返回该套接字的文件描述符。新创建的套接字用于服务器与客户机的通信,而原来的套接字仍然处于监听状态。

1】 http://linux.die.net/man/2/accept

2】 http://baike.baidu.com/view/521407.htm

17、connect

int connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);

    用来将参数sockfd socket 连至参数serv_addr 指定的网络地址。

1】 http://linux.die.net/man/2/connect

2】 http://baike.baidu.com/view/888434.htm

18、getpeername

int getpeername(int s, struct sockaddr *name, socklen_t *namelen);

    getpeername() returns the name of the peer connected to socket s. 也就是获取socket的对方地址。

    getsockname() returns the current name for the specified socket.

1】 http://baike.baidu.com/view/569194.html

2】 http://linux.die.net/man/2/getsockname

19、stat

返回文件的信息

int stat(const char *path, struct stat *buf);

These functions return information about a file.

On success, zero is returned. On error, -1 is returned, and errno is set appropriately.

1】 http://linux.die.net/man/2/stat

20、strftime

size_t strftime(char *s, size_t max, const char *format, const struct tm *tm);

    The strftime() function formats the broken-down time tm according to the format specification format and places the result in the character array s of size max.

1】 http://baike.baidu.com/view/1284677.htm

2】 http://linux.die.net/man/3/strftime

tm 多是用localtime函数转换过的本地时间。

posted on 2013-01-18 09:45  一个人的天空@  阅读(381)  评论(0编辑  收藏  举报