Unix Network Programming
文章平均质量分 65
介绍在Unix的网络编程技术,参考史蒂芬斯的书籍。
duxingzhe103
博学而笃志,切问而近思
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Unix Network Programming Episode 102
if(FD_ISSET(STDIN_FILENO, &rset)) { if((n=read(STDIN_FILENO, toiptr, &to[MAXLINE]-toiptr))<0) { if(errno!=EWOULDBLOCK) err_sys("read...原创 2024-10-11 09:21:26 · 227 阅读 · 0 评论 -
Unix Network Programming Episode 103
#include "unp.h"void str_client(FILE *fp, int sockfd){ pid_t pid; char sendline[MAXLINE], recvline[MAXLINE]; if((pid=Fork())==0) { while(Readline(socfd, recvline, MAXLINE)&...原创 2024-10-11 09:21:51 · 575 阅读 · 0 评论 -
Unix Network Programming Episode 101
在这里插入代码片str_echo function: asks for client’s credentials原创 2024-09-03 09:06:13 · 933 阅读 · 0 评论 -
Unix Network Programming Episode 100
Receiving Sender CredentialsIn Figure 14.13(See 9.3.6), we showed another type of data that can be passed along a Unix domain socket as ancillary data: user credentials. Exactly how credentials are p...原创 2024-09-03 09:05:54 · 892 阅读 · 1 评论 -
Unix Network Programming Episode 99
#include "unp.h"ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd){ struct msghdr msg; struct iovec iov[1]; ssize_t n;#ifdef HAVE_MSGHDR_MSG_CONTROL union{ str...原创 2024-08-05 13:14:59 · 175 阅读 · 0 评论 -
Unix Network Programming Episode 98
#include "unp.h"int my_open(const char *, int);int main(int argc, char **argv){ int fd, n; char buff[BUFFSIZE]; if(argc!=2) err_quit("usage: mycat <pathname>"); ...原创 2024-08-05 13:14:42 · 169 阅读 · 0 评论 -
Unix Network Programming Episode 97
Unix Domain Datagram Client/Server#include "unp.h"int main(int argc, char **argv){ int sockfd; struct sockaddr_un servaddr; sockfd=Socket(AF_LOCAL, SOCK_STREAM,0); bzero(&ser...原创 2024-07-01 10:47:09 · 785 阅读 · 1 评论 -
Unix Network Programming Episode 96
‘socketpair’ FunctionThe socketpair function creates two sockets that are then connected together. This function applies only to Unix domain sockets.#include &lt;sys/socket.h&gt;int socketpair(int ...原创 2024-07-01 10:46:52 · 638 阅读 · 1 评论 -
Unix Network Programming Episode 95
Unix Domain ProtocolsIntroductionThe Unix domain protocols are not an actual protocol suite, but a way of performing client/server communication on a single host using the same API that is used for ...原创 2024-06-03 13:59:56 · 543 阅读 · 1 评论 -
Unix Network Programming Episode 94
The kqueue interface includes the following two functions and macro:#include &lt;sys/types.h&gt;#include &lt;sys/event.h&gt;#include &lt;sys/time.h&gt;int kqueue(void);int kevent(int kq, const st...原创 2024-06-03 13:59:45 · 241 阅读 · 1 评论 -
Unix Network Programming Episode 92
The difference between CMSG_LEN and CMSG_SPACE is that the former does not account for any padding following the data portion of the ancillary data object and is therefore the value to store in cmsg_l...原创 2024-05-06 13:16:44 · 831 阅读 · 1 评论 -
Unix Network Programming Episode 93
The problem here is the buffering performed automatically by the standard I/O library on the server. There are three types of buffering performed by the standard I/O library:1.Fully buffered means th...原创 2024-05-06 13:16:59 · 723 阅读 · 1 评论 -
Unix Network Programming Episode 91
Both functions package most arguments into a msghdr structure.struct msghdr { void *msg_name; /* protocol address */ socklen_t msg_namelen; /* size of protocol address */ struct iovec *msg_iov; /*...原创 2024-04-07 11:01:18 · 932 阅读 · 1 评论 -
Unix Network Programming Episode 90
#include &amp;quot;unp.h&amp;quot;int readable_timeo(int fd, int second){ fd_set rset; struct timeval tv; FD_ZERO(&amp;amp;amp;rset); FD_SET(fd, &amp;amp;amp;rset); tv.tv_sec=second; t原创 2024-04-07 11:01:01 · 309 阅读 · 1 评论 -
Unix Network Programming Episode 89
Advanced I/O FunctionsIntroductionThis chapter covers a variety of functions and techniques that we lump into the category of “advanced I/O.” First is setting a timeout on an I/O operation, which ca...原创 2024-03-04 17:50:41 · 325 阅读 · 1 评论 -
Unix Network Programming Episode 88
‘inetd’ DaemonOn a typical Unix system, there could be many servers in existence, just waiting for a client request to arrive. Examples are FTP, Telnet, Rlogin, TFTP, and so on. With systems before 4...原创 2024-03-04 17:50:24 · 1329 阅读 · 1 评论 -
Unix Network Programming Episode 87
‘daemon_init’ FunctionFigure 13.4 shows a function named daemon_init that we can call (normally from a server) to daemonize the process. This function should be suitable for use on all variants of Un...原创 2024-02-02 09:12:57 · 209 阅读 · 1 评论 -
Unix Network Programming Episode 86
Source Code PortabilityMost existing network applications are written assuming IPv4. sockaddr_in structures are allocated and filled in and the calls to socket specify AF_INET as the first argument. ...原创 2024-02-02 09:12:33 · 807 阅读 · 1 评论 -
Unix Network Programming Episode 85
‘gethostbyname_r’ and ‘gethostbyaddr_r’ FunctionsThere are two ways to make a nonre-entrant function such as gethostbyname re-entrant.1.Instead of filling in and returning a static structure, the ca...原创 2024-01-12 11:49:12 · 1005 阅读 · 1 评论 -
Unix Network Programming Episode 84
#include "unp.h"#include &lt;time.h&gt;int main(int argc, char **argv){ int sockfd; ssize_t n; char buff[MAXLINE]; time_t ticks; socklen_t len; struct sockaddr_storage clie...原创 2024-01-12 11:48:56 · 1028 阅读 · 1 评论 -
Unix Network Programming Episode 83
#include "unp.h"int main(int argc, char **argv){ int sockfd, n; char recvline[MAXLINE+1]; socklen_t salen; struct sockaddr *sa; if(argc!=3) err_quit("usage: daytimeudpc...原创 2023-12-05 10:29:39 · 534 阅读 · 1 评论 -
Unix Network Programming Episode 82
#include "unp.h"#include <time.h>int main(int argc, char **argv){ int listenfd, connfd; socklen_t len; char buff[MAXLINE]; time_t ticks; strct sockaddr_storage clientaddr...原创 2023-12-05 10:29:24 · 572 阅读 · 1 评论 -
Unix Network Programming Episode 81
#include "unp.h"int tcp_listen(const char *host, const char *serv, socklen_t *addrlenp){ int listenfd, n; const int on=1; struct addrinfo hints, res, &ressave; bzero(&hints...原创 2023-11-07 09:26:05 · 262 阅读 · 1 评论 -
Unix Network Programming Episode 80
‘tcp_connect’ FunctionWe will now write two functions that use getaddrinfo to handle most scenarios for the TCP clients and servers that we write. The first function, tcp_connect, performs the nor...原创 2023-11-07 09:25:52 · 279 阅读 · 1 评论 -
Unix Network Programming Episode 79
‘gai_strerror’ FunctionThe nonzero error return values from getaddrinfo have the names and meanings shown in Figure 11.7. The function gai_strerror takes one of these values as an argument and retur...原创 2023-10-07 14:13:24 · 366 阅读 · 1 评论 -
Unix Network Programming Episode 78
‘getaddrinfo’ FunctionThe gethostbyname and gethostbyaddr functions only support IPv4. The API for resolving IPv6 addresses went through several iterations, as will be described in Section 11.20(See...原创 2023-10-07 14:13:07 · 348 阅读 · 1 评论 -
Unix Network Programming Episode 77
‘gethostbyaddr’ FunctionThe function gethostbyaddr takes a binary IPv4 address and tries to find the hostname corresponding to that address. This is the reverse of gethostbyname.#include <netdb...原创 2023-09-04 09:36:29 · 432 阅读 · 1 评论 -
Unix Network Programming Episode 76
We encourage the use of getaddrinfo (Section 11.6(See 8.9.6)) in new programs.#include <netdb.h>struct hostent *gethostbyname (const char *hostname);The non-null pointer returned by this ...原创 2023-09-04 09:36:14 · 617 阅读 · 1 评论 -
Unix Network Programming Episode 75
Controlling Terminationfor(;;){ len=sizeof(struct sockaddr_in); rd_sz=Sctp_recvmsg(sock_fd, readbuf, sizeof(readbuf), (SA *)&amp;amp;cliaddr, &amp;amp;len, &amp;amp;sri, &amp;amp;msg_flags); if(strea...原创 2023-08-04 17:58:33 · 643 阅读 · 0 评论 -
Unix Network Programming Episode 74
SCTP One-to-Many-Style Streaming Echo Client: ‘main’ Function#include &quot;unp.h&quot;int main(int argc, char **argv){ int sock_fd; struct sockaddr_in servaddr; struct sctp_event_subscribe e...原创 2023-08-04 17:58:18 · 787 阅读 · 0 评论 -
Unix Network Programming Episode 72
ssf_flags will be set to one of two values:SCTP_DATA_UNSENT, which indicates that the message could never be transmitted to the peer (e.g., flow control prevented the message from being sent before...原创 2023-07-03 09:21:26 · 130 阅读 · 0 评论 -
Unix Network Programming Episode 73
SCTP One-to-Many-Style Streaming Echo Server: ‘main’ Function#include "unp.h"int main(int argc, char **argv){ int sock_fd, msg_flags; char readbuf[BUFFSZIE]; struct sockaddr_in serv...原创 2023-07-03 09:21:45 · 781 阅读 · 0 评论 -
Unix Network Programming Episode 70
‘sctp_opt_info’ FunctionThe sctp_opt_info function is provided for implementations that cannot use the getsockopt functions for SCTP. This inability to use the getsockopt function is because some of...原创 2023-06-12 10:13:23 · 1436 阅读 · 0 评论 -
Unix Network Programming Episode 69
With SCTP, a one-to-many socket can also be used in conjunction with the sctp_peeloff function (see Section 9.12(See 8.7.12)) to allow the iterative and concurrent server models to be combined as foll...原创 2023-05-05 17:10:39 · 588 阅读 · 1 评论 -
Unix Network Programming Episode 68
Elementary SCTP SocketsIntroductionSCTP is a newer transport protocol, standardized in the IETF in 2000 (compared with TCP, which was standardized in 1981). It was first designed to meet the needs...原创 2023-05-05 17:10:56 · 736 阅读 · 1 评论 -
Unix Network Programming Episode 67
include "unp.h"static void recvfrom_int(int);static int count;void dg_echo(int sockfd, SA *pclientaddr, socklen_t clientlne){ int n; socklen_t len; char mesg[MAXLINE]; Singal(SI...原创 2023-04-03 08:56:03 · 106 阅读 · 1 评论 -
Unix Network Programming Episode 66
PerformanceWhen an application calls sendto on an unconnected UDP socket, Berkeley-derived kernels temporarily connect the socket, send the datagram, and then unconnect the socket (pp. 762–763 of TC...原创 2023-04-03 08:55:42 · 136 阅读 · 1 评论 -
Unix Network Programming Episode 65
Lost DatagramsOur UDP client/server example is not reliable. If a client datagram is lost (say it is discarded by some router between the client and server), the client will block forever in its cal...原创 2023-03-01 10:07:26 · 118 阅读 · 1 评论 -
Unix Network Programming Episode 64
#include "unp.h"int main(int argc, char **argv){ int sockfd; struct sockaddr_in servaddr, cliaddr; sockfd=Socket(AF_INET, SOCK_DGRAM,0); bzero(&amp;servaddr, sizeof(servaddr)); ...原创 2023-03-01 10:06:56 · 88 阅读 · 1 评论 -
Unix Network Programming Episode 63
‘fcntl’ Functionfcntl 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...原创 2023-02-01 16:08:35 · 268 阅读 · 1 评论