??xml version="1.0" encoding="utf-8" standalone="yes"?>亚洲国产精久久久久久久,久久有码中文字幕,久久久久久久久66精品片http://www.shnenglu.com/sscchh-2000/与大家一起分享知?/description>zh-cnWed, 07 May 2025 15:01:07 GMTWed, 07 May 2025 15:01:07 GMT60关于旉的几个变?/title><link>http://www.shnenglu.com/sscchh-2000/archive/2006/12/29/16969.html</link><dc:creator>史传U?/dc:creator><author>史传U?/author><pubDate>Fri, 29 Dec 2006 03:07:00 GMT</pubDate><guid>http://www.shnenglu.com/sscchh-2000/archive/2006/12/29/16969.html</guid><wfw:comment>http://www.shnenglu.com/sscchh-2000/comments/16969.html</wfw:comment><comments>http://www.shnenglu.com/sscchh-2000/archive/2006/12/29/16969.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/sscchh-2000/comments/commentRss/16969.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/sscchh-2000/services/trackbacks/16969.html</trackback:ping><description><![CDATA[最q看C关于旉上面的几个变量:timeval,time_t,tm。请了解的详l说说它们的定义和相关的调用函数Q让大家一h学习Q呵c?img src ="http://www.shnenglu.com/sscchh-2000/aggbug/16969.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/sscchh-2000/" target="_blank">史传U?/a> 2006-12-29 11:07 <a href="http://www.shnenglu.com/sscchh-2000/archive/2006/12/29/16969.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Unix|络~程W三?卷一:套接字联|API学习W记(3)http://www.shnenglu.com/sscchh-2000/archive/2006/05/17/7292.html史传U?/dc:creator>史传U?/author>Wed, 17 May 2006 01:42:00 GMThttp://www.shnenglu.com/sscchh-2000/archive/2006/05/17/7292.htmlhttp://www.shnenglu.com/sscchh-2000/comments/7292.htmlhttp://www.shnenglu.com/sscchh-2000/archive/2006/05/17/7292.html#Feedback0http://www.shnenglu.com/sscchh-2000/comments/commentRss/7292.htmlhttp://www.shnenglu.com/sscchh-2000/services/trackbacks/7292.html
Z帮助我们理解conncetQacceptQcloseq几个函敎ͼ以及使用netstat工具来调试TCP应用E序Q我们必ȝ解TCPq接是如何徏立和l止的和TCP状态{换图?br />

三次握手

当一个TCPq接建立Ӟ发生了以下场景:

  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.

最需要三ơ包交换Q因此称作TCP的三ơ握手,如下图所C:
囄Q TCP 的三ơ握?/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 q接的终?/h4>

TCP建立旉要三ơ通知Q而终止一个TCPq接旉要四ơ通知?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—the client or the server—can 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 状态{换图

关于TCPq接的徏立和l止操作可以׃个状态{换图来详l说明,如下图所C:
囄QTCP 状态{换图
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 PacketsQ?/h4>

下土昄了一个TCPq接实际发生的包交换情况Q连接徏立,数据传输和连接终止。在每一端传输的时候,也给ZTCP状态。?br />TCP q接的包交换

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>

毫无疑问Q关于网l编E中最让h误解点之一是 TIME_WAIT 状态?在一端调用了close之后Q该端维持这个状态的旉Z倍最大段生存旉Q?span class="docEmphasis">maximum segment lifetime (MSL)Q?/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.

一般网l程序所使用的协?/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.



]]>母亲节快CQ写点东襉K给母亲http://www.shnenglu.com/sscchh-2000/archive/2006/05/12/7011.html史传U?/dc:creator>史传U?/author>Fri, 12 May 2006 06:10:00 GMThttp://www.shnenglu.com/sscchh-2000/archive/2006/05/12/7011.htmlhttp://www.shnenglu.com/sscchh-2000/comments/7011.htmlhttp://www.shnenglu.com/sscchh-2000/archive/2006/05/12/7011.html#Feedback20http://www.shnenglu.com/sscchh-2000/comments/commentRss/7011.htmlhttp://www.shnenglu.com/sscchh-2000/services/trackbacks/7011.html      2000q那q_我考上了吉林大学,母亲哭了。我x亲也是和我一P期待着q个梦想Q而真正实C的时候,我们都抑制不了那个喜悦的心情。我x亲一定感到很q福Q她会感到很骄傲的。可能有些h会想Q考了大学有什么了不v的,现在的大学生跟民工一P到处都是。也没有什么好骄傲的吧。可是,在这里我要告诉你Q我们那个村Q甚至那个乡Q在我的记忆中,出来这样一个大学生Q那已经是上个世U六七十q代的事情了Q也是在中间隔了近30q了。你可以惌那样的场景。母亲能不骄傲嘛Q其实母亲的教现在xQ不是逼着让我学习Q而是让我自由的发展,说v来也怪,时候我可是一个非常调皮的孩子Q整天和伙伴们玩的很疯。到处下x|上树掏鸟H。凡是能玩的都玩了。这可能跟母亲的a传n教有关系的。表姐也Nq我Q“小时候大安是一L的,甚至比我q用功,怎么我没有考上大学你去考上了呢Q”我也开玩笑的回{:其实我玩的时候,头脑q是在思考东西呢。其实表姐也是一个很优秀的,只是׃家庭的原因,实在太穷了,上完了初中就没有上学了。说到这里,其实在我们村里,或者Q何一个IL农村Q我们这些孩子不是笨Q而是真正׃安孩子多,又非常IP又要让每个孩子都能读点书Q认上几个字。所以大部分的h都只能读到初中甚臛_学没有毕业就辍学了。所以我觉得母亲难能可贵的,是她的坚持Q因为在我初三那q毕业的时候,׃考试成W不理惻I要读高中q需要交五千块钱的扩招费。那个时候真是非常艰隄一步。真可以用摔锅卖铁来形容。所以我很感谢母亲在那么困难的时候能够咬牙撑q来了。当然在q里q要感谢我的q爸q妈。没有他们的大力支持我也很难走到q一步?br />      9月䆾Q我d了生我养我的那个贫瘠的农村,临走的时候,我怀揣着八千块钱t往长春的列车上。八千块钱里面现在还清楚的记得:叔叔借的两千Q舅舅借的一千,表哥借的一千五Q还有姨哥借的一块和姨姐借的五百。剩下的两千是亲朋好友来和喜酒给的。从此开始了我在长春的求学之路。也是我来到长春之后的两个月左叻I父亲和母亲也M上v。目的就是打工给我挣来年的学贏V要知道亲戚安的生zMq不好,能支助我q么多钱M实在不易了。下一q的学费当然不可能,也不好意思张口了。先开始说说我的父母吧Q来C上vQ他们真的不Ҏ。在老乡的帮助下Q很快父母便开始了W一份工作。母亲在一个菜市场里面帮别人卖鸡鸭Q家等Q一个月500块钱。说hq个可能看上dƈ不怎么太辛苦。但是我要说q个工作我母亲咬牙坚持了两个月最l辞M。ؓ什么呢Q母亲每天早?点就起床Q然后到菜市场去Q这个时候有一车货Q由于菜市场是在二楼Q母Ԍq有另外一个阿姨,生活也一定不易,否则Ҏ不可能来q个地方Q他们俩׃个又一个满载家的子往二楼上面爬。这样一个多时候之后就开始给店主卖,然后把一只又一只的家禽宰杀Q然后去皮,z干净。就q样一D|_母亲整个的不成样子了Q手皮退了一层又一层。因为实在挺不住了,d院治疗。结果辛苦挣了一炚w几乎都用在了ȝ费上面了。再来说说父亲吧Q父亲的W一份工作是送货Q就是那个脚y三轮RQ每天送好几次Q每ơ有好几百斤Q有时候能辑ֈ上千斤。一个月也就六七癑֝钱。就q样也挺了几个月便辞了。我q有一个妹妹,父母q次外出打工Q不仅仅是ؓ了我一个h的求学。因为我妹妹也高一了,父母不仅仅要供我一个h?br />      来到了长春,一切都是那么新奇,看到了大学生zȝ的是太自׃。没有老师的管束。也许是我的理想Q也许是q来之易的求学机会,我仍然想在高中那P努力学习Q因为我的理x要让父母q上q福的生z,不再那么操劳。让乡村们都能够q上q福的生zR所以我必须努力学习Q来学习到真正的本领Q这h有希望。让我意外的是,W一q结束之后,我竟然获得了奖助学金?700块钱。还记得当时我简直不敢相信这是真的。这可是需要母亲日日夜夜拼命挣上一q啊Q我不知道该说什么好Q只能感谢学校对我的x。就q样Q或许是受到了激励,或许我真的很优秀。整个大学四q我一p得了奖助学金?6Q?00块钱Q其中奖学金12Q?00Q。再加上中国工商银行h14Q?00。我顺利的完成了大学四q的学业。除了学业之外。我q用一部分钱支助我妹妹M。另外由于爷爷奶奶年U大了,爸妈他们又不在n辏V我也给他们寄过几次生活贏V还记着最多一ơ是500Q那时候爷L病了Q需要住院。此外由于爸妈在外打工,大姑他们l我家种地。我也寄q几癑֝钱。同时也是减轻了爸妈他们的一点负担吧。整个大学生z过得很充实Q更让我q运的是Q遇C一个对我有知遇之恩的老师Q也是我现在的导师陈老师Q他Ҏ生活和学习上的帮助让我感Ȁ不尽Q而且我还遇到了一个通情辄Q艰苦朴素而又十分漂亮的女孩,也就是我的女朋友霖霖Q这几年来真的很感谢她的理解和支持?br />      不但我获得了q么多的q运。我的妹妹也是幸q的。由于我在学校和国家的支助下完成了学业,我爸妈辛苦挣的钱p够供我妹妹读书了。现在我妹妹已经快毕业了Q目前在实习中,虽然每月900块钱Q但也是相当不错了。而是现在q有一q就毕业了,׃比较q运Q读的是公费。每个月q能有八九百块钱的生z补助。从而能让父母他们不要那么操劳了?br />      可是Q就在前两天Q我爸爸跟我说我母亲又去介绍所׃100块钱找了一份工作。母亲的工作是给一家医院洗菜,LQ打杂什么的。一个月700块钱Q每天早上六点到晚上九点。我昨天晚上l母亲打了一个电话,{到快九点半才找到母亲的。我坚决要求母亲辞掉工作Q可是母亲就说闲着n也是|q不如忙点呢。一个小时还不到2块钱Q母亲还是那L乐观。我真的不知道怎么样劝才能劝她辞掉q䆾工作?br />      其实母亲的n体ƈ不好Q在我上高中的时候,母亲下地q活累出了肝炎,最后ȝd了,但是也不能太劳篏。而且母亲现在q有子宫肌瘤Q虽然是良性的Q但是也d血带来了n体的严重贫血。我婶婶跟我_“你妈有病你和你妹妹怎么不让Ҏ啊,别h有病都治Q她有病留着Q真是奇怪。”其实我妈也xQ不q却_“等你俩上班挣钱了再治,现在d院也查了Q说不要紧,而且手术费也得六七千Q根本也没有钱治。”,一个h的力量真的是那么的微弱啊。我只能盼望我早Ҏ业,Lq母亲ȝ。我在这里要_“妈Q我׃Q是你的坚强支撑了这个家庭,让我们感C您伟大的母爱Q”?br />      看到了我的故事,我希望每一个向我这LI乡娃都能够有一颗奋斗的心,来改变我们的生活。都能够q着自己的母Ԍ能够期待着明天好的生z?..

]]>
Unix|络~程W三?卷一:套接字联|API学习W记(2)http://www.shnenglu.com/sscchh-2000/archive/2006/05/10/6860.html史传U?/dc:creator>史传U?/author>Wed, 10 May 2006 03:13:00 GMThttp://www.shnenglu.com/sscchh-2000/archive/2006/05/10/6860.htmlhttp://www.shnenglu.com/sscchh-2000/comments/6860.htmlhttp://www.shnenglu.com/sscchh-2000/archive/2006/05/10/6860.html#Feedback0http://www.shnenglu.com/sscchh-2000/comments/commentRss/6860.htmlhttp://www.shnenglu.com/sscchh-2000/services/trackbacks/6860.html该节主要展示一个简单的[日期旉]服务器的E序CZ.
1.3 一个简单的日期旉服务器程序代?br />   q个服务器程序可以ؓ上一节的客户端提供服务?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 }

