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函數轉換過的本地時間。