--- /dev/null
+//////////////////////////////////////////////\r
+// Server.cpp\r
+\r
+#include <iostream>\r
+#include <string>\r
+#include "winsock2.h"\r
+#include "socket.h"\r
+\r
+using namespace std;\r
+using namespace Win32xx;\r
+\r
+\r
+class CServerSocket : public CSocket\r
+{\r
+public:\r
+ CServerSocket() {}\r
+ virtual ~CServerSocket() {}\r
+ virtual void OnReceive()\r
+ {\r
+ // This function is called automatically when there is data to receive\r
+ char str[1024] = {0};\r
+ int i = Receive(str, 1024, 0);\r
+ cout << i <<" chars received: " << str << endl;\r
+ }\r
+};\r
+\r
+int main()\r
+{\r
+ // Create the main server socket.\r
+ // It is used to listen for clients\r
+ CServerSocket Server;\r
+ if (!Server.Create(AF_INET, SOCK_STREAM))\r
+ {\r
+ cout << "Failed to create socket\n" ;\r
+ return 0;\r
+ }\r
+\r
+ // Bind the IP address and port# to the main socket\r
+ if (SOCKET_ERROR == Server.Bind("127.0.0.1", "3000"))\r
+ {\r
+ cout << "Failed to bind IP address to socket\n" ;\r
+ return 0;\r
+ }\r
+\r
+ // Listen on the socket for clients to connect\r
+ if (SOCKET_ERROR == Server.Listen())\r
+ {\r
+ cout << "Listen on socket failed\n";\r
+ return 0;\r
+ }\r
+\r
+ // Create the socket to communicate with the Client\r
+ CServerSocket Client;\r
+ cout << "Waiting for the client to connect\n";\r
+ do\r
+ {\r
+ Server.Accept(Client, NULL, NULL);\r
+ }\r
+ while (SOCKET_ERROR == Client.GetSocket());\r
+\r
+ cout << "Client connected\n";\r
+\r
+ // Monitor the client socket for network events, such as data ready to receive\r
+ Client.StartEvents();\r
+\r
+ // Send data to the client\r
+ cout << "Type data to send, type quit to exit\n";\r
+ string s;\r
+ for (;;) // infinite loop\r
+ {\r
+ getline(cin, s);\r
+ if (s == "quit") break;\r
+ int i = Client.Send(s.c_str(), (int)s.length(), 0);\r
+ cout << "Sending " << i << " characters\n";\r
+ }\r
+\r
+ return 0;\r
+}\r
+\r