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

📄 intermediate.html

📁 SDK FAQ集
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><html lang="en"><head><title>Winsock Programmer's FAQ: Intermediate Winsock Issues</title><link rel="Stylesheet" type="text/css" href="./faq.css"></head><body bgcolor="#ffffee" text="#000000" link="#491e00" vlink="#7d2e01" alink="#da7417"><!--  ---- Header Bar ----  --><table border="0" width="95%" bgcolor="#006000" cellpadding="5" cellspacing="3" align="center">	<tr>		<td align="left" bgcolor="#e0e0c0">			<font size="2" face=Verdana,Arial,Helvetica>				<b><a href="newbie.html">&lt;&lt;</a></b>			</font>		</td>		<td align="center">			<font face=Verdana,Arial,Helvetica color="#ffffee">				<p align=center class=bigger3><b>				Winsock Programmer's FAQ<br>				Section 3: Intermediate Winsock Issues<br>				</b></p>			</font>			</td>		<td align="right" bgcolor="#e0e0c0">			<font size="2" face=Verdana,Arial,Helvetica>				<b><a href="advanced.html">&gt;&gt;</a></b>			</font>		</td>	</tr></table><!--  ---- Body Table ----  --><table width="95%" border="0" cellpadding="10">	<tr valign="top">		<td><a name="appproto"></a><h5>3.1 - How do I speak { HTTP, POP3, SMTP, FTP, Telnet, NNTP, etc. } with Winsock?</h5><p>Winsock proper does not provide a way for you to speak these protocols,because it only deals with the layers underneath these application-levelprotocols. However, there are many ways for you to get your program tospeak these protocols.</p><p>Perhaps the easiest method is to use a third-party library. The<a href=resources/libraries.html>Resources section</a> lists severalof these.</p><p>Another common method is to use the WinInet library exposed byMicrosoft's Internet Explorer. This offers easy access to the HTTP,FTP and Gopher protocols. Newer versions of Microsoft's developmenttools include classes and controls to access this mechanism.</p><p>Finally, you can always roll your own. You shouldstart by reading the specifications for the protocol youwant to implement. Most of the Internet's protocols aredocumented in <a href="glossary.html#rfc">RFCs</a>.  The <ahref="http://www.cyberport.com/~tangent/programming/rfcs/">Important RFCs</a> page links tothe most commonly referenced application-level RFCs. As far as standardsdocuments go, RFCs are generally clearly-written. Many of the standardprotocols are intrinsically simple: they don't require much work toimplement. Others may require more work to implement than you may beprepared for.</p><a name="getipaddr"></a><h5>3.2 - How do I get my IP address from within a Winsock program?</h5><p>There are three methods, which each have advantages and disadvantages:</p><ol><li>To get the local IP address if you already have a bound or connectedsocket, just call <code>getsockname()</code> on the socket.<li>To get your address without opening a socket first, do a<code>gethostbyname()</code> on the value <code>gethostname()</code>returns. This will return a <i>list</i> of all the host's interfaces,as shown in <a href=examples/ipaddr.html>this example</a>. (Seethe example page for problems with the method.)<li>The third method only works on Winsock2. The new <code>WSAIoctl()</code> API supports the<code>SIO_GET_INTERFACE_LIST</code> option, and one of the bits ofinformation returned is the addresses of each of the network interfacesin the system. <a href=examples/getifaces.html>[C++ Example]</a>(Again, see the example page for caveats.)</ol><a name="packetscheme"></a><h5>3.3 - What's the proper way to impose a packet scheme on a stream protocol like TCP?</h5><p>The two most common methods are delimiters and length-prefixing.</p><p>An example of delimiters is separating packets with, say, a caret(^). Naturally your delimiter must never occur in regular data, or youmust have some way of "escaping" delimiter characters.</p><p>An example of length-prefixing is prepending a two-byte integercontaining the packet length on every packet. See the FAQ article <ahref="articles/effective-tcp.html">How to Use TCP Effectively</a> for the proper way tosend integers over the network.</p><p>There are hybrid methods, too. The HTTP protocol, for example,separates header lines with CRLF pairs (a kind of delimiting), but whenan HTTP reply contains a block of binary data, the sever also sends the<i>Content-length</i> header before it sends the data, which is a kindof length-prefixing.</p><p>I favor simple length-prefixing, because as soon as you read the lengthprefix, you know how many more bytes to expect. By contrast, delimitersrequire that you blindly read until you find the end of the packet.</p><a name="firewall"></a><h5>3.4 - How do I write my program to work through a firewall?</h5><p>Most firewalls support the SOCKS protocol for this purpose, eitherversion 4 or version 5. SOCKS allows a client machine on a protectedinternal network to ask the firewall to act as a relay between it andan Internet host. SOCKS version 5 adds UDP support, as well as someadvanced features like end-to-end encryption and secure logins. It'soverkill for most applications, but some servers require that you usea SOCKS5-compatible login to get through.</p><p>You can find out more about SOCKS at the <ahref="http://www.socks.nec.com/">NEC SOCKS site</a> and at Stardust's<a href="http://www.stardust.com/events/socks98/">SOCKS Summit 98</a>site. You may also want to read the <ahref="../rfcs/useful.html#rfc1928">SOCKS5 RFC</a>. Note that a clarifiedversion of this RFC is due out soon; I'll update this Question as soonas I find out about it.</p><p>The NEC site has a good and free "SOCKSifier" called SOCKSCap thatturns almost any Winsock program into a SOCKS-compliant program, so youmay not need to modify your app. (There are other vendors that offerSOCKSifiers, and a few non-Microsoft Winsock stacks are SOCKSified. Checkyour stack's documentation to see if it supports SOCKS natively.) TheSOCKSifier method's downside is that your users must set up SOCKSCapand that they must run your program <i>through</i> the SOCKSifier. Thus,it's more user-friendly to implement the SOCKS protocol directly.</p><p>There is some BSD sockets client-side SOCKS code at the NEC sitethat you can easily modify to work with Winsock. This is included aspart of the Unix SOCKS server package as a library that the includedutility programs (rtelnet, rping, etc.) all link to.</p><p>Note that SOCKS inherently has trouble with some protocols becauseof the way they operate. Examples of this are RealAudio and almost any"multiuser" protocol, such as multiplayer online games and conferencingprograms. These programs require some kind of intelligent proxy thatunderstands the protocol. Unfortunately, these types of proxies are onlyavailable for well-established protocols like RealAudio.</p><a name="svrport"></a><h5>3.5 - I'm writing a server. What's a good network port to use?</h5><p>If you're writing a server for an existing, popular Internetprotocol, it's already got a port number assigned to it. You canfind the most popular of these numbers at the website for the <ahref="http://www.iana.org/">Internet Assigned Numbers Authority</a>(IANA).</p><p>If you're writing a server for a new protocol, there are a few rulesand suggestions you should obey when choosing your server's port:</p><ol><li>Ports 1-1023 are off-limits to people inventingnew protocols. They are reserved by the IANA for new "standard"protocols. Important protocols like POP3 and HTTP have low numbers(110 and 80, respectively), but your new K-RAD game server shouldn't. Notethat id Software is going to Hell for using port 666 with their DOOMnetwork server. They cleaned up their act with Quake, though.<li>Ports 1024 through 49151 are Registered Ports, which are a goodrange to choose your ports from. Just beware that the entire world ischoosing from ports in this range, so it may make sense for you to <ahref="http://www.isi.edu/cgi-bin/iana/port-number.pl">register</a>your port, or at least check the <ahref="http://www.isi.edu/in-notes/iana/assignments/port-numbers">currentlist</a> of assigned ports. Just be aware that no one is obligated tocheck that list before they make up <i>their</i> app's port number.<li>Ports 49152 through 65535 are Dynamic Ports, meaning that operatingsystems use ports in this range when choosing random ports. (TheFTP protocol, for example, uses random ports in the data transferphase.) This is a poor range to choose ports from, because there's afairly decent chance that your program and the OS will fight over agiven port eventually.<li>Even within the "safe" 1024-49151 range, there are many numbers notlisted as already taken in the IANA's assigned numbers list. Of these, youshould avoid port numbers with patterns to them, or a widely-recognizedmeaning. People tend to pick these since they're easy to remember, butthis increases the chances of a collision. Ports 1234, 5150 and 22222are bad choices, for example.</ol><p>You should also give some thought to making your program's portconfigurable, in case your program is run on a machine where anotherserver is already using that port. One way to do this is through Winsock's<code>getservbyname()</code> function: if that function returns a portnumber, use that, otherwise use the default port number. Then userscan change your program's port by editing the SERVICES file, located in%WINSYSDIR%\DRIVERS\ETC on Windows NT/2000 systems and c:\Windows\ onWin9x machines.</p><a name="bsdcompat"></a><h5>3.6 - I'm having trouble porting a BSD sockets program to Winsock. Help!</h5><p>There's an <a href="articles/bsd-compatibility.html">article</a> in the FAQthat covers this issue in some detail.</p><a name="broadcast"></a><h5>3.7 - How do I send a broadcast packet?</h5><p>With the UDP protocol (but not with TCP) you can send a packetso that all workstations on the network will see it. This can beuseful at times, but keep in mind that this creates a load on all themachines on the network, even on machines that aren't listening forthe packet. This is because the packet has to go down through severallayers of the network <a href="glossary.html#stack">stack</a>before it is rejected. As a result, most routers are configured to dropbroadcast packets. The practical upshot of that is that broadcastsare usually only effective for programs that only have to work on asingle LAN. To get around this problem, you may want to consider <ahref="http://www.sockets.com/ch16.htm#Multicast">multicasting</a>instead.</p><p>If you still want to use broadcasting, you must first use the<code>setsockopt()</code> function to enable the <code>SO_BROADCAST</code>flag. Next, you have to decide what kind of broadcast you want to use. Thesimplest type is a packet sent to IP address 255.255.255.255.</p><p>Simple broadcasts have a number of problems, however, so to makesure your broadcasts work properly, you should do a "directed broadcast"instead. A directed broadcast is targeted at a single LAN; some routerswill allow you to send a directed broadcast from one LAN to anotherbecause they usually cause less disruption than simple broadcasts. Also,some network stacks only respond to certain packets if they are directedbroadcasts. For example, a broadcast "ping" packet is usually ignoredunless it is directed at one of the LANs that the receiving host ison.</p><p>A directed broadcast uses a different kind of address. To constructthis address, you need to know the LAN's network address and thenetmask for that LAN. (You can construct the network address from theaddress of one of the hosts on the LAN and the netmask.) So, for network172.16.0.0 with netmask 255.255.0.0, the directed broadcast address is172.16.255.255. Mathematically, the directed broadcast address is thenetwork address bitwise OR'd with the one's complement of the netmask:</p><pre>

⌨️ 快捷键说明

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