锘??xml version="1.0" encoding="utf-8" standalone="yes"?>蜜桃麻豆www久久,久久av免费天堂小草播放,国内精品久久久久久中文字幕http://m.shnenglu.com/sscchh-2000/category/1672.html涓庡ぇ瀹朵竴璧峰垎浜煡璇?/description>zh-cnThu, 22 May 2008 18:33:12 GMTThu, 22 May 2008 18:33:12 GMT60Unix緗戠粶緙栫▼絎笁鐗?鍗蜂竴:濂楁帴瀛楄仈緗慉PI瀛︿範絎旇(3)http://m.shnenglu.com/sscchh-2000/archive/2006/05/17/7292.html鍙蹭紶綰?/dc:creator>鍙蹭紶綰?/author>Wed, 17 May 2006 01:42:00 GMThttp://m.shnenglu.com/sscchh-2000/archive/2006/05/17/7292.htmlhttp://m.shnenglu.com/sscchh-2000/comments/7292.htmlhttp://m.shnenglu.com/sscchh-2000/archive/2006/05/17/7292.html#Feedback0http://m.shnenglu.com/sscchh-2000/comments/commentRss/7292.htmlhttp://m.shnenglu.com/sscchh-2000/services/trackbacks/7292.html
涓轟簡甯姪鎴戜滑鐞嗚Вconncet錛宎ccept錛宑lose榪欏嚑涓嚱鏁幫紝浠ュ強浣跨敤netstat宸ュ叿鏉ヨ皟璇昑CP搴旂敤紼嬪簭錛屾垜浠繀欏葷悊瑙CP榪炴帴鏄浣曞緩绔嬪拰緇堟鐨勫拰TCP鐘舵佽漿鎹㈠浘銆?br />

涓夋鎻℃墜

褰撲竴涓猅CP榪炴帴寤虹珛鏃訛紝鍙戠敓浜嗕互涓嬪満鏅細

  1. The server must be prepared to accept an incoming connection. This is normally done by calling socket, bind, and listen and is called a passive open.

  2. The client issues an active open by calling connect. This causes the client TCP to send a "synchronize" (SYN) segment, which tells the server the client's initial sequence number for the data that the client will send on the connection. Normally, there is no data sent with the SYN; it just contains an IP header, a TCP header, and possible TCP options (which we will talk about shortly).

  3. The server must acknowledge (ACK) the client's SYN and the server must also send its own SYN containing the initial sequence number for the data that the server will send on the connection. The server sends its SYN and the ACK of the client's SYN in a single segment.

  4. The client must acknowledge the server's SYN.

鏈灝戦渶瑕佷笁嬈″寘浜ゆ崲錛屽洜姝ょО浣淭CP鐨勪笁嬈℃彙鎵嬶紝濡備笅鍥炬墍紺猴細
鍥劇ず錛毬燭CP 鐨勪笁嬈℃彙鎵?/p>

graphics/02fig02.gif

We show the client's initial sequence number as J and the server's initial sequence number as K. The acknowledgment number in an ACK is the next expected sequence number for the end sending the ACK. Since a SYN occupies one byte of the sequence number space, the acknowledgment number in the ACK of each SYN is the initial sequence number plus one. Similarly, the ACK of each FIN is the sequence number of the FIN plus one.