产生一个TCP套接?/h4>

10 创徏一个TCP套接字,与客L一栗?/h4>

l定服务器的qؓ人知的端口到该套接字

11?5 服务器通过填充|络套接字结构体中的端口域,以及服务器的|络接口QIP地址Q,然后q行l定Q调用bindQ。在q里指定IP地址为INADDR_ANYQؓ了让客户端可以连接服务器的Q一|络接口Q因为服务器可能有多块网卡,也就对应了多个IP地址Q,也就是说如果服务器有两个IP地址Q客Lq接MIP地址卛_。后l章节中介绍了如何限制客Lq接C个固定的接口上?/p>

转换为监听套接字

16 通过调用listenQ一个套接字p{换ؓ监听套接字,q就是说该套接字负责接收来自客户端的q接hQ而ƈ不真正与客户端进行信息传输?br />帔RLISTENQ 是在头文?tt>unp.h中定义的Q它是指能够同时监听客户端连接的个数。不过LISTENQ的客L同时q接服务器,它们会在一个队列中排队Q来{待服务器的处理。后l章节有更详l的讨论?br />
接收客户端连接,发送回?/p>

17?1 一般地Q服务器q程在调用accept之后q入到睡眠状态,{待着客户端地q接h. 一个TCPq接通过一个称ZҎ手来建立Q当三方握手完成之后Qaccept调用q回。返回值是一个新的套接字描述W(一个整数值connfdQ,q个新的套接字负责与客户端进行通讯。对于每一个客L地连接,accept都返回一个新的套接字描述W。整本书使用的无限@环风格是q样的:

				
for ( ; ; ) {
    . . .
}
				

当前旉和日期通过调用库函数time来获得,q且通过调用ctimeq行转换Q得我们能够直观的阅读。如下:
Mon May 26 20:58:40 2003

l止q接

22 客户端调用close之后Q服务器关闭q接。这时候引起了一个TCPq接l止序列Q一个FIN发送到每一端,同时每一个FIN都要被另一端确认。在后面章节中将会对TCPq接建立时候的三方握手以及TCPq接l止时候的四包交换有更详细的讨论?br />   以上l出的客L和服务器版本都是协议相关的(IPv4Q,在后面将会给Z个协议无关的版本QIPv4和IPv6都适用Q主要通过使用getaddrinfo函数Q?br />   最后需要补充的一ҎQ在以上涉及到Socket API调用的时候,每个函数的第一个字母变成了大写Q其意义和小写开头的是一LQ只不过多了一个错误处理Ş了?/p>

]]>
err_开头的自定义函?/title><link>http://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6838.html</link><dc:creator>史传U?/dc:creator><author>史传U?/author><pubDate>Tue, 09 May 2006 13:39:00 GMT</pubDate><guid>http://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6838.html</guid><wfw:comment>http://www.shnenglu.com/sscchh-2000/comments/6838.html</wfw:comment><comments>http://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6838.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/sscchh-2000/comments/commentRss/6838.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/sscchh-2000/services/trackbacks/6838.html</trackback:ping><description><![CDATA[ <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td valign="top"> <h3 class="docSection1Title" id="162666-841">D.3 Standard Error Functions</h3> <p class="docText">We define our own set of error functions that are used throughout the text to<br /> handle error conditions. The reason for using our own error functions is to <br />let us write our error handling with a single line of C code, as in</p> <pre> </pre> <pre>if <span id="pjdfjzh" class="docEmphasis">(error condition)</span> err_sys <span id="rrlvzxf" class="docEmphasis">(printf format with any number of arguments)</span>; </pre> <pre> </pre> <p class="docText">instead of</p> <pre> </pre> <pre>if <span id="hhlxppn" class="docEmphasis">(error condition)</span> { char buff [2002]; snprintf(buff, sizeof (buff), <span id="trnzbrz" class="docEmphasis">printf format <br />    with any number of arguments)</span>; perror(buff); exit (1); } </pre> <pre> </pre> <p class="docText">Our error functions use the variable-length argument list facility from <tt>ANSI C</tt>. <br />See Section 7.3 of [Kernighan and Ritchie 1988] for additional details.</p> <p class="docText"> <a class="docLink" href="mk:@MSITStore:E:\study_magizine\UNIX%20Network%20Programming%20Volume[1].1%20The%20Sockets%20Networking%20API.chm::/0131411551_app04lev1sec3.html#app04fig03"> <font color="#002c99">Figure D.3</font> </a>lists the differences between the various error functions. <br />If the global integer <tt>daemon_proc</tt> is nonzero, the message is passed to<br /><tt>syslog</tt> with the indicated level; otherwise, the error is output to standard error.<br />Figure D.3. Summary of our standard error functions.<br /><img id="132235130214" height="138" alt="graphics/xdfig03.gif" src="mk:@MSITStore:E:\study_magizine\UNIX%20Network%20Programming%20Volume[1].1%20The%20Sockets%20Networking%20API.chm::/FILES/xdfig03.gif" width="367" border="0" /></p> <p class="docText"> <a class="docLink" href="mk:@MSITStore:E:\study_magizine\UNIX%20Network%20Programming%20Volume[1].1%20The%20Sockets%20Networking%20API.chm::/0131411551_app04lev1sec3.html#app04fig04"> <font color="#002c99">Figure D.4</font> </a>shows the first five functions from <a class="docLink" href="mk:@MSITStore:E:\study_magizine\UNIX%20Network%20Programming%20Volume[1].1%20The%20Sockets%20Networking%20API.chm::/0131411551_app04lev1sec3.html#app04fig03"><font color="#002c99">Figure D.3</font></a>.</p> <h5 class="docExampleTitle"> <a name="app04fig04"> </a>Figure D.4 Our standard error functions.</h5> <p class="docText"> <span id="zrdxbrx" class="docEmphasis">lib/error.c</span> </p> <pre> 1 #include "unp.h" 2 #include <stdarg.h> /* ANSI C header file */ 3 #include <syslog.h> /* for syslog() */ 4 int daemon_proc; /* set nonzero by daemon_init() */ 5 static void err_doit(int, int, const char *, va_list); 6 /* Nonfatal error related to system call 7 * Print message and return */ 8 void 9 err_ret(const char *fmt, ...) 10 { 11 va_list ap; 12 va_start(ap, fmt); 13 err_doit(1, LOG_INFO, fmt, ap); 14 va_end(ap); 15 return; 16 } 17 /* Fatal error related to system call 18 * Print message and terminate */ 19 void 20 err_sys(const char *fmt, ...) 21 { 22 va_list ap; 23 va_start(ap, fmt); 24 err_doit(1, LOG_ERR, fmt, ap); 25 va_end(ap); 26 exit(1); 27 } 28 /* Fatal error related to system call 29 * Print message, dump core, and terminate */ 30 void 31 err_dump(const char *fmt, ...) 32 { 33 va_list ap; 34 va_start(ap, fmt); 35 err_doit(1, LOG_ERR, fmt, ap); 36 va_end(ap); 37 abort(); /* dump core and terminate */ 38 exit(1); /* shouldn't get here */ 39 } 40 /* Nonfatal error unrelated to system call 41 * Print message and return */ 42 void 43 err_msg(const char *fmt, ...) 44 { 45 va_list ap; 46 va_start(ap, fmt); 47 err_doit(0, LOG_INFO, fmt, ap); 48 va_end(ap); 49 return; 50 } 51 /* Fatal error unrelated to system call 52 * Print message and terminate */ 53 void 54 err_quit(const char *fmt, ...) 55 { 56 va_list ap; 57 va_start(ap, fmt); 58 err_doit(0, LOG_ERR, fmt, ap); 59 va_end(ap); 60 exit(1); 61 } 62 /* Print message and return to caller 63 * Caller specifies "errnoflag" and "level" */ 64 static void 65 err_doit(int errnoflag, int level, const char *fmt, va_list ap) 66 { 67 int errno_save, n; 68 char buf[MAXLINE + 1]; 69 errno_save = errno; /* value caller might want printed */ 70 #ifdef HAVE_VSNPRINTF 71 vsnprintf(buf, MAXLINE, fmt, ap); * safe */ 72 #else 73 vsprintf(buf, fmt, ap); /* not safe */ 74 #endif 75 n = strlen(buf); 76 if (errnoflag) 77 snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save)); 78 strcat(buf, "\n"); 79 if (daemon_proc) { 80 syslog(level, buf); 81 } else { 82 fflush(stdout); /* in case stdout and stderr are the same */ 83 fputs(buf, stderr); 84 fflush(stderr); 85 } 86 return; 87 } </pre> <ul> </ul> </td> </tr> </tbody> </table> <td> </td> <table cellspacing="0" cellpadding="0" width="100%" border="0"> <tbody> <tr> <td class="tt1"> <font color="#002c99"> </font> </td> <td class="tt1" valign="top" align="right"> </td> </tr> </tbody> </table> <img src ="http://www.shnenglu.com/sscchh-2000/aggbug/6838.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/sscchh-2000/" target="_blank">史传U?/a> 2006-05-09 21:39 <a href="http://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6838.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>unp.h文g内容http://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6836.html史传U?/dc:creator>史传U?/author>Tue, 09 May 2006 12:43:00 GMThttp://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6836.htmlhttp://www.shnenglu.com/sscchh-2000/comments/6836.htmlhttp://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6836.html#Feedback3http://www.shnenglu.com/sscchh-2000/comments/commentRss/6836.htmlhttp://www.shnenglu.com/sscchh-2000/services/trackbacks/6836.html 1 /* Our own header. Tabs are set for 4 spaces, not 8 */ 2 #ifndef __unp_h 3 #define __unp_h 4 #include "../config.h" /* configuration options for current OS */ 5 /* "../config.h" is generated by configure */ 6 /* If anything changes in the following list of #includes, must change 7 acsite.m4 also, for configure's tests. */ 8 #include <sys/types.h> /* basic system data types */ 9 #include <sys/socket.h> /* basic socket definitions */ 10 #include <sys/time.h> /* timeval{} for select() */ 11 #include <time.h> /* timespec{} for pselect() */ 12 #include <netinet/in.h> /* sockaddr_in{} and other Internet defns */ 13 #include <arpa/inet.h> /* inet(3) functions */ 14 #include <errno.h> 15 #include <fcntl.h> /* for nonblocking */ 16 #include <netdb.h> 17 #include <signal.h> 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <string.h> 21 #include <sys/stat.h> /* for S_xxx file mode constants */ 22 #include <sys/uio.h> /* for iovec{} and readv/writev */ 23 #include <unistd.h> 24 #include <sys/wait.h> 25 #include <sys/un.h> /* for Unix domain sockets */ 26 #ifdef HAVE_SYS_SELECT_H 27 # include <sys/select.h> /* for convenience */ 28 #endif 29 #ifdef HAVE_SYS_SYSCTL_H 30 # include <sys/sysctl.h> 31 #endif 32 #ifdef HAVE_POLL_H 33 # include <poll.h> /* for convenience */ 34 #endif 35 #ifdef HAVE_SYS_EVENT_H 36 # include <sys/event.h> /* for kqueue */ 37 #endif 38 #ifdef HAVE_STRINGS_H 39 # include <strings.h> /* for convenience */ 40 #endif 41 /* Three headers are normally needed for socket/file ioctl's: 42 * <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>. 43 */ 44 #ifdef HAVE_SYS_IOCTL_H 45 # include <sys/ioctl.h> 46 #endif 47 #ifdef HAVE_SYS_FILIO_H 48 # include <sys/filio.h> 49 #endif 50 #ifdef HAVE_SYS_SOCKIO_H 51 # include <sys/sockio.h> 52 #endif 53 #ifdef HAVE_PTHREAD_H 54 # include <pthread.h> 55 #endif 56 #ifdef HAVE_NET_IF_DL_H 57 # include <net/if_dl.h> 58 #endif 59 #ifdef HAVE_NETINET_SCTP_H 60 #include <netinet/sctp.h> 61 #endif 62 /* OSF/1 actually disables recv() and send() in <sys/socket.h> */ 63 #ifdef __osf__ 64 #undef recv 65 #undef send 66 #define recv(a,b,c,d) recvfrom(a,b,c,d,0,0) 67 #define send(a,b,c,d) sendto(a,b,c,d,0,0) 68 #endif 69 #ifndef INADDR_NONE 70 #define INADDR_NONE 0xffffffff /* should have been in <netinet/in.h> */ 71 #endif 72 #ifndef SHUT_RD /* these three POSIX names are new */ 73 #define SHUT_RD 0 /* shutdown for reading */ 74 #define SHUT_WR 1 /* shutdown for writing */ 75 #define SHUT_RDWR 2 /* shutdown for reading and writing */ 76 #endif 77 #ifndef INET_ADDRSTRLEN 78 #define INET_ADDRSTRLEN 16 /* "ddd.ddd.ddd.ddd\0" 79 1234567890123456 */ 80 #endif 81 /* Define following even if IPv6 not supported, so we can always allocate 82 an adequately sized buffer without #ifdefs in the code. */ 83 #ifndef INET6_ADDRSTRLEN 84 #define INET6_ADDRSTRLEN 46 /* max size of IPv6 address string: 85 "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx" or 86 "xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:ddd.ddd.ddd.ddd\0" 87 1234567890123456789012345678901234567890123456 */ 88 #endif 89 /* Define bzero() as a macro if it's not in standard C library. */ 90 #ifndef HAVE_BZERO 91 #define bzero(ptr,n) memset (ptr, 0, n) 92 #endif 93 /* Older resolvers do not have gethostbyname2() */ 94 #ifndef HAVE_GETHOSTBYNAME2 95 #define gethostbyname2(host,family) gethostbyname((host)) 96 #endif 97 /* The structure returned by recvfrom_flags() */ 98 struct unp_in_pktinfo { 99 struct in_addr ipi_addr; /* dst IPv4 address */ 100 int ipi_ifindex; /* received interface index */ 101 }; 102 /* We need the newer CMSG_LEN() and CMSG_SPACE() macros, but few 103 implementations support them today. These two macros really need 104 an ALIGN() macro, but each implementation does this differently. */ 105 #ifndef CMSG_LEN 106 #define CMSG_LEN(size) (sizeof(struct cmsghdr) + (size)) 107 #endif 108 #ifndef CMSG_SPACE 109 #define CMSG_SPACE(size) (sizeof(struct cmsghdr) + (size)) 110 #endif 111 /* POSIX requires the SUN_LEN() macro, but not all implementations define 112 it (yet). Note that this 4.4BSD macro works regardless whether there is 113 a length field or not. */ 114 #ifndef SUN_LEN 115 # define SUN_LEN (su) \ 116 (sizeof (*(su)) - sizeof ((su)->sun_path) + strlen((su)->sun_path)) 117 #endif 118 /* POSIX renames "Unix domain" as "local IPC." 119 Not all systems define AF_LOCAL and PF_LOCAL (yet). */ 120 #ifndef AF_LOCAL 121 #define AF_LOCAL AF_UNIX 122 #endif 123 #ifndef PF_LOCAL 124 #define PF_LOCAL PF_UNIX 125 #endif 126 /* POSIX requires that an #include of <poll.h> define INFTIM, but many 127 systems still define it in <sys/stropts.h>. We don't want to include 128 all the STREAMS stuff if it's not needed, so we just define INFTIM here. 129 This is the standard value, but there's no guarantee it is -1. */ 130 #ifndef INFTIM 131 #define INFTIM (-1) /* infinite poll timeout */ 132 #ifdef HAVE_POLL_H 133 #define INFTIM_UNPH /* tell unpxti.h we defined it */ 134 #endif 135 #endif 136 /* Following could be derived from SOMAXCONN in <sys/socket.h>, but many 137 kernels still #define it as 5, while actually supporting many more */ 138 #define LISTENQ 1024 /* 2nd argument to listen () */ 139 /* Miscellaneous constants */ 140 #define MAXLINE 4096 /* max text line length */ 141 #define BUFFSIZE 8192 /* buffer size for reads and writes */ 142 /* Define some port number that can be used for our examples */ 143 #define SERV_PORT 9877 /* TCP and UDP */ 144 #define SERV_PORT_STR "9877" /* TCP and UDP */ 145 #define UNIXSTR_PATH "/tmp/unix.str" /* Unix domain stream */ 146 #define UNIXDG_PATH "/tmp/unix.dg" /* Unix domain datagram */ 147 /* Following shortens all the typecasts of pointer arguments: */ 148 #define SA struct sockaddr 149 #define HAVE_STRUCT_SOCKADDR_STORAGE 150 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE 151 /* 152 * RFC 3493: protocol-independent placeholder for socket addresses 153 */ 154 #define __SS_MAXSIZE 128 155 #define __SS_ALIGNSIZE (sizeof(int64_t)) 156 #ifdef HAVE_SOCKADDR_SA_LEN 157 #define __SS_PAD1SIZE (__SS_ALIGNSIZE - sizeof(u_char) - sizeof(sa_family_t)) 158 #else 159 #define __SS_PAD1SIZE (__SS_ALIGNSIZE - sizeof(sa_family_t)) 160 #endif 161 #define __SS_PAD2SIZE (__SS_MAXSIZE - 2*__SS_ALIGNSIZE) 162 struct sockaddr_storage { 163 #ifdef HAVE_SOCKADDR_SA_LEN 164 u_char ss_len; 165 #endif 166 sa_family_t ss_family; 167 char __ss_pad1[__SS_PAD1SIZE]; 168 int64_t __ss_align; 169 char __ss_pad2[__SS_PAD2SIZE]; 170 }; 171 #endif 172 #define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) 173 /* default file access permissions for new files */ 174 #define DIR_MODE (FILE_MODE | S_IXUSR | S_IXGRP | S_IXOTH) 175 /* default permissions for new directories */ 176 typedef void Sigfunc (int); /* for signal handlers */ 177 #define min(a,b) ((a) < (b) ? (a) : (b)) 178 #define max(a,b) ((a) > (b) ? (a) : (b)) 179 #ifndef HAVE_ADDRINFO_STRUCT 180 # include "../lib/addrinfo.h" 181 #endif 182 #ifndef HAVE_IF_NAMEINDEX_STRUCT 183 struct if_nameindex { 184 unsigned int if_index; /* 1, 2, ... */ 185 char *if_name; /* null-terminated name: "le0", ... */ 186 }; 187 #endif 188 #ifndef HAVE_TIMESPEC_STRUCT 189 struct timespec { 190 time_t tv_sec; /* seconds */ 191 long tv_nsec; /* and nanoseconds */ 192 }; 193 #endif

]]>
Unix|络~程W三?卷一:套接字联|API学习W记(1)http://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6834.html史传U?/dc:creator>史传U?/author>Tue, 09 May 2006 12:31:00 GMThttp://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6834.htmlhttp://www.shnenglu.com/sscchh-2000/comments/6834.htmlhttp://www.shnenglu.com/sscchh-2000/archive/2006/05/09/6834.html#Feedback4http://www.shnenglu.com/sscchh-2000/comments/commentRss/6834.htmlhttp://www.shnenglu.com/sscchh-2000/services/trackbacks/6834.htmlhttp://www.unpbook.com/
1.1 介绍

?.1  |络E序: 客户端和服务?/h5>

graphics/01fig01.gif

图?.2 服务器同时处理多个客L

graphics/01fig02.gif

图?.3 在同一个以太网使用TCP协议通信的客L和服务器graphics/01fig03.gif
图?.4 q域|上的客L与服务器Q例如Web览器和Web服务器)

