Unix Network Programming
文章平均质量分 65
介绍在Unix的网络编程技术,参考史蒂芬斯的书籍。
duxingzhe103
博学而笃志,切问而近思
展开
-
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 · 142 阅读 · 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 · 125 阅读 · 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 · 744 阅读 · 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 · 594 阅读 · 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 · 497 阅读 · 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 · 205 阅读 · 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 · 780 阅读 · 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 · 668 阅读 · 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 · 896 阅读 · 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 · 272 阅读 · 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 · 282 阅读 · 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 · 1283 阅读 · 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 · 182 阅读 · 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 · 768 阅读 · 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 · 965 阅读 · 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 · 978 阅读 · 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 · 511 阅读 · 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 · 547 阅读 · 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 · 230 阅读 · 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 · 251 阅读 · 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 · 333 阅读 · 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 · 314 阅读 · 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 · 394 阅读 · 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 · 581 阅读 · 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 · 604 阅读 · 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 · 746 阅读 · 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 · 91 阅读 · 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 · 743 阅读 · 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 · 1390 阅读 · 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 · 558 阅读 · 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 · 693 阅读 · 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 · 76 阅读 · 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 · 109 阅读 · 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 · 89 阅读 · 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 · 59 阅读 · 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 · 235 阅读 · 1 评论 -
Unix Network Programming Episode 62
sinfo_pid provides the default value to use when setting the SCTP payload protocol identifier in all data transmissions.sinfo_context specifies the default value to place in the sinfo_context field,...原创 2023-02-01 16:08:45 · 411 阅读 · 1 评论 -
Unix Network Programming Episode 61
SCTP Socket OptionsThe relatively large number of socket options for SCTP (17 at present writing) reflects the finer grain of control SCTP provides to the application developer. We specify the level...原创 2023-01-03 08:53:37 · 416 阅读 · 1 评论 -
Unix Network Programming Episode 60
IPV6_CHECKSUM Socket OptionThis socket option specifies the byte offset into the user data where the checksum field is located. If this value is non-negative, the kernel will: (i) compute and store...原创 2023-01-03 08:54:02 · 399 阅读 · 1 评论 -
Unix Network Programming Episode 59
We can summarize our discussion of these socket options with the following recommendations:1.Set the SO_REUSEADDR socket option before calling bind in all TCP servers. 2.When writing a multicast ap...原创 2022-12-01 10:06:24 · 428 阅读 · 1 评论