An everyday analogy for establishing a TCP connection is the telephone system [Nemeth 1997]. The socket function is the equivalent of having a telephone to use. bind is telling other people your telephone number so that they can call you. listen is turning on the ringer so that you will hear when an incoming call arrives. connect requires that we know the other person's phone number and dial it. accept is when the person being called answers the phone. Having the client's identity returned by accept (where the identify is the client's IP address and port number) is similar to having the caller ID feature show the caller's phone number. One difference, however, is that accept returns the client's identity only after the connection has been established, whereas the caller ID feature shows the caller's phone number before we choose whether to answer the phone or not. If the DNS is used (Chapter 11), it provides a service analogous to a telephone book. getaddrinfo is similar to looking up a person's phone number in the phone book. getnameinfo would be the equivalent of having a phone book sorted by telephone numbers that we could search, instead of a book sorted by name.

TCP 閫夐」

Each SYN can contain TCP options. Commonly used options include the following:

  • MSS option. With this option, the TCP sending the SYN announces its maximum segment size, the maximum amount of data that it is willing to accept in each TCP segment, on this connection. The sending TCP uses the receiver's MSS value as the maximum size of a segment that it sends. We will see how to fetch and set this TCP option with the TCP_MAXSEG socket option (Section 7.9).

  • Window scale option. The maximum window that either TCP can advertise to the other TCP is 65,535, because the corresponding field in the TCP header occupies 16 bits. But, high-speed connections, common in today's Internet (45 Mbits/sec and faster, as described in RFC 1323 [Jacobson, Braden, and Borman 1992]), or long delay paths (satellite links) require a larger window to obtain the maximum throughput possible. This newer option specifies that the advertised window in the TCP header must be scaled (left-shifted) by 0鈥?4 bits, providing a maximum window of almost one gigabyte (65,535 x 214). Both end-systems must support this option for the window scale to be used on a connection. We will see how to affect this option with the SO_RCVBUF socket option (Section 7.5).

    To provide interoperability with older implementations that do not support this option, the following rules apply. TCP can send the option with its SYN as part of an active open. But, it can scale its windows only if the other end also sends the option with its SYN. Similarly, the server's TCP can send this option only if it receives the option with the client's SYN. This logic assumes that implementations ignore options that they do not understand, which is required and common, but unfortunately, not guaranteed with all implementations.

  • Timestamp option. This option is needed for high-speed connections to prevent possible data corruption caused by old, delayed, or duplicated segments. Since it is a newer option, it is negotiated similarly to the window scale option. As network programmers there is nothing we need to worry about with this option.

These common options are supported by most implementations. The latter two are sometimes called the "RFC 1323 options," as that RFC [Jacobson, Braden, and Borman 1992] specifies the options. They are also called the "long fat pipe options," since a network with either a high bandwidth or a long delay is called a long fat pipe. Chapter 24 of TCPv1 contains more details on these options.

TCP 榪炴帴鐨勭粓姝?/h4>

TCP寤虹珛鏃墮渶瑕佷笁嬈¢氱煡錛岃岀粓姝竴涓猅CP榪炴帴鏃墮渶瑕佸洓嬈¢氱煡銆?br />One application calls close first, and we say that this end performs the active close. This end's TCP sends a FIN segment, which means it is finished sending data.

  1. The other end that receives the FIN performs the passive close. The received FIN is acknowledged by TCP. The receipt of the FIN is also passed to the application as an end-of-file (after any data that may have already been queued for the application to receive), since the receipt of the FIN means the application will not receive any additional data on the connection.

  2. Sometime later, the application that received the end-of-file will close its socket. This causes its TCP to send a FIN.

  3. The TCP on the system that receives this final FIN (the end that did the active close) acknowledges the FIN.

Since a FIN and an ACK are required in each direction, four segments are normally required. We use the qualifier "normally" because in some scenarios, the FIN in Step 1 is sent with data. Also, the segments in Steps 2 and 3 are both from the end performing the passive close and could be combined into one segment. We show these packets in Figure 2.3.

Figure 2.3. Packets exchanged when a TCP connection is closed.

graphics/02fig03.gif

A FIN occupies one byte of sequence number space just like a SYN. Therefore, the ACK of each FIN is the sequence number of the FIN plus one.

Between Steps 2 and 3 it is possible for data to flow from the end doing the passive close to the end doing the active close. This is called a half-close and we will talk about this in detail with the shutdown function in Section 6.6.

The sending of each FIN occurs when a socket is closed. We indicated that the application calls close for this to happen, but realize that when a Unix process terminates, either voluntarily (calling exit or having the main function return) or involuntarily (receiving a signal that terminates the process), all open descriptors are closed, which will also cause a FIN to be sent on any TCP connection that is still open.

Although we show the client in Figure 2.3 performing the active close, either end鈥攖he client or the server鈥攃an perform the active close. Often the client performs the active close, but with some protocols (notably HTTP/1.0), the server performs the active close.

TCP 鐘舵佽漿鎹㈠浘

鍏充簬TCP榪炴帴鐨勫緩绔嬪拰緇堟鎿嶄綔鍙互鐢變竴涓姸鎬佽漿鎹㈠浘鏉ヨ緇嗚鏄庯紝濡備笅鍥炬墍紺猴細
鍥劇ず錛歍CP 鐘舵佽漿鎹㈠浘
graphics/02fig04.gif

There are 11 different states defined for a connection and the rules of TCP dictate the transitions from one state to another, based on the current state and the segment received in that state. For example, if an application performs an active open in the CLOSED state, TCP sends a SYN and the new state is SYN_SENT. If TCP next receives a SYN with an ACK, it sends an ACK and the new state is ESTABLISHED. This final state is where most data transfer occurs.

The two arrows leading from the ESTABLISHED state deal with the termination of a connection. If an application calls close before receiving a FIN (an active close), the transition is to the FIN_WAIT_1 state. But if an application receives a FIN while in the ESTABLISHED state (a passive close), the transition is to the CLOSE_WAIT state.

We denote the normal client transitions with a darker solid line and the normal server transitions with a darker dashed line. We also note that there are two transitions that we have not talked about: a simultaneous open (when both ends send SYNs at about the same time and the SYNs cross in the network) and a simultaneous close (when both ends send FINs at the same time). Chapter 18 of TCPv1 contains examples and a discussion of both scenarios, which are possible but rare.

One reason for showing the state transition diagram is to show the 11 TCP states with their names. These states are displayed by netstat, which is a useful tool when debugging client/server applications. We will use netstat to monitor state changes in Chapter 5.

瑙傚療鍖咃紙Watching the Packets錛?/h4>

涓嬪湡鏄劇ず浜嗕竴涓猅CP榪炴帴瀹為檯鍙戠敓鐨勫寘浜ゆ崲鎯呭喌錛氳繛鎺ュ緩绔嬶紝鏁版嵁浼犺緭鍜岃繛鎺ョ粓姝€傚湪姣忎竴绔紶杈撶殑鏃跺欙紝涔熺粰鍑轟簡TCP鐘舵併偮?br />TCP 榪炴帴鐨勫寘浜ゆ崲

graphics/02fig05.gif

The client in this example announces an MSS of 536 (indicating that it implements only the minimum reassembly buffer size) and the server announces an MSS of 1,460 (typical for IPv4 on an Ethernet). It is okay for the MSS to be different in each direction (see Exercise 2.5).

Once a connection is established, the client forms a request and sends it to the server. We assume this request fits into a single TCP segment (i.e., less than 1,460 bytes given the server's announced MSS). The server processes the request and sends a reply, and we assume that the reply fits in a single segment (less than 536 in this example). We show both data segments as bolder arrows. Notice that the acknowledgment of the client's request is sent with the server's reply. This is called piggybacking and will normally happen when the time it takes the server to process the request and generate the reply is less than around 200 ms. If the server takes longer, say one second, we would see the acknowledgment followed later by the reply. (The dynamics of TCP data flow are covered in detail in Chapters 19 and 20 of TCPv1.)

We then show the four segments that terminate the connection. Notice that the end that performs the active close (the client in this scenario) enters the TIME_WAIT state. We will discuss this in the next section.

It is important to notice in Figure 2.5 that if the entire purpose of this connection was to send a one-segment request and receive a one-segment reply, there would be eight segments of overhead involved when using TCP. If UDP was used instead, only two packets would be exchanged: the request and the reply. But switching from TCP to UDP removes all the reliability that TCP provides to the application, pushing lots of these details from the transport layer (TCP) to the UDP application. Another important feature provided by TCP is congestion control, which must then be handled by the UDP application. Nevertheless, it is important to understand that many applications are built using UDP because the application exchanges small amounts of data and UDP avoids the overhead of TCP connection establishment and connection termination.

TIME_WAIT 鐘舵?/h3>

姣棤鐤戦棶錛屽叧浜庣綉緇滅紪紼嬩腑鏈璁╀漢璇В鐐逛箣涓灝辨槸 TIME_WAIT聽鐘舵併?鍦ㄤ竴绔皟鐢ㄤ簡close涔嬪悗錛岃绔淮鎸佽繖涓姸鎬佺殑鏃墮棿涓轟袱鍊嶆渶澶ф鐢熷瓨鏃墮棿錛?span class="docEmphasis">maximum segment lifetime (MSL)錛夈?/h3>

Every implementation of TCP must choose a value for the MSL. The recommended value in RFC 1122 [Braden 1989] is 2 minutes, although Berkeley-derived implementations have traditionally used a value of 30 seconds instead. This means the duration of the TIME_WAIT state is between 1 and 4 minutes. The MSL is the maximum amount of time that any given IP datagram can live in a network. We know this time is bounded because every datagram contains an 8-bit hop limit (the IPv4 TTL field in Figure A.1 and the IPv6 hop limit field in Figure A.2) with a maximum value of 255. Although this is a hop limit and not a true time limit, the assumption is made that a packet with the maximum hop limit of 255 cannot exist in a network for more than MSL seconds.

The way in which a packet gets "lost" in a network is usually the result of routing anomalies. A router crashes or a link between two routers goes down and it takes the routing protocols seconds or minutes to stabilize and find an alternate path. During that time period, routing loops can occur (router A sends packets to router B, and B sends them back to A) and packets can get caught in these loops. In the meantime, assuming the lost packet is a TCP segment, the sending TCP times out and retransmits the packet, and the retransmitted packet gets to the final destination by some alternate path. But sometime later (up to MSL seconds after the lost packet started on its journey), the routing loop is corrected and the packet that was lost in the loop is sent to the final destination. This original packet is called a lost duplicate or a wandering duplicate. TCP must handle these duplicates.

There are two reasons for the TIME_WAIT state:

  1. To implement TCP's full-duplex connection termination reliably

  2. To allow old duplicate segments to expire in the network

The first reason can be explained by looking at Figure 2.5 and assuming that the final ACK is lost. The server will resend its final FIN, so the client must maintain state information, allowing it to resend the final ACK. If it did not maintain this information, it would respond with an RST (a different type of TCP segment), which would be interpreted by the server as an error. If TCP is performing all the work necessary to terminate both directions of data flow cleanly for a connection (its full-duplex close), then it must correctly handle the loss of any of these four segments. This example also shows why the end that performs the active close is the end that remains in the TIME_WAIT state: because that end is the one that might have to retransmit the final ACK.

To understand the second reason for the TIME_WAIT state, assume we have a TCP connection between 12.106.32.254 port 1500 and 206.168.112.219 port 21. This connection is closed and then sometime later, we establish another connection between the same IP addresses and ports: 12.106.32.254 port 1500 and 206.168.112.219 port 21. This latter connection is called an incarnation of the previous connection since the IP addresses and ports are the same. TCP must prevent old duplicates from a connection from reappearing at some later time and being misinterpreted as belonging to a new incarnation of the same connection. To do this, TCP will not initiate a new incarnation of a connection that is currently in the TIME_WAIT state. Since the duration of the TIME_WAIT state is twice the MSL, this allows MSL seconds for a packet in one direction to be lost, and another MSL seconds for the reply to be lost. By enforcing this rule, we are guaranteed that when we successfully establish a TCP connection, all old duplicates from previous incarnations of the connection have expired in the network.

There is an exception to this rule. Berkeley-derived implementations will initiate a new incarnation of a connection that is currently in the TIME_WAIT state if the arriving SYN has a sequence number that is "greater than" the ending sequence number from the previous incarnation. Pages 958鈥?59 of TCPv2 talk about this in more detail. This requires the server to perform the active close, since the TIME_WAIT state must exist on the end that receives the next SYN. This capability is used by the rsh command. RFC 1185 [Jacobson, Braden, and Zhang 1990] talks about some pitfalls in doing this.

涓鑸綉緇滅▼搴忔墍浣跨敤鐨勫崗璁?/h3>

涓嬪浘鎬葷粨浜嗕竴浜涳細

Figure 2.19. Protocol usage of various common Internet applications.

graphics/02fig19.gif

The first two applications, ping and traceroute, are diagnostic applications that use ICMP. traceroute builds its own UDP packets to send and reads ICMP replies.

The three popular routing protocols demonstrate the variety of transport protocols used by routing protocols. OSPF uses IP directly, employing a raw socket, while RIP uses UDP and BGP uses TCP.

The next five are UDP-based applications, followed by seven TCP applications and four that use both UDP and TCP. The final five are IP telephony applications that use SCTP exclusively or optionally UDP, TCP, or SCTP.



]]>Unix緗戠粶緙栫▼絎笁鐗?鍗蜂竴:濂楁帴瀛楄仈緗慉PI瀛︿範絎旇(2)http://m.shnenglu.com/sscchh-2000/archive/2006/05/10/6860.html鍙蹭紶綰?/dc:creator>鍙蹭紶綰?/author>Wed, 10 May 2006 03:13:00 GMThttp://m.shnenglu.com/sscchh-2000/archive/2006/05/10/6860.htmlhttp://m.shnenglu.com/sscchh-2000/comments/6860.htmlhttp://m.shnenglu.com/sscchh-2000/archive/2006/05/10/6860.html#Feedback0http://m.shnenglu.com/sscchh-2000/comments/commentRss/6860.htmlhttp://m.shnenglu.com/sscchh-2000/services/trackbacks/6860.html璇ヨ妭涓昏灞曠ず涓涓畝鍗曠殑[鏃ユ湡鏃墮棿]鏈嶅姟鍣ㄧ殑紼嬪簭紺轟緥.
摟1.3 涓涓畝鍗曠殑鏃ユ湡鏃墮棿鏈嶅姟鍣ㄧ▼搴忎唬鐮?br />聽聽聽榪欎釜鏈嶅姟鍣ㄧ▼搴忓彲浠ヤ負涓婁竴鑺傜殑瀹㈡埛绔彁渚涙湇鍔°?br />

 1 #include     "unp.h"
