织梦CMS - 轻松建站从此开始!

罗索

Linux中网络编程的常用函数

落鹤生 发布于 2013-01-25 13:45 点击:次 
1、fcntl 头文件#include fcntl.h #include fcntl.h 定义函数int fcntl(int fd , int cmd); int fcntl(int fd,int cmd,long arg); int fcntl(int fd,int cmd,struct flock * lock); fcntl()用来操作文件描述符的一些特性。参数fd代表欲设置的文件描述词,参数cmd代表欲
TAG:

1、fcntl

头文件 #include <fcntl.h>

#include <fcntl.h>

定义函数 int fcntl(int fd , int cmd);

int fcntl(int fd,int cmd,long arg);

int fcntl(int fd,int cmd,struct flock * lock);

fcntl()用来操作文件描述符的一些特性。参数fd代表欲设置的文件描述词,参数cmd代表欲操作的指令。

其详细情况见参考中。

【1】 http://linux.die.net/man/2/fcntl

【2】 http://baike.baidu.com/view/1930855.htm

【3】 http://blog.mcuol.com/User/lvembededsys/Article/2388_1.htm

【4】 http://blog.chinaunix.net/space.php?uid=21461208&do=blog&cuid=1915256

2、setsockopt

    setsockopt()函数用于任意类型、任意状态套接口的设置选项值。尽管在不同协议层上存在选项,但本函数仅定义了最高的“套接口”层次上的选项。

【1】 http://linux.die.net/man/2/setsockopt

【2】 http://baike.baidu.com/view/569217.htm

3、inet_aton

   inet_aton() converts the Internet host address cp from the standard numbers-and-dots notation into binary data and stores it in the structure that inp points to. inet_aton() returns non-zero if the address is valid, zero if not.

The inet_addr() function也是相同功能。

【1】 http://linux.die.net/man/3/inet_aton

【2】 http://baike.baidu.com/view/1870333.htm

【3】http://linux.die.net/man/3/inet_addr

4、对于sockaddr_in结构体的定义,则不同头文件定义又不同。

struct sockaddr_in {

    sa_family_t    sin_family; /* address family: AF_INET */

    u_int16_t      sin_port;   /* port in network byte order */

    struct in_addr sin_addr;   /* internet address */

};

 

/* Internet address. */

struct in_addr {

    u_int32_t      s_addr;     /* address in network byte order */

};

【1】 http://linux.die.net/man/7/ip

【2】 http://baike.baidu.com/view/2355183.htm

5、htonl()

    将主机的无符号长整形数转换成网络字节顺序。

【1】 http://baike.baidu.com/view/569196.html

【2】 http://linux.die.net/man/3/htonl

6、gettimeofday

    The functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.

【1】 http://linux.die.net/man/2/gettimeofday

【2】 http://blog.sina.com.cn/s/blog_4d61a7570100eci0.html

【3】 http://zhidao.baidu.com/question/75454941

【4】http://hi.baidu.com/%B1%AB%B9%FA%CC%CE/blog/item/778bcc23d850f7ae4623e8a0.html

7、semctl

semctl() performs the control operation specified by cmd on the semaphore set identified by semid, or on the semnum-th semaphore of that set. (The semaphores in a set are numbered starting at 0.)

系统调用semctl用来执行在信号量集上的控制操作。

【1】 http://linux.die.net/man/2/semctl

【2】 http://baike.baidu.com/view/3803900.htm

【3】 http://hi.baidu.com/rhose/blog/item/83e74dd74dcf0d1a3bf3cf5b.html

8、waitpid

    通过waitpid调用和wait调用来收集子进程的信息。

【1】 http://www.cnblogs.com/mydomain/archive/2010/10/24/1859826.html

【2】 http://baike.baidu.com/view/2365304.htm

【3】 http://linux.die.net/man/2/waitpid

9、getopt

getopt()用来分析命令行参数。

int getopt(int argc, char * const argv[], const char *optstring);

