Unix Network Programming
文章平均质量分 65
介绍在Unix的网络编程技术,参考史蒂芬斯的书籍。
duxingzhe103
博学而笃志,切问而近思
展开
-
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 · 876 阅读 · 1 评论 -
Unix Network Programming Episode 90
#include "unp.h"int readable_timeo(int fd, int second){ fd_set rset; struct timeval tv; FD_ZERO(&rset); FD_SET(fd, &rset); tv.tv_sec=second; t原创 2024-04-07 11:01:01 · 261 阅读 · 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 · 269 阅读 · 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 · 1268 阅读 · 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 · 177 阅读 · 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 · 754 阅读 · 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 · 944 阅读 · 1 评论 -
Unix Network Programming Episode 84
#include "unp.h"#include <time.h>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 · 959 阅读 · 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 · 499 阅读 · 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 · 538 阅读 · 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 · 219 阅读 · 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 · 243 阅读 · 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 · 316 阅读 · 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 · 296 阅读 · 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 · 373 阅读 · 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 · 565 阅读 · 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 · 597 阅读 · 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 · 730 阅读 · 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 · 71 阅读 · 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 · 726 阅读 · 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 · 1377 阅读 · 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 · 551 阅读 · 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 · 667 阅读 · 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 · 65 阅读 · 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 · 97 阅读 · 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 · 77 阅读 · 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 · 44 阅读 · 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 · 226 阅读 · 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 · 398 阅读 · 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 · 412 阅读 · 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 · 382 阅读 · 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 · 404 阅读 · 1 评论 -
Unix Network Programming Episode 58
SO_OOBINLINE Socket OptionWhen this option is set, out-of-band data will be placed in the normal input queue (i.e., inline). When this occurs, the MSG_OOB flag to the receive functions cannot be use...原创 2022-12-01 10:06:01 · 358 阅读 · 1 评论 -
Unix Network Programming Episode 57
SO_KEEPALIVE Socket OptionWhen the keep-alive option is set for a TCP socket and no data has been exchanged across the socket in either direction for two hours, TCP automatically sends a keep-alive ...原创 2022-11-07 16:02:05 · 176 阅读 · 0 评论 -
Unix Network Programming Episode 56
int main(int argc, char **argv){ int fd; socklen_t len;main function to check all socket optionssock_str_flag function: convert flag option to a string原创 2022-11-07 16:01:48 · 143 阅读 · 0 评论 -
Unix Network Programming Episode 55
Socket OptionsIntroductionThere are various ways to get and set the options that affect a socket:The getsockopt and setsockopt functionsThe fcntl functionThe ioctl functionThis chapter s...原创 2022-10-08 11:30:00 · 182 阅读 · 0 评论 -
Unix Network Programming Episode 54
TCP Echo Server (Revisited Again)We now redo our TCP echo server from Section 6.8(See 8.4.8) using poll instead of select. In the previous version using select, we had to allocate a client array alo...原创 2022-10-08 11:29:42 · 61 阅读 · 0 评论 -
Unix Network Programming Episode 53
Denial-of-Service AttacksUnfortunately, there is a problem with the server that we just showed. Consider what happens if a malicious client connects to the server, sends one byte of data (other than...原创 2022-09-05 15:41:56 · 75 阅读 · 0 评论 -
Unix Network Programming Episode 52
The first argument is a pointer to the first element of an array of structures. Each element of the array is a pollfd structure that specifies the conditions to be tested for a given descriptor, fd....原创 2022-09-05 15:41:39 · 106 阅读 · 0 评论 -
Unix Network Programming Episode 51
Denial-of-Service AttacksUnfortunately, there is a problem with the server that we just showed. Consider what happens if a malicious client connects to the server, sends one byte of data (other than...原创 2022-08-01 08:49:39 · 53 阅读 · 0 评论