graphics/01fig04.gif

1.2 代码CZ和解?/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是自定义的头文gQ?a class="" title="unp.h文g内容" href="/sscchh-2000/archive/2006/05/09/6836.html" target="_blank">查看源代?/a>。我们编译ƈ执行以上代码Q得C下输出结果:

solaris %a.out 206.168.112.96     our input

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

下面要分析以?7行代?后箋章节中有更详l的讨论.

包含我们自己的头文g

1 该头文g包含了大多数|络E序所需要的多个头文件以及定义了我们要使用的一些常?例如 MAXLINE).

命o行参?/font>

2? q是含有命o行参数的d数定?即main函数).我们以ANSI C标准来书写代?

创徏TCP套接?a name="ch01lev3sec3">

10?1 socket函数调用创徏了一个网l流套接?(Internet (AF_INET) stream (SOCK_STREAM) socket), 该函数返回一个整数?它描qC该套接字,以后的函数通过该整数值来使用q个套接?例如connect和read{调?. 其中err_开头的函数是我们自定义的函?详见q里.

定服务器IP地址和端口号

12?6 我们填充了网l套接字地址l构(一个名为servaddr的结构体sockaddr_in),填充的信息包括服务器IP地址和端口号.我们把整个结构体首先清零,然后讄地址族ؓAF_INET(IPV6该项为AF_INET6),端口号ؓ13(旉服务器的端口?是一个大安知道的端口号).IP地址由命令行参数指定(argv[1]).IP地址和端口号必须按照指定的格式来填充,我们通过调用htons(L字节到|络字节的转换)和inet_pton(点分十进制到32位整数的转换)两个调用来进行{化到所需要的格式.

在调用inet_pton的时候可而能会遇到问?因ؓq是IPv6新增的函?以前的IPv4版本可以调用inet_addr来替代该函数.

与服务器建立一个连?/h4>

17?8 TCP套接字调用connect函数,׃服务?main函数的第二个参数)建立了一个TCPq接,我们必须指定套接字结构体的第三个参数长度,它L让编译器通过C的sizeofq算W来计算.

d和显C服务器的回?/h4>

19?5 调用read来读取服务器的回?利用标准I/O来显C回复信息.此外,在用TCP的时候我们必要注意,因ؓ它是一个没有边界的字节协?服务器的回复是一?6字节的串:

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

\r 是回? \n 是换?

l止E序
26 exit l止E序.Unix在一个进E结束时候L关闭所有打开的描q符,因此我们的TCP套接字此时关闭了.
后箋内容对此有更深入的讨论.



]]>
VC中视?View)通知对话?Dialog)的方?/title><link>http://www.shnenglu.com/sscchh-2000/archive/2006/04/28/6438.html</link><dc:creator>史传U?/dc:creator><author>史传U?/author><pubDate>Fri, 28 Apr 2006 13:47:00 GMT</pubDate><guid>http://www.shnenglu.com/sscchh-2000/archive/2006/04/28/6438.html</guid><wfw:comment>http://www.shnenglu.com/sscchh-2000/comments/6438.html</wfw:comment><comments>http://www.shnenglu.com/sscchh-2000/archive/2006/04/28/6438.html#Feedback</comments><slash:comments>2</slash:comments><wfw:commentRss>http://www.shnenglu.com/sscchh-2000/comments/commentRss/6438.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/sscchh-2000/services/trackbacks/6438.html</trackback:ping><description><![CDATA[     摘要: 在利? VC Q? MFC ~程的时候,我们往往能碰到这U情况:在一个模拟程序中Q如有些游戏Q,需要实时显CZ些属性的|文字或者图形)Q这个时候我们可以重新打开一个对话框Q可以是一个模态或者非模态的Q来昄q些信息Q本文以模态对话框ZQ简单实现这个技术。以下代码阴影部分是我利? ClassWizard 加的Q蓝色部分是我手工加的。其余是...  <a href='http://www.shnenglu.com/sscchh-2000/archive/2006/04/28/6438.html'>阅读全文</a><img src ="http://www.shnenglu.com/sscchh-2000/aggbug/6438.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/sscchh-2000/" target="_blank">史传U?/a> 2006-04-28 21:47 <a href="http://www.shnenglu.com/sscchh-2000/archive/2006/04/28/6438.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一个读取数据库的问?/title><link>http://www.shnenglu.com/sscchh-2000/archive/2006/04/25/6272.html</link><dc:creator>史传U?/dc:creator><author>史传U?/author><pubDate>Tue, 25 Apr 2006 13:11:00 GMT</pubDate><guid>http://www.shnenglu.com/sscchh-2000/archive/2006/04/25/6272.html</guid><wfw:comment>http://www.shnenglu.com/sscchh-2000/comments/6272.html</wfw:comment><comments>http://www.shnenglu.com/sscchh-2000/archive/2006/04/25/6272.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/sscchh-2000/comments/commentRss/6272.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/sscchh-2000/services/trackbacks/6272.html</trackback:ping><description><![CDATA[ <p>最q由于要发布交通仿真程?TSS)的Demo版,以前E序使用的是ZSQL2000数据库的数据源,现在需要改成基于Access数据库,但是在改成Access数据库之后,发现仿真的时候,前台客户端程序可以正常读取Access数据库,而后台服务器q程不能够正的d数据库。ؓ什么用SQL2000数据库的时候没有问题,而用Access数据库就会出现问题呢Q经q调试终于找Z问题Q当模拟开始的时候,前台此时正在保存该方案号的信息到数据库中Q而此时后台服务器E序也紧接着d该方案号信息Q此时我觉得可能是Access数据库在处理q发的时候出C问题。以致于服务器进E读取的Ҏ号信息不正确Q接下来的其它信息也׃正确了,q就造成了不能正常模拟。〔SQL数据库在处理q种情况Ӟ可能是有一个很好的机制保证了数据的正确性。?/p> <img src ="http://www.shnenglu.com/sscchh-2000/aggbug/6272.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/sscchh-2000/" target="_blank">史传U?/a> 2006-04-25 21:11 <a href="http://www.shnenglu.com/sscchh-2000/archive/2006/04/25/6272.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>VC中访问Access数据库的ҎQ不需要用户徏立ODBC数据源)http://www.shnenglu.com/sscchh-2000/archive/2006/04/13/ab.html史传U?/dc:creator>史传U?/author>Thu, 13 Apr 2006 05:01:00 GMThttp://www.shnenglu.com/sscchh-2000/archive/2006/04/13/ab.htmlhttp://www.shnenglu.com/sscchh-2000/comments/5457.htmlhttp://www.shnenglu.com/sscchh-2000/archive/2006/04/13/ab.html#Feedback9http://www.shnenglu.com/sscchh-2000/comments/commentRss/5457.htmlhttp://www.shnenglu.com/sscchh-2000/services/trackbacks/5457.html  阅读全文

]]>
ƷþӰԺ| þAvԴվ| þ99Ʒ| Ʒþþþû| þۺ϶| þþAVɫۺ| þþþƷ| ŷպþĻ| Ʒ99þò| 2020þþƷۺһ | Ʒþ| ŷһþþƷ޾Ʒþþþ | þ˽˹Ʒ| þþƷƷ| þþþþ޾Ʒ| ŮƷþþ| þþþþ޾Ʒ| þҹɫ˾Ʒ| þþþþۺһĻ| һþƵ| ƷþþĻ| þóۺɫۺ| պʮ˽һþ| 99þùۺϾƷԭ| 99þɫĻ| þþƷ˵| ŷպĻþ| þۺŷ| þþƷа| þøŮ߳MBA| þֻǾƷ23| ձŷƷһҳþ| þѹƵ| AAAþþþƷ| ݺɫݺݺݺݺɫۺϾþ| þպƷһ| þ¶ݺɫ| ɫۺϾþ֮ۺϾþ| þۺϾƷþ| 2021Ʒҹþ| ޾þһ|