调用一次,返回一个选项。在命令行选项参数再也检查不到optstring中包含的选项时,返回-1,同时optind储存第一个不包含在optstring中的选项的命令行参数。

字符串optstring可以下列元素,

(1)单个字符,表示选项,

(2)单个字符后接一个冒号:表示该选项后必须跟一个参数。参数紧跟在选项后或者以空格隔开。该参数的指针赋给optarg

(3)单个字符后跟两个冒号,表示该选项后必须跟一个参数。参数必须紧跟在选项后不能以空格隔开。该参数的指针赋给optarg。(这个特性是GNU的扩展)。

getopt处理以'-’开头的命令行参数,如optstring="ab:c::d::",命令行为getopt.exe -a -b host -ckeke -d haha

在这个命令行参数中,-a和-b就是选项元素,去掉'-',a,b,c就是选项。host是b的参数,keke是c的参数。但haha并不是d的参数,因为它们中间有空格隔开。

默认情况下getopt会重新排列命令行参数的顺序,所以到最后所有不包含在optstring中的选项的命令行参数都排到最后。

如getopt.exe -a ima -b host -ckeke -d haha,包含在optstring中命令行参数是:

-a -b host -ckeke -d

不包含在optstring中的参数为ima及haha,所以重排后,如下所示:

最后命令行参数的顺序是: -a -b host -ckeke -d ima haha

如果optstring中的字符串以'+'加号开头或者环境变量POSIXLY_CORRE被设置。那么一遇到不包含选项的命令行参数,getopt就会停止,返回-1。

【1】 http://baike.baidu.com/view/2406693.htm

【2】 http://linux.die.net/man/3/getopt

【3】 http://www.cnitblog.com/zouzheng/archive/2007/04/02/25034.aspx

【4】 http://blogold.chinaunix.net/u1/50826/showart_472964.html

【5】 http://www.ibm.com/developerworks/cn/aix/library/au-unix-getopt.html

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下,还有一种使用形式,如下:

  1. #define  NO_DATA(fmt, args...) \ 
  2. {\ 
  3. fprintf(stdout, fmt, ##args);\ 
  4.   
  5. int main(int argc, char* argv[]) 
  6. char* pc = new char[14]; 
  7. strncpy(pc, "1234", 5); 
  8. NO_DATA("%d %s", 2, pc); 
  9. delete[] pc; 
  10. 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。

  1. token = strtok( string, seps ); 
  2. while( token != NULL ) 
  3.    /* While there are tokens in "string" */ 
  4.    printf( " %s\n", token ); 
  5.    /* Get next token: */ 
  6.    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()释放。

  1. char *s="Golden Global View"
  2. char *d; 
  3. d=strdup(s); 
  4. printf("%s",d); 
  5. 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中。

  1. char ip_dot_dec[20]; 
  2. struct in_addr na; 
  3. cout << "Input IP addr: "
  4. cin >> ip_dot_dec; 
  5. inet_pton(AF_INET, ip_dot_dec, (void *)&na); 
  6. cout << "inet_pton: 0x" << hex << na.s_addr << endl; 
  7. inet_ntop(AF_INET, (void *)&na, ip_dot_dec, 16); 
  8. 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.

  1. int sfd; 
  2. struct sockaddr_un addr; 
  3. sfd = socket(AF_UNIX, SOCK_STREAM, 0); 
  4. if (sfd == -1) 
  5.     perror("socket"); 
  6.     exit(EXIT_FAILURE); 
  7.   
  8. /* Clear structure */ 
  9. memset(&addr, 0, sizeof(struct sockaddr_un)); 
  10. addr.sun_family = AF_UNIX; 
  11. strncpy(addr.sun_path, MY_SOCK_PATH, sizeof(addr.sun_path) - 1); 
  12.  
  13. if (bind(sfd, (struct sockaddr *) &addr, sizeof(struct sockaddr_un)) == -1) 
  14.     perror("bind"); 
  15.     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函数转换过的本地时间。

(qq78292959)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201301/16485.html]
本文出处:博客园 作者:qq78292959 原文
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容