⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 advanced.html

📁 SDK FAQ集
💻 HTML
📖 第 1 页 / 共 2 页
字号:
the system's networking configuration.</p><p>The <a href="examples/getmac-rpc.html">second method</a> depends on aproperty of the Remote Procedure Call specification that says that thelast 6 bytes of a UUID will be derived from the hardware address of thelocal machine's network adapter. In the case of Ethernet (the commoncase), this is the MAC address. The major problem with this method isthat it will only return one address; if there is more than one Ethernetnetwork adapter in the system, this method won't let you get that secondadapter's address.</p><p>The descriptions introducing each example go into these issuesfurther.</p><p>The <a href="#snmp">SNMP</a> item above links to an article thatcontains another method for getting MAC addresses.</p><a name="maxsockets"></a><h5>4.9 - How many simultaneous sockets can I have open with Winsock?</h5><p>On Win9x machines, there's quite-low limit imposed by the kernel:100 connections. You can increase this limit by editing the registrykey HKLM\System\CurrentControlSet\Services\VxD\MSTCP\MaxConnections. OnWindows 95, the key is a DWORD; on Windows 98, it's a string. I've seensome reports of instability when this value is increased to more thana few times its default value.</p><p>The rest of this discussion will cover only Windows NT and Windows2000. These systems have much higher intrinsic capabilities, and thusallow you to use many more sockets. But, the Winsock specification doesnot set a particular limit, so the only sure way to tell is to try iton all the Winsock stacks you plan on supporting.</p><p>Beyond that vague advice, things get more complicated. The simplistictest is to just write a program that just opens sockets, to see wherethe program stops running: <a href="examples/get-sockets.html">[C++Example]</a>.</p><p>The above program isn't terribly realistic. I've seen it grab morethan 30,000 sockets before failing on Windows NT 4.0. Anecdotal evidencefrom the Winsock 2 mailing list puts the real limit much lower, typically4,000 to 16,000 sockets, even on NT systems with hundreds of megabytes ofphysical memory. The difference is that the example program just grabssocket handles, but does not actually create connections with them ortie up any network stack buffers.</p><p>According to people at Microsoft, the WinNT/Win2K kernel allocatessockets out of the non-paged memory pool. (That is, memory that cannotbe swapped to the page file by the virtual memory subsystem.) The sizeof this pool is necessarily fixed, and is dependent on the amount ofphysical memory in the system.</p><p>On Intel x86 machines, the non-paged memory pool stops growing at1/8 the size of physical RAM, with a hard maximum of 128 megabytes. Thehard limit is 256 megabytes on Windows 2000. Thus for NT 4, the size ofthe non-paged pool stops increasing once the machine has 1 GB of RAM. OnWin2K, you hit the wall at 2 GB.</p><p>The amount of data associated with each socket varies depending on howthat socket's used, but the minimum size is around 2 KB. OverlappedI/O buffers also eat into the non-paged pool, in blocks of 4 KB. (4KB is the x86's memory management unit's page size.) Thus a simplisticapplication that's regularly sending and receiving on a socket will tieup at least 10 KB of non-pageable memory.</p><p>Assuming that simple case of 10 KB of data per connection, thetheoretical maximum number of sockets on NT 4.0 is about 12,800s, andon Win2K 25,600.</p><p>I have seen reports of a 64 MB Windows NT 4.0 machine hitting the wallat 1,500 connections, a 128 MB machine at around 4,000 connections, anda 192 MB machine maxing out at 4,700 connections. It would appear thaton these machines, each connection is using between 4 KB and 6 KB. Thediscrepancy between these numbers and the 10 KB number above is probablydue to the fact that in these servers, not all connections were sendingand receiving all the time. The idle connections will only be using about2 KB each.</p><p>So, adjusting our "average" size down to 6 KB per socket,NT 4.0 could handle about 21,800 sockets and Win2K about 43,700sockets. The largest value I've seen reported is 16,000 socketson Windows NT 4.0.</p><p>There's one more complication to keep in mind: your server programwill not be the only thing running on the machine. If nothing else,there will be core OS services running. These other programs will becompeting with yours for space in the non-paged memory pool.</p><a name="fdsetsize"></a><h5>4.10 - Can I change FD_SETSIZE to make <tt>select()</tt> wait on more than 64 sockets?</h5><p>You can, but in practice it may not work. Several common Winsockstacks and Layered Service Providers limit themselves internally to thedefault value of FD_SETSIZE, 64. However, you can write a test programto try this on the systems you plan on supporting, to see if they are notlimited. Also, you can always send each group of 64 sockets to a differentthread, so that several calls to <code>select()</code> can occursimultaneously.</p><a name="forceif"></a><h5>4.11 - How do I make Winsock use a specific network interface?</h5><p>On a machine with multiple network interfaces (a modem for dialupInternet and a LAN card, for example), it can sometimes be useful toforce Winsock to use a specific interface. Before I go into how, keepin mind that the routing layer of the stack exists to handle this foryou. If your setup isn't working the way you want, maybe you just need tochange the routing tables. (This is done with the "route" and "netstat"command-line programs on Microsoft stacks.)</p><p>There are two common reasons why you might want to force Winsock touse a particular network interface. The first is when you only wantyour server program to handle incoming connections on a particularinterface. For example, if you have an NT machine set up as an Internetgateway, and it also runs a server that you only want internal LAN usersto be able to access, you will want to set it to only listen on theLAN interface. The other reason is that you have two or more possibleoutgoing routes, and you want your client program to connect using aparticular one without the routing layer getting in the way.</p><p>You can do both of these things with the <code>bind()</code>function. Using one of the "get my IP addresses" <ahref="examples/index.html">examples</a>, you can present your user witha list of possible addresses. Then they can pick the appropriateaddress to use, which your program will use in the <code>bind()</code>call. Obviously, this is only feasible for programs intended for advancedusers.</p><p>Incidentally, this is how virtual hosting on the Internet works. Asingle server is set up with a single network card but several IPaddresses. Windows NT/2000 can do this, but Win9x cannot. To set thisup in NT, go into the TCP/IP area of the Network control panel,and then click the Advanced button. IIRC, you can enter up to fivenetwork addresses per interface in NT Workstation, perhaps more in NTServer.</p><p>Note that this information does not apply to the Windows 95/98multihomed computer <a href="intermediate.html#dunbug">DUN bug</a>. Thisproblem cannot be fixed by <code>bind()</code>ing to the LAN interfacein an effort to force the OS to use it exclusively. The problem is dueto a bug in the OS's name resolver. See the DUN bug FAQ item forworkarounds.</p><a name="tcpbits"></a><h5>4.12 - What is the { SYN, ACK, FIN, RST } bit?</h5><p>See the FAQ article <a href="articles/debugging-tcp.html">DebuggingTCP</a>.</p><a name="clientbind"></a><h5>4.13 - Is it a bad idea to <tt>bind()</tt> to a particular port in a client program?</h5><p>It's occasionally justifiable, but most of the time it's a verybad idea.</p><p>I've only heard of two good uses of this feature. The first iswhen your program needs to bind to a port in a particular range. Someimplementations of the Berkeley "r commands" (e.g. rlogin, rsh, rcp,etc.) do this for security purposes. Because only the superuser ona Unix system can bind to a low-numbered port (1-1023), such an rcommand tries, sequentially, to bind to one of the ports in this rangeuntil it succeeds. This allows the remote server to surmise that if theconnection is coming from a low-numbered port, the remote user must bea superuser. (This port range limit also applies to Windows NT andWindows 2000, but <i>not</i> to Windows 9x.)</p><p>The second justifiable example is FTP in its "receive file" mode:it binds to a random port and then tells the file server to "send thefile to this port". This is justifiable because it arguably cleans up theprotocol, and the FTP client doesn't need to bind to any particular port,it just needs to bind to <i>a</i> port. (Incidentally, it does this bybinding to port 0 <img src="./bitmaps/waist-dot.gif" alt="-" width=7 height=6 hspace=2> the stack chooses an available port when youdo this.) This is also justifiable because the FTP client is acting as aserver in this case, so it makes sense that it has to bind to a port.</p><p>By contrast, it is almost always an error to bind to a<i>particular</i> port in a client. (Notice that both of the aboveexamples are flexible about the ports they bind to.) To see why thisis bad, consider a web browser. They often create several connectionsto download a single web page, one each to fetch all of the individualpieces of the page: images, applets, sound clips, etc. If they alwaysbound to a particular local port, they could only have one connectiongoing at a time. Also, you couldn't have a second instance of the webbrowser downloading another page at the same time.</p><p>That's not the biggest problem, though. When you close a TCPconnection, it goes into the <tt>TIME_WAIT</tt> state for a short period(between 30 and 120 seconds, typically), during which you cannot reusethat connection's "5-tuple:" the combination of {local host, local port,remote host, remote port, transport protocol}. (This timeout periodis a feature of all correctly-written TCP/IP stacks, and is coveredin <a href="../rfcs/official.html#rfc793">RFC 793</a> and especially<a href="../rfcs/official.html#rfc1122">RFC 1122</a>.) In practicalterms, this means that if you bind to a specific port all the time,you cannot connect to the same host using the same remote port untilthe <tt>TIME_WAIT</tt> period expires. I have personally seen anomalouscases where the <tt>TIME_WAIT</tt> period does not occur, but when thishappens, it's a bug in the stack, not something you should count on.</p><p>For more on this matter, see the <ahref="articles/lame-list.html#item18">Lame List</a>.</p>		</td>	</tr></table><!--  ---- Document Footer ----  --><hr noshade size=1 color=#404040><table cellpadding=5 cellspacing=0 border=0 width=95% align=center> 	<tr>		<td align=left>		    <a href="intermediate.html">&lt;&lt; Intermediate Winsock Issues</a>		</td>		<td align=right>		    <a href="resources/index.html">Winsock Resources &gt;&gt;</a>		</td>	</tr>	<tr>		<td align=left>			<i>Last modified on 29 April 2000 at 15:52 UTC-7</i>		</td>		<td align=right>			<font size=-1>Please send corrections to <a href="mailto:tangent@cyberport.com">tangent@cyberport.com</a>.</font>		</td>	</tr>	</table>	<table cellpadding=5 cellspacing=0 border=0 width=95% align=center> 	<tr>		<td align=left width=33%>			<font size=-1>				<a href="index.html"><b>&lt;</b> Go to the main FAQ page</a>			</font>		</td>		<td width=33%>			<font size=-1>			<center>				<a href="http://www.cyberport.com/~tangent/programming"><b>&lt;&lt;</b> Go to my Programming pages</a>			</center>			</font>		</td>		<td align=right width=33%>			<font size=-1>				<a href="http://www.cyberport.com/~tangent/"><b>&lt;&lt;&lt;</b> Go to my Home Page</a>			</font>		</td>	</tr>	</table>	</body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -