connecting.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html><head><title>Connecting</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  4. <link href="RaknetManual.css" rel="stylesheet" type="text/css">
  5. <meta name="title" content="RakNet - Advanced multiplayer game networking API"></head>
  6. <body leftmargin="0" topmargin="0" style="background-color: rgb(255, 255, 255);" alink="#003399" link="#003399" marginheight="0" marginwidth="0" vlink="#003399">
  7. <img src="RakNet_Icon_Final-copy.jpg" alt="Oculus VR, Inc." width="150" height="150"><br>
  8. <br>
  9. <table border="0" width="100%">
  10. <tbody>
  11. <tr>
  12. <td bgcolor="#2c5d92"><font color="#ffffff" face="Arial, Helvetica, sans-serif" size="3"><span class="RakNetWhiteHeader"><img src="spacer.gif" height="1" width="8">Connecting
  13. to other systems</span></td>
  14. </tr>
  15. </tbody>
  16. </table>
  17. <table border="0" cellpadding="10" cellspacing="0" width="100%">
  18. <tbody>
  19. <tr>
  20. <td><span class="RakNetBlueHeader">Find
  21. who to connect to<br>
  22. </span><br>
  23. There are 5 ways to find other systems to connect to:<br>
  24. <ol>
  25. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Direct IP entry (you already know it)</li>
  26. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">LAN Broadcast</li>
  27. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Using the CloudServer/CloudClient plugins</li>
  28. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Using the Lobby server or RoomsPlugin</li>
  29. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Using MasterServer2</li>
  30. </ol>
  31. <span class="RakNetBlueHeader">Option 1: Direct IP Entry</span><br>
  32. <br>
  33. The simplest and easiest way from a coding perspective is to either
  34. hardcode an IP address or domain name, or present a GUI to the user
  35. asking them to enter the IP address of the other system they would like
  36. to connect to. Most of the samples use this method. Back when networked
  37. games first came out, this was the only option available.<br>
  38. <br>
  39. <span style="font-style: italic;">Advantages:<br>
  40. </span>
  41. <ul>
  42. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Little to no GUI work required of programmers and
  43. artists</li>
  44. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">If the IP address or domain name is fixed, such as if
  45. you are running a dedicated server, this is the correct solution</li>
  46. </ul>
  47. <span style="font-style: italic;"><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Disadvantages:<br>
  48. </span>
  49. <ul>
  50. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Inflexible</li>
  51. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Users will only be able to play with people they
  52. already know.</li>
  53. </ul>
  54. <p><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Note: Use
  55. 127.0.0.1, or localhost, to connect to another instance of RakPeer on
  56. the same computer, or the same application.</p>
  57. <span class="RakNetBlueHeader">Option 2: Lan Broadcast</span>
  58. <p> RakNet supports the ability to broadcast a packet to find other systems
  59. on a local LAN, with optional data to send and retrieve identifying the
  60. application. The sample <span style="font-weight: bold;">LANServerDiscovery</span>
  61. demonstrates this technique.<br>
  62. <br>
  63. In RakPeerInterface.h, the Ping function will can do this, as follows<br>
  64. <br>
  65. <span style="font-style: italic;"></span><small>rakPeer-&gt;Ping("255.255.255.255",
  66. REMOTE_GAME_PORT, onlyReplyOnAcceptingConnections);</small><br>
  67. <span style="font-weight: bold;"></span><br>
  68. REMOTE_GAME_PORT should be whatever port the other system is running
  69. the application you care about on. onlyReplyOnAcceptingConnections is a
  70. boolean indicating if the other system should reply, even if they have
  71. no connections available for you to connect to.<br>
  72. <br>
  73. Open systems will reply with ID_UNCONNECTED_PONG. From the sample:<br>
  74. <br>
  75. <span class="RakNetCode">if (p-&gt;data[0]==ID_UNCONNECTED_PONG)<br>
  76. {<br>
  77. &nbsp;&nbsp;&nbsp; RakNet::TimeMS time;<br>
  78. &nbsp;&nbsp;&nbsp; RakNet::BitStream bsIn(packet-&gt;data,packet-&gt;length,false);<br>
  79. &nbsp;&nbsp;&nbsp; bsIn.IgnoreBytes(1);<br>
  80. &nbsp;&nbsp;&nbsp; bsIn.Read(time);<br>
  81. &nbsp;&nbsp;&nbsp; printf("Got pong from %s with time
  82. %i\n", p-&gt;systemAddress.ToString(), RakNet::GetTime() - time);</span><br>
  83. }<br>
  84. <span style="font-weight: bold;"></span><br>
  85. In order to send custom user data, call
  86. RakPeer::SetOfflinePingResponse(customUserData, lengthInBytes); RakNet
  87. will copy the data passed to it and return this data appended to
  88. ID_UNCONNECTED_PONG.<br>
  89. <span style="font-style: italic;"><br>
  90. <font size="-1">Note: there is a hardcoded define
  91. MAX_OFFLINE_DATA_LENGTH in RakPeer.cpp limiting the length of your
  92. custom user data. Change this value and recompile if your data is
  93. larger than this define.<br>
  94. </span><br>
  95. <span style="font-style: italic;">Advantages:</span></p>
  96. <ul>
  97. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">You can join games automatically on program startup,
  98. no GUI or user interaction required</li>
  99. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Best way to find games on the LAN</li>
  100. </ul>
  101. <span style="font-style: italic;"><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Disadvantages:</span><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2"><br>
  102. <ul>
  103. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Won't work for the general internet</li>
  104. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Not as flexible as the <a href="lightweightdatabase.html">lightweight database plugin</a></li>
  105. </ul>
  106. <p><span class="RakNetBlueHeader">Option 3: Using the CloudServer/CloudClient plugins</span><br>
  107. <br>
  108. The <a href="cloudcomputing.html">CloudServer/CloudClient</a> plugin can act as a directory server without modification.</p>
  109. <p><span style="font-style: italic;">Advantages:</span></p>
  110. <ul>
  111. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Customizable</li>
  112. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">You can subscribe to update notifications when memory has been updated from another system</li>
  113. </ul>
  114. <span style="font-style: italic;"><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Disadvantages:</span><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2"><br>
  115. <ul>
  116. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Only scales to a few hundred users</li>
  117. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">You have to host your own server</li>
  118. </ul>
  119. <p><span class="RakNetBlueHeader">Option 4: Using the Lobby server or RoomsPlugin</span><br>
  120. <br>
  121. If you are not using a service that already provides a lobby, on the PC we provide database code that has similar functionality. The lobby server provides a database driven service for players to
  122. interact and start games. It provides features such as friends,
  123. matchmaking, emails, ranking, instant messenging, quick match, rooms,
  124. and room moderation.<br>
  125. <br>
  126. See the samples <span style="font-weight: bold;">Lobby2Server_PGSQL</span>
  127. and <span style="font-weight: bold;">Lobby2Client</span>
  128. for a demonstration of how to use this feature.<br>
  129. <br>
  130. <span style="font-style: italic;">Advantages: </span>
  131. </p>
  132. <ul>
  133. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">The most flexible solution for players to join games</li>
  134. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Allows users to interact before starting games</li>
  135. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Builds community</li>
  136. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Supports multiple titles</li>
  137. </ul>
  138. <span style="font-style: italic;"><span style="font-style: italic;">Disadvantages:<br>
  139. </span></span>
  140. <ul>
  141. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Requires a separate, dedicated server to host the
  142. plugin, and the server must have database support</li>
  143. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Feature is relatively large and complex compared to a
  144. simple game listing, requiring a good investment in time and programming</li>
  145. </ul>
  146. <p><span class="RakNetBlueHeader">Option 5: MasterServer2</span><br>
  147. <br>
  148. We host a master server for our customers. See <A HREF="http://masterserver2.raknet.com/">this page</A> for more details.
  149. <br>
  150. <br>
  151. <span style="font-style: italic;">Advantages:
  152. </span>
  153. <ul>
  154. <li>Fast</li>
  155. <li>Easy to use</li>
  156. <li>You do not have to host a server</li>
  157. </ul>
  158. <span style="font-style: italic;">Disadvantages:<br>
  159. </span>
  160. <ul>
  161. <li>Special licensing if you need the server sources</li>
  162. </ul>
  163. <span class="RakNetBlueHeader">Initiate
  164. connection attempt<span style="font-weight: normal;"><br>
  165. <br>
  166. </span></span>Once you know the IP
  167. address of the remote system(s) to connect to, use
  168. RakPeerInterface::Connect() to initiate an <span style="font-style: italic;">asynchronous</span>
  169. connection attempt. The parameters to connect are:<br>
  170. <br>
  171. <span class="RakNetCode">ConnectionAttemptResult Connect( const char* host, unsigned short remotePort, const char *passwordData, int passwordDataLength, PublicKey *publicKey=0, unsigned connectionSocketIndex=0, unsigned sendConnectionAttemptCount=6, unsigned timeBetweenSendConnectionAttemptsMS=1000, RakNet::TimeMS timeoutTime=0 )</span><br>
  172. <ul>
  173. <li>host is an IP address, or domain name.</li>
  174. <li>remotePort is the port that the remote system is
  175. listening on, which you passed to the Startup() function</li>
  176. <li>passwordData is optional binary data to send with the
  177. connection request. If this data does not match what was passed to
  178. RakPeerInterface::SetPassword(), the remote system will reply with
  179. ID_INVALID_PASSWORD.</li>
  180. <li>passwordDataLength is the length, in bytes, of
  181. passwordData</li>
  182. <li>publicKey is the public key parameter that was passed to InitializeSecurity() on the remote system. If you don't use security, pass 0.</li>
  183. <li>connectionSocketIndex is the index into the array of socket descriptors passed to socketDescriptors in RakPeer::Startup() to determine the one to send on.</li>
  184. <li>sendConnectionAttemptCount is how many datagrams to send before giving up as unable to connect. This is also used for MTU detection, with 3 different MTU sizes. So the default of 12 means send each MTU size 4 times, which should be sufficient to tolerate any reasonable packetloss. Lower values mean that ID_CONNECTION_ATTEMPT_FAILED would be returned to you faster.</li>
  185. <li>timeBetweenSendConnectionAttemptsMS is how many milliseconds to wait before sending another connection attempt. A good value is 4 times the expected ping.</li>
  186. <li>timeoutTime is how many milliseconds to wait, for this particular connection, before dropping the remote system if a message cannot be delivered. The default value of 0 means use the global value from SetTimeoutTime().</li>
  187. </ul>
  188. <p>Connect() will return CONNECTION_ATTEMPT_STARTED for a successful attempt, something else on failure.<br>
  189. <br>
  190. <span style="font-style: italic;">Note: Connect()
  191. returning true does NOT mean you are connected. If successful the message ID_CONNECTION_REQUEST_ACCEPTED should be received. If not you will recieve one of the error messages.</span></p>
  192. <p class="RakNetBlueHeader">Connectivity messages returned as the first byte of the Packet::data structure</p>
  193. <p><strong>Connection closed</strong>: ID_DISCONNECTION_NOTIFICATION, ID_CONNECTION_LOST<br>
  194. <strong>New connection</strong>:
  195. ID_NEW_INCOMING_CONNECTION, ID_CONNECTION_REQUEST_ACCEPTED<br>
  196. <strong>Connection attempt failed</strong>:
  197. ID_CONNECTION_ATTEMPT_FAILED, ID_REMOTE_SYSTEM_REQUIRES_PUBLIC_KEY, ID_OUR_SYSTEM_REQUIRES_SECURITY, ID_PUBLIC_KEY_MISMATCH, ID_ALREADY_CONNECTED, ID_NO_FREE_INCOMING_CONNECTIONS, ID_CONNECTION_BANNED, ID_INVALID_PASSWORD, ID_INCOMPATIBLE_PROTOCOL_VERSION, ID_IP_RECENTLY_CONNECTED</p>
  198. <p class="RakNetBlueHeader">Troubleshooting ID_CONNECTION_ATTEMPT_FAILED</p>
  199. <p> ID_CONNECTION_ATTEMPT_FAILED is
  200. the generic message meaning no communication was established with the remote system. Possible
  201. reasons include:</p>
  202. <ul>
  203. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">The IP address is wrong</li>
  204. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">That system is not running RakNet, or
  205. RakPeerInterface::Startup() was not called on that system</li>
  206. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">The remote system has started RakNet, but
  207. RakPeerInterface::SetMaximumIncomingConnections() was not called</li>
  208. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">A firewall on either system is blocking UDP packets
  209. on the port you have chosen</li>
  210. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">A router on the remote system is blocking incoming
  211. UDP packets on the port you have chosen. See the <a href="natpunchthrough.html">NAT Punchthrough</a>
  212. plugin to resolve this.</li>
  213. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">On Windows Vista,&nbsp;the network driver
  214. security service pack sometimes breaks UDP, not just for RakNet, but in
  215. general, even for DirectPlay. This service pack should be rolled back,
  216. and not installed.</li>
  217. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2"><a href="secureconnections.html">Secure
  218. connections</a> are enabled, and your system failed the security
  219. check.</li>
  220. <li><font class="G10" color="#666666" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2">Your IP address was banned with
  221. RakPeerInterface::AddToBanList(). Note that some plugins, such as the <a href="connectionFilter.html">connection filter</a>,
  222. have the option to ban IP addresses automatically.</li>
  223. </ul>
  224. <p> Assuming you are able to connect, it
  225. is time to go onto the section:
  226. <a href="creatingpackets.html">Creating
  227. Packets</a><br>
  228. </p>
  229. </td>
  230. </tr>
  231. </tbody>
  232. </table>
  233. <table border="0" width="100%">
  234. <tbody>
  235. <tr>
  236. <td bgcolor="#2c5d92" class="RakNetWhiteHeader"><font color="#ffffff" face="Arial, Helvetica, sans-serif" size="3"><strong>See
  237. Also</td>
  238. </tr>
  239. </tbody>
  240. </table>
  241. <table border="0" cellpadding="10" cellspacing="0" width="100%">
  242. <tbody>
  243. <tr>
  244. <td><font class="G10" color="#111122" face="Geneva, Verdana, Arial, Helvetica, sans-serif" size="1">
  245. <a href="index.html">Index</a><br>
  246. <a href="connectionFilter.html">Connection Filter</a><br>
  247. <a href="creatingpackets.html">Creating Packets</a><br>
  248. <a href="lightweightdatabase.html">Lightweight
  249. Database plugin</a><br>
  250. <a href="natpunchthrough.html"><span style="text-decoration: underline;">NAT Punchthrough plugin</span></a><br>
  251. <a href="secureconnections.html">Secure connections</a><br>
  252. <a href="faq.html">FAQ</a><br>
  253. </td>
  254. </tr>
  255. </tbody>
  256. </table>
  257. </body></html>
粤ICP备19079148号