2 #include <time.h>
3 int
4 main(int argc, char **argv)
5 {
6 int listenfd, connfd;
7 struct sockaddr_in servaddr;
8 char buff[MAXLINE];
9 time_t ticks;
10 listenfd = Socket(AF_INET, SOCK_STREAM, 0);
11 bzeros(&servaddr, sizeof(servaddr));
12 servaddr.sin_family = AF_INET;
13 servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
14 servaddr.sin_port = htons(13); /* daytime server */
15 Bind(listenfd, (SA *) &servaddr, sizeof(servaddr));
16 Listen(listenfd, LISTENQ);
17 for ( ; ; ) {
18 connfd = Accept(listenfd, (SA *) NULL, NULL);
19 ticks = time(NULL);
20 snprintf(buff, sizeof(buff), "%.24s\r\n", ctime(&ticks));
21 Write(connfd, buff, strlen(buff));
22 Close(connfd);
23 }
24 }

浜х敓涓涓猅CP濂楁帴瀛?/h4>

10 鍒涘緩涓涓猅CP濂楁帴瀛楋紝涓庡鎴風涓鏍楓?/h4>

緇戝畾鏈嶅姟鍣ㄧ殑騫夸負浜虹煡鐨勭鍙e埌璇ュ鎺ュ瓧

