README 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. Fork from https://github.com/DFHack/clsocket to be used in Bullet Physics SDK
  2. Modifications: custom build system (CMake, premake etc), use preprocessor define _WIN32 instead of WIN32, __LINUX__, __APPLE__
  3. ------------------------------------------------------------------------------------------
  4. * History
  5. ------------------------------------------------------------------------------------------
  6. Written by Mark Carrier to provide a mechanism for writing cross platform socket code. This library was originally written to only support blocking TCP sockets. Over the years it has been extended to support UDP and RAW sockets as well. This is the first official release of the library and the following functionality is supported:
  7. * Cross platform socket support.
  8. o Windows 95, Windows 98, Windows XP
  9. o Linux, Unix
  10. o Macintosh OSX
  11. * Support for sychronious, and asychronious sockets
  12. * Supports TCP Streams
  13. * Supports UDP Datagrams
  14. * Supports Raw Sockets
  15. * Thread Safe
  16. * Signal Safe
  17. ------------------------------------------------------------------------------------------
  18. * SimpleSocket Class Overview
  19. ------------------------------------------------------------------------------------------
  20. Network communications via sockets can be abstracted into two categories of functionality; the active socket and the passive socket. The active socket object initiates a connection with a known host, whereas the passive socket object waits (or listens) for inbound requests for communication. The functionality of both objects is identical as far as sending and receiving data. This library makes distinction between the two objects because the operations for constructing and destructing the two are different.
  21. This library is different from other socket libraries which define TCP sockets, UDP sockets, HTTP sockets, etc. The reason is the operations required for TCP, UDP, and RAW network communication is identical from a logical stand point. Thus a program could initially be written employing TCP streams, and then at some future point it could be discovered that UDP datagrams would satisify the solution. Changing between the two transport protocols would only require changing how the object is instantiated. The remaining code would in theory require minimal to no changes.
  22. This library avoids abstractions like HTTP socket, or SMTP socket, soley because this type of object mixes the application and the transport layer. These types of abstractions can be created using this library as a base class.
  23. The simple socket library is comprised of two class which can be used to represent all socket communications.
  24. * Active Socket Class
  25. * Passive Socket Class
  26. ------------------------------------------------------------------------------------------
  27. * SimpleSocket Class Examples
  28. ------------------------------------------------------------------------------------------
  29. When operating on a socket object most methods will return true or false
  30. Simple Active Socket
  31. As mentioned previously the active socket (CActiveSocket) is used to initiate a connections with a server on some known port. So you want to connect to an existing server...
  32. How do you do it?
  33. There are many ways using the existing Berkley Socket API, but the goal of this class is to remove the many calls and man page lookups and replace them with clear, concise set of methods which allow a developer to focus on the logic of network programming.
  34. The following code will connect to a DAYTIME server on port 13, query for the current time, and close the socket.
  35. #include <string.h>
  36. #include "ActiveSocket.h" // Include header for active socket object definition
  37. int main(int argc, char **argv)
  38. {
  39. CActiveSocket socket; // Instantiate active socket object (defaults to TCP).
  40. char time[50];
  41. memset(&time, 0, 50);
  42. //--------------------------------------------------------------------------
  43. // Initialize our socket object
  44. //--------------------------------------------------------------------------
  45. socket.Initialize();
  46. //--------------------------------------------------------------------------
  47. // Create a connection to the time server so that data can be sent
  48. // and received.
  49. //--------------------------------------------------------------------------
  50. if (socket.Open("time-C.timefreq.bldrdoc.gov", 13))
  51. {
  52. //----------------------------------------------------------------------
  53. // Send a requtest the server requesting the current time.
  54. //----------------------------------------------------------------------
  55. if (socket.Send((const uint8 *)"\n", 1))
  56. {
  57. //----------------------------------------------------------------------
  58. // Receive response from the server.
  59. //----------------------------------------------------------------------
  60. socket.Receive(49);
  61. memcpy(&time, socket.GetData(), 49);
  62. printf("%s\n", time);
  63. //----------------------------------------------------------------------
  64. // Close the connection.
  65. //----------------------------------------------------------------------
  66. socket.Close();
  67. }
  68. }
  69. return 1;
  70. }
  71. You can see that the amount of code required to an object for network communciation is very small and simple.
  72. Simple Passive Socket
  73. Now you want to build a server.
  74. How do you do it?
  75. For a practical test lets build an echo server. The server will listen on port 6789 an repsond back with what ever has been sent to the server.
  76. #include "PassiveSocket.h" // Include header for active socket object definition
  77. #define MAX_PACKET 4096
  78. int main(int argc, char **argv)
  79. {
  80. CPassiveSocket socket;
  81. CActiveSocket *pClient = NULL;
  82. //--------------------------------------------------------------------------
  83. // Initialize our socket object
  84. //--------------------------------------------------------------------------
  85. socket.Initialize();
  86. socket.Listen("127.0.0.1", 6789);
  87. while (true)
  88. {
  89. if ((pClient = socket.Accept()) != NULL)
  90. {
  91. //----------------------------------------------------------------------
  92. // Receive request from the client.
  93. //----------------------------------------------------------------------
  94. if (pClient->Receive(MAX_PACKET))
  95. {
  96. //------------------------------------------------------------------
  97. // Send response to client and close connection to the client.
  98. //------------------------------------------------------------------
  99. pClient->Send( pClient->GetData(), pClient->GetBytesReceived() );
  100. pClient->Close();
  101. }
  102. delete pClient;
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. // Receive request from the client.
  107. //-----------------------------------------------------------------------------
  108. socket.Close();
  109. return 1;
  110. }