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

📄 basic-server.html

📁 SDK FAQ集
💻 HTML
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN"><html lang="en"><head><title>Winsock Programmer's FAQ: Basic Blocking Server</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="../../examples/basics/basic-client.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>				Basic Winsock Examples: Blocking Server				</b></p>			</font>			</td>		<td align="right" bgcolor="#e0e0c0">			<font size="2" face=Verdana,Arial,Helvetica>				<b><a href="../../examples/dllping.html">&gt;&gt;</a></b>			</font>		</td>	</tr></table><!--  ---- Body Table ----  --><table width="95%" border="0" cellpadding="10">	<tr valign="top">		<td><p>This program is the direct counterpart of the <ahref="basic-client.html">basic client example</a>: it is the simplestpossible program of its kind. This program simply sets up a listeneron the network interface and port passed on the program's command line,accepts incoming connections, and echoes the remote peer's packets backto them.</p><p>The skeleton <code>main()</code> function in <a href="main.cpp">main.cpp</a>calls our <code>DoWinsock()</code> function, which contains the main loopof the program. The function starts off by setting up the listener. Thisbinds the program to a particular network interface and port and tellsWinsock to forward all connection requests to the program.</p><p>The program then enters an endless loop to wait for connections. Itaccepts each incoming connection and then bounces packets back to theclient until the client closes the connection. When that happens, theserver closes its side of the socket and then goes back to waiting forconnections.</p><p>Notice that you must pass an address on the command line tothis program, due to the way the common <code>main()</code> functionworks. For this program, you can safely use "0.0.0.0" all the time: whenyou <code>bind()</code> to the "any" address, Winsock sends incomingconnections from any network interface to your program.</p><h4>Interesting Behavior</h4><p>This server exhibits some interesting behavior that you may want toexperiment with.</p><ol><li>If your computer has more than one network interface (e.g., a modemfor connecting to the Internet and a network card for a LAN connection)you can pass a specific network address to the program on the commandline. This makes the program bind to only that address, so incomingconnection requests from the other network interface are ignored.<img src="../../bitmaps/dot-clear.gif" alt="" width=1 height=30 align=top> <br clear=all><li>This server will only handle a single connection at a time, butbecause of Winsock's "connection backlog" mechanism, multiple clientscan have connections pending at one time. Only the client that connectedfirst will get its packets echoed back. However, when that first clientdisconnects, the server will immediately complete the next waitingconnection.<img src="../../bitmaps/dot-clear.gif" alt="" width=1 height=30 align=top> <br clear=all><li>Try adjusting the second parameter to the server's<code>listen()</code> call to see how it affects multiple clientconnections.<img src="../../bitmaps/dot-clear.gif" alt="" width=1 height=30 align=top> <br clear=all><li>Try running the <code>netstat</code> command line program whilebasic-server is running to see its listener record. (Note that netstatmay only work correctly for this under Windows NT/2000.)<img src="../../bitmaps/dot-clear.gif" alt="" width=1 height=30 align=top> <br clear=all></ol><h4>Building the Program</h4><p>The only module you will need to compile this program,aside from the common files listed on the main examples page, is<a href="basic-server.cpp">basic-server.cpp</a>. The comment at the top of the file givescomplete compilation instructions; alternately, you can use the common<a href="Makefile">Makefile</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="../../examples/basics/basic-client.html">&lt;&lt; Basic Blocking Client</a>		</td>		<td align=right>		    <a href="../../examples/dllping.html">Ping: ICMP.DLL Method &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 + -