11鈥?5 鏈嶅姟鍣ㄩ氳繃濉厖緗戠粶濂楁帴瀛楃粨鏋勪綋涓殑绔彛鍩燂紝浠ュ強鏈嶅姟鍣ㄧ殑緗戠粶鎺ュ彛錛圛P鍦板潃錛夛紝鐒跺悗榪涜緇戝畾錛堣皟鐢╞ind錛夈傚湪榪欓噷鎸囧畾IP鍦板潃涓篒NADDR_ANY錛屼負浜嗚瀹㈡埛绔彲浠ヨ繛鎺ユ湇鍔″櫒鐨勪換涓緗戠粶鎺ュ彛錛堝洜涓烘湇鍔″櫒鍙兘鏈夊鍧楃綉鍗★紝涔熷氨瀵瑰簲浜嗗涓狪P鍦板潃錛夛紝涔熷氨鏄濡傛灉鏈嶅姟鍣ㄦ湁涓や釜IP鍦板潃錛屽鎴風榪炴帴浠諱竴IP鍦板潃鍗沖彲銆傚悗緇珷鑺備腑浠嬬粛浜嗗浣曢檺鍒跺鎴風榪炴帴鍒頒竴涓浐瀹氱殑鎺ュ彛涓娿?/p>

杞崲涓虹洃鍚鎺ュ瓧

