2017-02-19 18:25:55 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
#include "PassiveSocket.h" // Include header for active socket object definition
|
2017-02-19 18:25:55 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
#define MAX_PACKET 4096
|
2017-02-19 18:25:55 +00:00
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
CPassiveSocket socket;
|
|
|
|
CActiveSocket *pClient = NULL;
|
2017-02-19 18:25:55 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// Initialize our socket object
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
socket.Initialize();
|
2017-02-19 18:25:55 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
socket.Listen("localhost", 6667);
|
2017-02-19 18:25:55 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if ((pClient = socket.Accept()) != NULL)
|
|
|
|
{
|
2017-02-19 20:08:58 +00:00
|
|
|
int clientPort = socket.GetClientPort();
|
2018-09-23 21:17:31 +00:00
|
|
|
printf("connected from %s:%d\n", socket.GetClientAddr(), clientPort);
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Receive request from the client.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
while (1)
|
2017-02-20 17:34:00 +00:00
|
|
|
{
|
|
|
|
//printf("try receive\n");
|
|
|
|
bool receivedData = false;
|
2017-02-21 02:54:12 +00:00
|
|
|
int recBytes = 0;
|
|
|
|
recBytes = pClient->Receive(MAX_PACKET);
|
|
|
|
if (recBytes)
|
2017-02-20 17:34:00 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
char *msg = (char *)pClient->GetData();
|
|
|
|
msg[recBytes] = 0;
|
|
|
|
printf("received message [%s]\n", msg);
|
2017-02-20 17:34:00 +00:00
|
|
|
//------------------------------------------------------------------
|
|
|
|
// Send response to client and close connection to the client.
|
|
|
|
//------------------------------------------------------------------
|
2018-09-23 21:17:31 +00:00
|
|
|
pClient->Send(pClient->GetData(), pClient->GetBytesReceived());
|
2017-02-20 17:34:00 +00:00
|
|
|
receivedData = true;
|
2018-09-23 21:17:31 +00:00
|
|
|
if (strncmp(msg, "stop", 4) == 0)
|
2017-02-20 17:34:00 +00:00
|
|
|
{
|
|
|
|
printf("Stop request received\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!receivedData)
|
|
|
|
{
|
|
|
|
printf("Didn't receive data.\n");
|
|
|
|
break;
|
2018-09-23 21:17:31 +00:00
|
|
|
}
|
2017-02-20 17:34:00 +00:00
|
|
|
}
|
|
|
|
printf("Disconnecting client.\n");
|
|
|
|
pClient->Close();
|
2018-09-23 21:17:31 +00:00
|
|
|
delete pClient;
|
|
|
|
}
|
|
|
|
}
|
2017-02-19 18:25:55 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Receive request from the client.
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
socket.Close();
|
2017-02-19 18:25:55 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return 1;
|
2017-02-19 18:25:55 +00:00
|
|
|
}
|