Unix Network Programming Episode 34

本文详细介绍了六种进程控制函数的区别,并解释了如何在Unix环境下编写并发服务器来处理多个客户端请求。此外,还介绍了用于关闭套接字和终止TCP连接的正常Unix close函数,以及获取套接字本地和远程地址的getsockname和getpeername函数。

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

Note the following differences among these six functions:

1.The three functions in the top row specify each argument string as a separate argument to the exec function, with a null pointer terminating the variable number of arguments. The three functions in the second row have an argv array, containing pointers to the argument strings. This argv array must contain a null pointer to specify its end, since a count is not specified.
2.The two functions in the left column specify a filename argument. This is converted into a pathname using the current PATH environment variable. If the filename argument to execlp or execvp contains a slash (/) anywhere in the string, the PATH variable is not used. The four functions in the right two columns specify a fully qualified pathname argument.
3.The four functions in the left two columns do not specify an explicit environment pointer. Instead, the current value of the external variable environ is used for building an environment list that is passed to the new program. The two functions in the right column specify an explicit environment list. The envp array of pointers must be terminated by a null pointer.

Concurrent Servers

The server in Figure 4.11(See 8.2.6) is an iterative server. For something as simple as a daytime server, this is fine. But when a client request can take longer to service, we do not want to tie up a single server with one client; we want to handle multiple clients at the same time. The simplest way to write a concurrent server under Unix is to fork a child process to handle each client.

pid_t pid;
int listenfd, connfd;

listenfd=Socket(...);

Bind(listenfd, ...);

Listen(listenfd, LISTENQ);

for(;;)
{
    connfd=Accept(listenfd, ...);

    if((pid=Fork())==0)
    {
        Close(listenfd);
        doit(connfd);
        Close(connfd);
        exit(0);
    }

    Close(connfd);
}

When a connection is established, accept returns, the server calls fork, and the child process services the client (on connfd, the connected socket) and the parent process waits for another connection (on listenfd, the listening socket). The parent closes the connected socket since the child handles the new client.

close’ Function

The normal Unix close function is also used to close a socket and terminate a TCP connection.

#include <unistd.h>
int close (int sockfd);

The default action of close with a TCP socket is to mark the socket as closed and return to the process immediately. The socket descriptor is no longer usable by the process: It cannot be used as an argument to read or write. But, TCP will try to send any data that is already queued to be sent to the other end, and after this occurs, the normal TCP connection termination sequence takes place.

‘getsockname’ and ‘getpeername’ Functions

These two functions return either the local protocol address associated with a socket (getsockname) or the foreign protocol address associated with a socket (getpeername).

#include <sys/socket.h>
int getsockname(int sockfd, struct sockaddr *localaddr, socklen_t *addrlen);
int getpeername(int sockfd, struct sockaddr *peeraddr, socklen_t *addrlen);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值