16 閫氳繃璋冪敤listen錛屼竴涓鎺ュ瓧灝辮漿鎹負鐩戝惉濂楁帴瀛楋紝榪欏氨鏄璇ュ鎺ュ瓧璐熻矗鎺ユ敹鏉ヨ嚜瀹㈡埛绔殑榪炴帴璇鋒眰錛岃屽茍涓嶇湡姝d笌瀹㈡埛绔繘琛屼俊鎭紶杈撱?br />甯擱噺LISTENQ 鏄湪澶存枃浠?tt>unp.h涓畾涔夌殑錛屽畠鏄寚鑳藉鍚屾椂鐩戝惉瀹㈡埛绔繛鎺ョ殑涓暟銆備笉瓚呰繃LISTENQ鐨勫鎴風鍚屾椂榪炴帴鏈嶅姟鍣紝瀹冧滑浼氬湪涓涓槦鍒椾腑鎺掗槦錛屾潵絳夊緟鏈嶅姟鍣ㄧ殑澶勭悊銆傚悗緇珷鑺傛湁鏇磋緇嗙殑璁ㄨ銆?br />
鎺ユ敹瀹㈡埛绔繛鎺ワ紝鍙戦佸洖澶?/p>

17鈥?1 涓鑸湴錛屾湇鍔″櫒榪涚▼鍦ㄨ皟鐢╝ccept涔嬪悗榪涘叆鍒扮潯鐪犵姸鎬侊紝絳夊緟鐫瀹㈡埛绔湴榪炴帴璇鋒眰. 涓涓猅CP榪炴帴閫氳繃涓涓О涓轟笁鏂規彙鎵嬫潵寤虹珛錛屽綋涓夋柟鎻℃墜瀹屾垚涔嬪悗錛宎ccept璋冪敤榪斿洖銆傝繑鍥炲兼槸涓涓柊鐨勫鎺ュ瓧鎻忚堪絎︼紙涓涓暣鏁板糲onnfd錛夛紝榪欎釜鏂扮殑濂楁帴瀛楄礋璐d笌瀹㈡埛绔繘琛岄氳銆傚浜庢瘡涓涓鎴風鍦拌繛鎺ワ紝accept閮借繑鍥炰竴涓柊鐨勫鎺ュ瓧鎻忚堪絎︺傛暣鏈功浣跨敤鐨勬棤闄愬驚鐜鏍兼槸榪欐牱鐨勶細

				
for ( ; ; ) {
    . . .
}
				

