tutorialsample2.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Tutorial code sample 2</TITLE>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></HEAD>
  5. <meta name="title" content="RakNet - Advanced multiplayer game networking API">
  6. </HEAD>
  7. <BODY BGCOLOR="#ffffff" LINK="#003399" vlink="#003399" alink="#003399" LEFTMARGIN="0" TOPMARGIN="0" MARGINWIDTH="0" MARGINHEIGHT="0"">
  8. <img src="RakNetLogo.jpg" alt="Oculus VR, Inc."><BR><BR>
  9. <table width="100%" border="0"><tr><td bgcolor="#6699CC"><font color="#FFFFFF" size="3" face="Arial, Helvetica, sans-serif"><strong>
  10. <img src="spacer.gif" width="8" height="1">Tutorial code sample 2</strong></font></td></tr></table>
  11. <TABLE BORDER="0" CELLPADDING="10" CELLSPACING="0" WIDTH="100%"><TR><TD>
  12. <FONT FACE="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2" CLASS="G10" COLOR="#3366CC"><strong>Connecting, reading, and parsing network messages.
  13. </strong></FONT>
  14. <FONT FACE="Geneva, Verdana, Arial, Helvetica, sans-serif" size="2" CLASS="G10" COLOR="#666666">
  15. <BR><BR>
  16. The target of this exercise was to add the following features to sample 1:
  17. <OL>
  18. <LI>Create a main loop for the program.
  19. <LI>Call Receive and store the pointer returned in a pointer variable of type Packet. Note that we continually call Receive() until no further packets are returned. A common mistake is to only call it once.
  20. <LI>Include the header file to use struct Packet
  21. <LI>If a packet arrived, check the first byte of Packet::data, and process accordingly. The list of enumerations is in MessageIdentifiers.h.
  22. <LI>Print out the comment that goes along with the enumeration.
  23. <LI>Deallocate the packet pointer when done with it.
  24. </OL>
  25. New code over sample 1 is in bold.
  26. </FONT>
  27. <PRE><FONT FACE="Geneva, Verdana, Arial, Helvetica, sans-serif" size="1" CLASS="G10" COLOR="#111122">
  28. #include &lt;stdio.h&gt;
  29. <strong>#include &lt;string.h&gt;</strong>
  30. #include "RakPeerInterface.h"
  31. <strong>#include "MessageIdentifiers.h"</strong>
  32. #define MAX_CLIENTS 10
  33. #define SERVER_PORT 60000
  34. int main(void)
  35. {
  36. char str[512];
  37. RakNet::RakPeerInterface *peer = RakNet::RakPeerInterface::GetInstance();
  38. bool isServer;
  39. <strong>RakNet::Packet *packet;</strong>
  40. printf("(C) or (S)erver?\n");
  41. gets(str);
  42. if ((str[0]=='c')||(str[0]=='C'))
  43. {
  44. RakNet::SocketDescriptor sd;
  45. peer-&gt;Startup(1,&amp;sd, 1);
  46. isServer = false;
  47. } else {
  48. RakNet::SocketDescriptor sd(SERVER_PORT,0);
  49. peer-&gt;Startup(MAX_CLIENTS, &amp;sd, 1);
  50. isServer = true;
  51. }
  52. <strong>
  53. if (isServer)
  54. {
  55. printf("Starting the server.\n");
  56. // We need to let the server accept incoming connections from the clients
  57. peer-&gt;SetMaximumIncomingConnections(MAX_CLIENTS);
  58. } else {
  59. printf("Enter server IP or hit enter for 127.0.0.1\n");
  60. gets(str);
  61. if (str[0]==0){
  62. strcpy(str, "127.0.0.1");
  63. }
  64. printf("Starting the client.\n");
  65. peer-&gt;Connect(str, SERVER_PORT, 0,0);
  66. }
  67. while (1)
  68. {
  69. for (packet=peer-&gt;Receive(); packet; peer-&gt;DeallocatePacket(packet), packet=peer-&gt;Receive())
  70. {
  71. switch (packet-&gt;data[0])
  72. {
  73. case ID_REMOTE_DISCONNECTION_NOTIFICATION:
  74. printf("Another client has disconnected.\n");
  75. break;
  76. case ID_REMOTE_CONNECTION_LOST:
  77. printf("Another client has lost the connection.\n");
  78. break;
  79. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  80. printf("Another client has connected.\n");
  81. break;
  82. case ID_CONNECTION_REQUEST_ACCEPTED:
  83. printf("Our connection request has been accepted.\n");
  84. break;
  85. case ID_NEW_INCOMING_CONNECTION:
  86. printf("A connection is incoming.\n");
  87. break;
  88. case ID_NO_FREE_INCOMING_CONNECTIONS:
  89. printf("The server is full.\n");
  90. break;
  91. case ID_DISCONNECTION_NOTIFICATION:
  92. if (isServer){
  93. printf("A client has disconnected.\n");
  94. } else {
  95. printf("We have been disconnected.\n");
  96. }
  97. break;
  98. case ID_CONNECTION_LOST:
  99. if (isServer){
  100. printf("A client lost the connection.\n");
  101. } else {
  102. printf("Connection lost.\n");
  103. }
  104. break;
  105. default:
  106. printf("Message with identifier %i has arrived.\n", packet-&gt;data[0]);
  107. break;
  108. }
  109. }
  110. }
  111. </strong>
  112. RakNet::RakPeerInterface::DestroyInstance(peer);
  113. return 0;
  114. }
  115. </FONT></PRE>
  116. </TD></TR></TABLE>
  117. </BODY>
  118. </HTML>
粤ICP备19079148号