LANServerDiscovery.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. // ----------------------------------------------------------------------
  11. // RakNet version 1.0
  12. // Filename ChatExample.cpp
  13. // Very basic chat engine example
  14. // ----------------------------------------------------------------------
  15. #include "MessageIdentifiers.h"
  16. #include "RakPeerInterface.h"
  17. #include "RakPeerInterface.h"
  18. #include "RakNetTypes.h"
  19. #include "GetTime.h"
  20. #include "BitStream.h"
  21. #include <assert.h>
  22. #include <cstdio>
  23. #include <cstring>
  24. #include <stdlib.h>
  25. #include "RakSleep.h"
  26. #include "Gets.h"
  27. int main(void)
  28. {
  29. // Pointers to the interfaces of our server and client.
  30. // Note we can easily have both in the same program
  31. RakNet::RakPeerInterface *client;
  32. RakNet::RakPeerInterface *server;
  33. bool b;
  34. char str[256];
  35. char serverPort[30], clientPort[30];
  36. RakNet::TimeMS quitTime;
  37. // Holds packets
  38. RakNet::Packet* p;
  39. printf("A client / server sample showing how clients can broadcast offline packets\n");
  40. printf("to find active servers.\n");
  41. printf("Difficulty: Beginner\n\n");
  42. printf("Instructions:\nRun one or more servers on the same port.\nRun a client and it will get pongs from those servers.\n");
  43. printf("Run as (s)erver or (c)lient?\n");
  44. Gets(str, sizeof(str));
  45. if (str[0]=='s' || str[0]=='S')
  46. {
  47. client=0;
  48. server=RakNet::RakPeerInterface::GetInstance();
  49. // A server
  50. printf("Enter the server port\n");
  51. Gets(serverPort,sizeof(serverPort));
  52. if (serverPort[0]==0)
  53. strcpy(serverPort, "60001");
  54. printf("Starting server.\n");
  55. // The server has to be started to respond to pings.
  56. RakNet::SocketDescriptor socketDescriptor(atoi(serverPort),0);
  57. socketDescriptor.socketFamily=AF_INET; // Only IPV4 supports broadcast on 255.255.255.255
  58. b = server->Startup(2, &socketDescriptor, 1)==RakNet::RAKNET_STARTED;
  59. server->SetMaximumIncomingConnections(2);
  60. if (b)
  61. printf("Server started, waiting for connections.\n");
  62. else
  63. {
  64. printf("Server failed to start. Terminating.\n");
  65. exit(1);
  66. }
  67. }
  68. else
  69. {
  70. client=RakNet::RakPeerInterface::GetInstance();
  71. server=0;
  72. // Get our input
  73. printf("Enter the client port to listen on, or 0\n");
  74. Gets(clientPort,sizeof(clientPort));
  75. if (clientPort[0]==0)
  76. strcpy(clientPort, "60000");
  77. printf("Enter the port to ping\n");
  78. Gets(serverPort,sizeof(serverPort));
  79. if (serverPort[0]==0)
  80. strcpy(serverPort, "60001");
  81. RakNet::SocketDescriptor socketDescriptor(atoi(clientPort),0);
  82. socketDescriptor.socketFamily=AF_INET; // Only IPV4 supports broadcast on 255.255.255.255
  83. client->Startup(1, &socketDescriptor, 1);
  84. // Connecting the client is very simple. 0 means we don't care about
  85. // a connectionValidationInteger, and false for low priority threads
  86. // All 255's mean broadcast
  87. client->Ping("255.255.255.255", atoi(serverPort), false);
  88. printf("Pinging\n");
  89. }
  90. printf("How many seconds to run this sample for?\n");
  91. Gets(str, sizeof(str));
  92. if (str[0]==0)
  93. {
  94. printf("Defaulting to 5 seconds\n");
  95. quitTime = RakNet::GetTimeMS() + 5000;
  96. }
  97. else
  98. quitTime = RakNet::GetTimeMS() + atoi(str) * 1000;
  99. // Loop for input
  100. while (RakNet::GetTimeMS() < quitTime)
  101. {
  102. if (server)
  103. p = server->Receive();
  104. else
  105. p = client->Receive();
  106. if (p==0)
  107. {
  108. RakSleep(30);
  109. continue;
  110. }
  111. if (server)
  112. server->DeallocatePacket(p);
  113. else
  114. {
  115. if (p->data[0]==ID_UNCONNECTED_PONG)
  116. {
  117. RakNet::TimeMS time;
  118. RakNet::BitStream bsIn(p->data,p->length,false);
  119. bsIn.IgnoreBytes(1);
  120. bsIn.Read(time);
  121. printf("Got pong from %s with time %i\n", p->systemAddress.ToString(), RakNet::GetTimeMS() - time);
  122. }
  123. else if (p->data[0]==ID_UNCONNECTED_PING)
  124. {
  125. printf("ID_UNCONNECTED_PING from %s\n",p->guid.ToString());
  126. }
  127. else if (p->data[0]==ID_UNCONNECTED_PING_OPEN_CONNECTIONS)
  128. {
  129. printf("ID_UNCONNECTED_PING_OPEN_CONNECTIONS from %s\n",p->guid.ToString());
  130. }
  131. client->DeallocatePacket(p);
  132. }
  133. RakSleep(30);
  134. }
  135. // We're done with the network
  136. if (server)
  137. RakNet::RakPeerInterface::DestroyInstance(server);
  138. if (client)
  139. RakNet::RakPeerInterface::DestroyInstance(client);
  140. return 0;
  141. }
粤ICP备19079148号