Common network programming functions in Linux (2)

Source: Internet
Author: User
Tags strtok

10. Use the # connector in a Linux environment in combination with ARGs...

The usage of #,## is described in [1], and the usage of va_list is described in [2.

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

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

In Linux, there is also a form of use, as follows:

# 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;

}

The output value is 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 shoshould be specified in S. in each subsequent call that shocould parse the same string, s shocould be null.

If any character that matches the character set delim is found in the S string, replace the character in the S string with a null character. That is to say, in the call process, the string S is changed.

This function returns a string separated by characters in the delim character set starting with S. If no split string exists, null is returned.

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. The space required is allocated by malloc () and can (must) be released by 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_ton (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.

Convert the address to the in_addr struct and copy it to * DST.

Char ip_dot_dec [20];

Struct in_addr NA;

Cout <"input ip addr :";

Cin> ip_dot_dec;

Inet_ton (af_inet, ip_dot_dec, (void *) & Na );

Cout <"inet_ton: 0x"

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] Baidu encyclopedia

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.

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 );

Create a new socket and return the file descriptor of the socket. The new socket is used for communication between the server and the client, and the original socket is still in the listening status.

[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 );

Connect the socket of the sockfd parameter to the network address specified by the parameter 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. That is, get the address of the socket peer.

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

Returned File Information

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 mostly refers to the local time converted using the localtime function.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.