褰撳墠鏃墮棿鍜屾棩鏈熼氳繃璋冪敤搴撳嚱鏁皌ime鏉ヨ幏寰楋紝騫朵笖閫氳繃璋冪敤ctime榪涜杞崲錛屼嬌寰楁垜浠兘澶熺洿瑙傜殑闃呰銆傚涓嬶細
Mon May 26 20:58:40 2003

緇堟榪炴帴

22 瀹㈡埛绔皟鐢╟lose涔嬪悗錛屾湇鍔″櫒鍏抽棴榪炴帴銆傝繖鏃跺欏紩璧蜂簡涓涓猅CP榪炴帴緇堟搴忓垪錛氫竴涓狥IN鍙戦佸埌姣忎竴绔紝鍚屾椂姣忎竴涓狥IN閮借琚彟涓绔‘璁ゃ傚湪鍚庨潰绔犺妭涓皢浼氬TCP榪炴帴寤虹珛鏃跺欑殑涓夋柟鎻℃墜浠ュ強TCP榪炴帴緇堟鏃跺欑殑鍥涘寘浜ゆ崲鏈夋洿璇︾粏鐨勮璁恒?br />聽聽聽浠ヤ笂緇欏嚭鐨勫鎴風鍜屾湇鍔″櫒鐗堟湰閮芥槸鍗忚鐩稿叧鐨勶紙IPv4錛夛紝鍦ㄥ悗闈㈠皢浼氱粰鍑轟竴涓崗璁棤鍏崇殑鐗堟湰錛圛Pv4鍜孖Pv6閮介傜敤錛屼富瑕侀氳繃浣跨敤getaddrinfo鍑芥暟錛夈?br />聽聽聽鏈鍚庨渶瑕佽ˉ鍏呯殑涓鐐規槸錛屽湪浠ヤ笂娑夊強鍒癝ocket API璋冪敤鐨勬椂鍊欙紝姣忎釜鍑芥暟鐨勭涓涓瓧姣嶅彉鎴愪簡澶у啓錛屽叾鎰忎箟鍜屽皬鍐欏紑澶寸殑鏄竴鏍風殑錛屽彧涓嶈繃澶氫簡涓涓敊璇鐞嗙艦浜嗐?/p>

]]>
Unix緗戠粶緙栫▼絎笁鐗?鍗蜂竴:濂楁帴瀛楄仈緗慉PI瀛︿範絎旇(1)http://m.shnenglu.com/sscchh-2000/archive/2006/05/09/6834.html鍙蹭紶綰?/dc:creator>鍙蹭紶綰?/author>Tue, 09 May 2006 12:31:00 GMThttp://m.shnenglu.com/sscchh-2000/archive/2006/05/09/6834.htmlhttp://m.shnenglu.com/sscchh-2000/comments/6834.htmlhttp://m.shnenglu.com/sscchh-2000/archive/2006/05/09/6834.html#Feedback0http://m.shnenglu.com/sscchh-2000/comments/commentRss/6834.htmlhttp://m.shnenglu.com/sscchh-2000/services/trackbacks/6834.htmlhttp://www.unpbook.com/
摟1.1 浠嬬粛

鍥?.1 聽緗戠粶紼嬪簭:聽瀹㈡埛绔拰鏈嶅姟鍣?/h5>

graphics/01fig01.gif

鍥韭?.2 鏈嶅姟鍣ㄥ悓鏃跺鐞嗗涓鎴風

graphics/01fig02.gif

鍥韭?.3 鍦ㄥ悓涓涓互澶綉浣跨敤TCP鍗忚閫氫俊鐨勫鎴風鍜屾湇鍔″櫒graphics/01fig03.gif
鍥韭?.4 騫垮煙緗戜笂鐨勫鎴風涓庢湇鍔″櫒錛堜緥濡俉eb嫻忚鍣ㄥ拰Web鏈嶅姟鍣級

graphics/01fig04.gif

摟1.2 浠g爜紺轟緥鍜岃В璇?/p>

1 #include聽 "unp.h"

