1 //////////////////////////////////////////////
\r
6 #include "winsock2.h"
\r
10 using namespace Win32xx;
\r
13 class CServerSocket : public CSocket
\r
17 virtual ~CServerSocket() {}
\r
18 virtual void OnReceive()
\r
20 // This function is called automatically when there is data to receive
\r
21 char str[1024] = {0};
\r
22 int i = Receive(str, 1024, 0);
\r
23 cout << i <<" chars received: " << str << endl;
\r
29 // Create the main server socket.
\r
30 // It is used to listen for clients
\r
31 CServerSocket Server;
\r
32 if (!Server.Create(AF_INET, SOCK_STREAM))
\r
34 cout << "Failed to create socket\n" ;
\r
38 // Bind the IP address and port# to the main socket
\r
39 if (SOCKET_ERROR == Server.Bind("127.0.0.1", "3000"))
\r
41 cout << "Failed to bind IP address to socket\n" ;
\r
45 // Listen on the socket for clients to connect
\r
46 if (SOCKET_ERROR == Server.Listen())
\r
48 cout << "Listen on socket failed\n";
\r
52 // Create the socket to communicate with the Client
\r
53 CServerSocket Client;
\r
54 cout << "Waiting for the client to connect\n";
\r
57 Server.Accept(Client, NULL, NULL);
\r
59 while (SOCKET_ERROR == Client.GetSocket());
\r
61 cout << "Client connected\n";
\r
63 // Monitor the client socket for network events, such as data ready to receive
\r
64 Client.StartEvents();
\r
66 // Send data to the client
\r
67 cout << "Type data to send, type quit to exit\n";
\r
69 for (;;) // infinite loop
\r
72 if (s == "quit") break;
\r
73 int i = Client.Send(s.c_str(), (int)s.length(), 0);
\r
74 cout << "Sending " << i << " characters\n";
\r