Unix Network Programming Episode 63

fcntl函数在Linux系统中用于执行文件描述符的各种控制操作,特别是在网络编程中,它可以设置非阻塞I/O和信号驱动I/O。O_NONBLOCK标志用于使socket变为非阻塞模式,而O_ASYNC则与SIGIO信号关联,用于当socket状态改变时的通知。此外,文章提到了recvfrom和sendto这两个函数在UDP编程中的使用,它们与read和write类似,但需要额外的参数来指定数据来源或目标地址。文章还介绍了UDP的基本特性,如其无连接性和不可靠性,并指出了一些使用UDP的应用,如DNS和SNMP。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

‘fcntl’ Function

fcntl stands for “file control” and this function performs various descriptor control
operations. Before describing the function and how it affects a socket, we need to look
at the bigger picture.

The fcntl function provides the following features related to network programming:

  • Nonblocking I/O— We can set the O_NONBLOCK file status flag using the F_SETFL command to set a socket as nonblocking. We will describe nonblocking I/O in Chapter 16(See 9.5).
  • Signal-driven I/O— We can set the O_ASYNC file status flag using the F_SETFL command, which causes the SIGIO signal to be generated when the status of a socket changes. We will discuss this in Chapter 25(See 9.14).
  • The F_SETOWN command lets us set the socket owner (the process ID or process group ID) to receive the SIGIO and SIGURG signals. The former signal is generated when signal-driven I/O is enabled for a socket (Chapter 25(See 9.14)) and the latter signal is generated when new out-of-band data arrives for a socket (Chapter 24(See 9.13)). The F_GETOWN command returns the current owner of the socket.

The term “socket owner” is defined by POSIX. Historically, Berkeley-derived implementations have called this “the process group ID of the socket” because the variable that stores this ID is the so_pgid member of the socket structure (p. 438 of TCPv2).

#include <fcntl.h>
int fcntl(intfd, int cmd, ... /* int arg */ );

Each descriptor (including a socket) has a set of file flags that is fetched with the F_GETFL command and set with the F_SETFL command. The two flags that affect a socket are

  • O_NONBLOCK—nonblocking I/O
  • O_ASYNC—signal-driven I/O

Elementary UDP Sockets

Introduction

There are some fundamental differences between applications written using TCP versus those that use UDP. These are because of the differences in the two transport layers: UDP is a connectionless, unreliable, datagram protocol, quite unlike the connection-oriented, reliable byte stream provided by TCP. Nevertheless, there are instances when it makes sense to use UDP instead of TCP, and we will go over this design choice in Section 22.4(See 9.11.4). Some popular applications are built using UDP: DNS, NFS, and SNMP, for example.

‘recvfrom’ and ‘sendto’ Functions

These two functions are similar to the standard read and write functions, but three additional arguments are required.

#include <sys/socket.h>
ssize_t recvfrom(int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen);
ssize_t sendto(int sockfd, const void *buff, size_t nbytes, int flags, const struct sockaddr *to, socklen_t addrlen);
UDP Echo Server: ‘main’ Function

We will now redo our simple echo client/server from Chapter 5(See 8.3) using UDP. Our UDP client and server programs follow the function call flow that we diagrammed in Figure 8.1(See 8.6.1). Figure 8.2 depicts the functions that are used. Figure 8.3 shows the server main function.

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值