2 int
3 main(int argc, char **argv)
4 {
5聽聽聽聽 int聽聽聽聽 sockfd, n;
6聽聽聽聽 char聽聽聽 recvline[MAXLINE + 1];
7聽聽聽聽 struct sockaddr_in servaddr;

8聽聽聽聽 if (argc != 2)
9聽聽聽聽聽聽聽聽 err_quit("usage: a.out <IPaddress>");

10聽聽聽聽 if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
11聽聽聽聽聽聽聽聽 err_sys("socket error");

12聽聽聽聽 bzero(&servaddr, sizeof(servaddr));
13聽聽聽聽 servaddr.sin_family = AF_INET;
14聽聽聽聽 servaddr.sin_port = htons(13);聽 /* daytime server */
15聽聽聽聽 if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0)
16聽聽聽聽聽聽聽聽 err_quit("inet_pton error for %s", argv[1]);

17聽聽聽聽 if (connect(sockfd, (SA *) &servaddr, sizeof(servaddr)) < 0)
18聽聽聽聽聽聽聽聽 err_sys("connect error");

19聽聽聽聽 while ( (n = read(sockfd, recvline, MAXLINE)) > 0) {
20聽聽聽聽聽聽聽聽 recvline[n] = 0;聽聽聽聽聽聽聽 /* null terminate */
21聽聽聽聽聽聽聽聽 if (fputs(recvline, stdout) == EOF)
22聽聽聽聽聽聽聽聽聽聽聽聽 err_sys("fputs error");
23聽聽聽聽 }
24聽聽聽聽 if (n < 0)
25聽聽聽聽聽聽聽聽 err_sys("read error");

26聽聽聽聽 exit(0);
27 }
鍏朵腑unp.h鏄嚜瀹氫箟鐨勫ご鏂囦歡錛?a class="" title="unp.h鏂囦歡鍐呭" href="/sscchh-2000/archive/2006/05/09/6836.html" target="_blank">鏌ョ湅婧愪唬鐮?/a>銆傛垜浠紪璇戝茍鎵ц浠ヤ笂浠g爜錛屽緱鍒頒互涓嬭緭鍑虹粨鏋滐細

solaris %a.out 206.168.112.96聽聽聽聽 our input

Mon May 26 20:58:40 2003聽聽聽聽聽聽聽聽聽聽the program's output

涓嬮潰綆瑕佸垎鏋愪互涓?7琛屼唬鐮?鍚庣畫绔犺妭涓湁鏇磋緇嗙殑璁ㄨ.

鍖呭惈鎴戜滑鑷繁鐨勫ご鏂囦歡

1 璇ュご鏂囦歡鍖呭惈浜嗗ぇ澶氭暟緗戠粶紼嬪簭鎵闇瑕佺殑澶氫釜澶存枃浠朵互鍙婂畾涔変簡鎴戜滑灝嗚浣跨敤鐨勪竴浜涘父閲?渚嬪 MAXLINE).

鍛戒護琛屽弬鏁?/font>

2鈥? 榪欐槸鍚湁鍛戒護琛屽弬鏁扮殑涓誨嚱鏁板畾涔?鍗砿ain鍑芥暟).鎴戜滑浠NSI C鏍囧噯鏉ヤ功鍐欎唬鐮?

鍒涘緩TCP濂楁帴瀛?a name="ch01lev3sec3">

10鈥?1 socket鍑芥暟璋冪敤鍒涘緩浜嗕竴涓綉緇滄祦濂楁帴瀛?(Internet (AF_INET) stream (SOCK_STREAM) socket), 璇ュ嚱鏁拌繑鍥炰竴涓暣鏁板?瀹冩弿榪頒簡璇ュ鎺ュ瓧,浠ュ悗鐨勫嚱鏁伴氳繃璇ユ暣鏁板兼潵浣跨敤榪欎釜濂楁帴瀛?渚嬪connect鍜宺ead絳夎皟鐢?. 鍏朵腑err_寮澶寸殑鍑芥暟鏄垜浠嚜瀹氫箟鐨勫嚱鏁?璇﹁榪欓噷.

紜畾鏈嶅姟鍣↖P鍦板潃鍜岀鍙e彿

12鈥?6 鎴戜滑濉厖浜嗙綉緇滃鎺ュ瓧鍦板潃緇撴瀯(涓涓悕涓簊ervaddr鐨勭粨鏋勪綋sockaddr_in),濉厖鐨勪俊鎭寘鎷湇鍔″櫒IP鍦板潃鍜岀鍙e彿.鎴戜滑鎶婃暣涓粨鏋勪綋棣栧厛娓呴浂,鐒跺悗璁劇疆鍦板潃鏃忎負AF_INET(IPV6璇ラ」涓篈F_INET6),绔彛鍙蜂負13(鏃墮棿鏈嶅姟鍣ㄧ殑绔彛鍙?鏄竴涓ぇ瀹墮兘鐭ラ亾鐨勭鍙e彿).IP鍦板潃鐢卞懡浠よ鍙傛暟鎸囧畾(argv[1]).IP鍦板潃鍜岀鍙e彿蹇呴』鎸夌収鎸囧畾鐨勬牸寮忔潵濉厖,鎴戜滑閫氳繃璋冪敤htons(涓繪満瀛楄妭嫻佸埌緗戠粶瀛楄妭嫻佺殑杞崲)鍜宨net_pton(鐐瑰垎鍗佽繘鍒跺埌32浣嶆暣鏁扮殑杞崲)涓や釜璋冪敤鏉ヨ繘琛岃漿鍖栧埌鎵闇瑕佺殑鏍煎紡.

鍦ㄨ皟鐢╥net_pton鐨勬椂鍊欏彲鑰岃兘浼氶亣鍒伴棶棰?鍥犱負榪欐槸IPv6鏂板鐨勫嚱鏁?浠ュ墠鐨処Pv4鐗堟湰鍙互璋冪敤inet_addr鏉ユ浛浠h鍑芥暟.

涓庢湇鍔″櫒寤虹珛涓涓繛鎺?/h4>

17鈥?8 TCP濂楁帴瀛楄皟鐢╟onnect鍑芥暟,灝變笌鏈嶅姟鍣?main鍑芥暟鐨勭浜屼釜鍙傛暟)寤虹珛浜嗕竴涓猅CP榪炴帴,鎴戜滑蹇呴』鎸囧畾濂楁帴瀛楃粨鏋勪綋鐨勭涓変釜鍙傛暟闀垮害,瀹冩繪槸璁╃紪璇戝櫒閫氳繃C鐨剆izeof榪愮畻絎︽潵璁$畻.

璇誨彇鍜屾樉紺烘湇鍔″櫒鐨勫洖澶?/h4>

19鈥?5 璋冪敤read鏉ヨ鍙栨湇鍔″櫒鐨勫洖澶?鍒╃敤鏍囧噯I/O鏉ユ樉紺鴻鍥炲淇℃伅.姝ゅ,鍦ㄤ嬌鐢═CP鐨勬椂鍊欐垜浠繀欏昏娉ㄦ剰,鍥犱負瀹冩槸涓涓病鏈夎竟鐣岀殑瀛楄妭嫻佸崗璁?鏈嶅姟鍣ㄧ殑鍥炲鏄竴涓?6瀛楄妭鐨勪覆:

Mon May 26 20 : 58 : 40 2003\r\n

\r 鏄洖杞? \n 鏄崲琛?

緇堟紼嬪簭
26 exit 緇堟紼嬪簭.Unix鍦ㄤ竴涓繘紼嬬粨鏉熸椂鍊欐繪槸鍏抽棴鎵鏈夋墦寮鐨勬弿榪扮,鍥犳鎴戜滑鐨凾CP濂楁帴瀛楁鏃跺叧闂簡.
鍚庣畫鍐呭灝嗗姝ゆ湁鏇存繁鍏ョ殑璁ㄨ.



]]>
国产日韩久久久精品影院首页| 精品国产VA久久久久久久冰 | 狠狠狠色丁香婷婷综合久久五月| 亚洲国产精品一区二区久久hs| 久久综合亚洲欧美成人| 国产亚洲精品自在久久| 国产精品内射久久久久欢欢| 四虎影视久久久免费观看| 波多野结衣AV无码久久一区| AV狠狠色丁香婷婷综合久久| 久久996热精品xxxx| 午夜不卡久久精品无码免费| 精品一区二区久久| 国产精品久久久久免费a∨| 久久亚洲中文字幕精品有坂深雪| 99久久精品九九亚洲精品| 狠狠色噜噜色狠狠狠综合久久| 色噜噜狠狠先锋影音久久| 久久无码AV一区二区三区| 久久er国产精品免费观看2| 久久综合亚洲色HEZYO国产| 久久er热视频在这里精品| 新狼窝色AV性久久久久久| 欧美精品丝袜久久久中文字幕| 国产精品久久一区二区三区 | 99精品久久久久久久婷婷| 久久天堂AV综合合色蜜桃网| 久久久久国产| 国产精品禁18久久久夂久 | 久久人人爽人人人人爽AV | 久久久久亚洲精品男人的天堂| 国内精品久久久久影院优| 久久亚洲日韩看片无码| 国产呻吟久久久久久久92| 狠狠色噜噜狠狠狠狠狠色综合久久 | 久久国产精品无码网站| 国产精品久久久99| 久久久九九有精品国产| 久久99热精品| 精品久久久久久久中文字幕| 国产综合精品久久亚洲|