Code-style consistency improvement:
Apply clang-format-all.sh using the _clang-format file through all the cpp/.h files. make sure not to apply it to certain serialization structures, since some parser expects the * as part of the name, instead of type. This commit contains no other changes aside from adding and applying clang-format-all.sh
This commit is contained in:
@@ -1,30 +1,30 @@
|
||||
|
||||
#include "PassiveSocket.h" // Include header for active socket object definition
|
||||
#include "PassiveSocket.h" // Include header for active socket object definition
|
||||
|
||||
#define MAX_PACKET 4096
|
||||
#define MAX_PACKET 4096
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
CPassiveSocket socket;
|
||||
CActiveSocket *pClient = NULL;
|
||||
CPassiveSocket socket;
|
||||
CActiveSocket *pClient = NULL;
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Initialize our socket object
|
||||
//--------------------------------------------------------------------------
|
||||
socket.Initialize();
|
||||
//--------------------------------------------------------------------------
|
||||
// Initialize our socket object
|
||||
//--------------------------------------------------------------------------
|
||||
socket.Initialize();
|
||||
|
||||
socket.Listen("localhost", 6667);
|
||||
socket.Listen("localhost", 6667);
|
||||
|
||||
while (true)
|
||||
{
|
||||
if ((pClient = socket.Accept()) != NULL)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if ((pClient = socket.Accept()) != NULL)
|
||||
{
|
||||
int clientPort = socket.GetClientPort();
|
||||
printf("connected from %s:%d\n", socket.GetClientAddr(),clientPort);
|
||||
//----------------------------------------------------------------------
|
||||
// Receive request from the client.
|
||||
//----------------------------------------------------------------------
|
||||
while (1)
|
||||
printf("connected from %s:%d\n", socket.GetClientAddr(), clientPort);
|
||||
//----------------------------------------------------------------------
|
||||
// Receive request from the client.
|
||||
//----------------------------------------------------------------------
|
||||
while (1)
|
||||
{
|
||||
//printf("try receive\n");
|
||||
bool receivedData = false;
|
||||
@@ -32,15 +32,15 @@ int main(int argc, char **argv)
|
||||
recBytes = pClient->Receive(MAX_PACKET);
|
||||
if (recBytes)
|
||||
{
|
||||
char* msg = (char*) pClient->GetData();
|
||||
msg[recBytes]=0;
|
||||
printf("received message [%s]\n",msg);
|
||||
char *msg = (char *)pClient->GetData();
|
||||
msg[recBytes] = 0;
|
||||
printf("received message [%s]\n", msg);
|
||||
//------------------------------------------------------------------
|
||||
// Send response to client and close connection to the client.
|
||||
//------------------------------------------------------------------
|
||||
pClient->Send( pClient->GetData(), pClient->GetBytesReceived() );
|
||||
pClient->Send(pClient->GetData(), pClient->GetBytesReceived());
|
||||
receivedData = true;
|
||||
if (strncmp(msg,"stop",4)==0)
|
||||
if (strncmp(msg, "stop", 4) == 0)
|
||||
{
|
||||
printf("Stop request received\n");
|
||||
break;
|
||||
@@ -50,18 +50,18 @@ int main(int argc, char **argv)
|
||||
{
|
||||
printf("Didn't receive data.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("Disconnecting client.\n");
|
||||
pClient->Close();
|
||||
delete pClient;
|
||||
}
|
||||
}
|
||||
delete pClient;
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Receive request from the client.
|
||||
//-----------------------------------------------------------------------------
|
||||
socket.Close();
|
||||
//-----------------------------------------------------------------------------
|
||||
// Receive request from the client.
|
||||
//-----------------------------------------------------------------------------
|
||||
socket.Close();
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -1,58 +1,57 @@
|
||||
|
||||
#include <string.h>
|
||||
#include "ActiveSocket.h" // Include header for active socket object definition
|
||||
#include "ActiveSocket.h" // Include header for active socket object definition
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
CActiveSocket socket; // Instantiate active socket object (defaults to TCP).
|
||||
char time[50];
|
||||
CActiveSocket socket; // Instantiate active socket object (defaults to TCP).
|
||||
char time[50];
|
||||
|
||||
memset(&time, 0, 50);
|
||||
memset(&time, 0, 50);
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Initialize our socket object
|
||||
//--------------------------------------------------------------------------
|
||||
socket.Initialize();
|
||||
//--------------------------------------------------------------------------
|
||||
// Initialize our socket object
|
||||
//--------------------------------------------------------------------------
|
||||
socket.Initialize();
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
// Create a connection to the time server so that data can be sent
|
||||
// and received.
|
||||
//--------------------------------------------------------------------------
|
||||
// if (socket.Open("time-C.timefreq.bldrdoc.gov", 13))
|
||||
if (socket.Open("192.168.86.196", 6667))
|
||||
{
|
||||
for (int i=0;i<100;i++)
|
||||
{
|
||||
//----------------------------------------------------------------------
|
||||
// Send a requtest the server requesting the current time.
|
||||
//----------------------------------------------------------------------
|
||||
char data[1024];
|
||||
sprintf(data,"%s %d","Hello",i);
|
||||
int len = strlen(data);
|
||||
data[len]=0;
|
||||
printf("Sending [%s]\n",data);
|
||||
len++;
|
||||
if (socket.Send((const uint8 *)data, len))
|
||||
{
|
||||
//----------------------------------------------------------------------
|
||||
// Receive response from the server.
|
||||
//----------------------------------------------------------------------
|
||||
int rec = socket.Receive(len);
|
||||
if (rec)
|
||||
{
|
||||
uint8* data = socket.GetData();
|
||||
memcpy(&time, data, len);
|
||||
printf("Received: [%s]\n", time);
|
||||
}
|
||||
}
|
||||
}
|
||||
//--------------------------------------------------------------------------
|
||||
// Create a connection to the time server so that data can be sent
|
||||
// and received.
|
||||
//--------------------------------------------------------------------------
|
||||
// if (socket.Open("time-C.timefreq.bldrdoc.gov", 13))
|
||||
if (socket.Open("192.168.86.196", 6667))
|
||||
{
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
//----------------------------------------------------------------------
|
||||
// Send a requtest the server requesting the current time.
|
||||
//----------------------------------------------------------------------
|
||||
char data[1024];
|
||||
sprintf(data, "%s %d", "Hello", i);
|
||||
int len = strlen(data);
|
||||
data[len] = 0;
|
||||
printf("Sending [%s]\n", data);
|
||||
len++;
|
||||
if (socket.Send((const uint8 *)data, len))
|
||||
{
|
||||
//----------------------------------------------------------------------
|
||||
// Receive response from the server.
|
||||
//----------------------------------------------------------------------
|
||||
int rec = socket.Receive(len);
|
||||
if (rec)
|
||||
{
|
||||
uint8 *data = socket.GetData();
|
||||
memcpy(&time, data, len);
|
||||
printf("Received: [%s]\n", time);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------
|
||||
// Close the connection.
|
||||
//----------------------------------------------------------------------
|
||||
socket.Close();
|
||||
}
|
||||
//----------------------------------------------------------------------
|
||||
// Close the connection.
|
||||
//----------------------------------------------------------------------
|
||||
socket.Close();
|
||||
}
|
||||
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -4,98 +4,97 @@
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
|
||||
// usually defined with #include <unistd.h>
|
||||
static void sleep( unsigned int seconds )
|
||||
{
|
||||
Sleep( seconds * 1000 );
|
||||
}
|
||||
// usually defined with #include <unistd.h>
|
||||
static void sleep(unsigned int seconds)
|
||||
{
|
||||
Sleep(seconds * 1000);
|
||||
}
|
||||
#endif
|
||||
|
||||
#define MAX_PACKET 4096
|
||||
#define MAX_PACKET 4096
|
||||
#define TEST_PACKET "Test Packet"
|
||||
|
||||
struct thread_data
|
||||
{
|
||||
const char *pszServerAddr;
|
||||
short int nPort;
|
||||
int nNumBytesToReceive;
|
||||
int nTotalPayloadSize;
|
||||
const char *pszServerAddr;
|
||||
short int nPort;
|
||||
int nNumBytesToReceive;
|
||||
int nTotalPayloadSize;
|
||||
};
|
||||
|
||||
|
||||
void *CreateTCPEchoServer(void *param)
|
||||
{
|
||||
CPassiveSocket socket;
|
||||
CActiveSocket *pClient = NULL;
|
||||
struct thread_data *pData = (struct thread_data *)param;
|
||||
int nBytesReceived = 0;
|
||||
CPassiveSocket socket;
|
||||
CActiveSocket *pClient = NULL;
|
||||
struct thread_data *pData = (struct thread_data *)param;
|
||||
int nBytesReceived = 0;
|
||||
|
||||
socket.Initialize();
|
||||
socket.Listen(pData->pszServerAddr, pData->nPort);
|
||||
socket.Initialize();
|
||||
socket.Listen(pData->pszServerAddr, pData->nPort);
|
||||
|
||||
if ((pClient = socket.Accept()) != NULL)
|
||||
{
|
||||
while (nBytesReceived != pData->nTotalPayloadSize)
|
||||
{
|
||||
if (nBytesReceived += pClient->Receive(pData->nNumBytesToReceive))
|
||||
{
|
||||
pClient->Send((const uint8 *)pClient->GetData(), pClient->GetBytesReceived());
|
||||
}
|
||||
}
|
||||
if ((pClient = socket.Accept()) != NULL)
|
||||
{
|
||||
while (nBytesReceived != pData->nTotalPayloadSize)
|
||||
{
|
||||
if (nBytesReceived += pClient->Receive(pData->nNumBytesToReceive))
|
||||
{
|
||||
pClient->Send((const uint8 *)pClient->GetData(), pClient->GetBytesReceived());
|
||||
}
|
||||
}
|
||||
|
||||
sleep(100);
|
||||
sleep(100);
|
||||
|
||||
delete pClient;
|
||||
}
|
||||
delete pClient;
|
||||
}
|
||||
|
||||
socket.Close();
|
||||
socket.Close();
|
||||
|
||||
return NULL;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
pthread_t threadId;
|
||||
struct thread_data thData;
|
||||
CActiveSocket client;
|
||||
char result[1024];
|
||||
pthread_t threadId;
|
||||
struct thread_data thData;
|
||||
CActiveSocket client;
|
||||
char result[1024];
|
||||
|
||||
thData.pszServerAddr = "127.0.0.1";
|
||||
thData.nPort = 6789;
|
||||
thData.nNumBytesToReceive = 1;
|
||||
thData.nTotalPayloadSize = (int)strlen(TEST_PACKET);
|
||||
thData.pszServerAddr = "127.0.0.1";
|
||||
thData.nPort = 6789;
|
||||
thData.nNumBytesToReceive = 1;
|
||||
thData.nTotalPayloadSize = (int)strlen(TEST_PACKET);
|
||||
|
||||
pthread_create(&threadId, 0, CreateTCPEchoServer, &thData);
|
||||
sleep(1); // allow a second for the thread to create and listen
|
||||
pthread_create(&threadId, 0, CreateTCPEchoServer, &thData);
|
||||
sleep(1); // allow a second for the thread to create and listen
|
||||
|
||||
client.Initialize();
|
||||
client.SetNonblocking();
|
||||
client.Initialize();
|
||||
client.SetNonblocking();
|
||||
|
||||
if (client.Open("127.0.0.1", 6789))
|
||||
{
|
||||
if (client.Send((uint8 *)TEST_PACKET, strlen(TEST_PACKET)))
|
||||
{
|
||||
int numBytes = -1;
|
||||
int bytesReceived = 0;
|
||||
if (client.Open("127.0.0.1", 6789))
|
||||
{
|
||||
if (client.Send((uint8 *)TEST_PACKET, strlen(TEST_PACKET)))
|
||||
{
|
||||
int numBytes = -1;
|
||||
int bytesReceived = 0;
|
||||
|
||||
client.Select();
|
||||
client.Select();
|
||||
|
||||
while (bytesReceived != strlen(TEST_PACKET))
|
||||
{
|
||||
numBytes = client.Receive(MAX_PACKET);
|
||||
while (bytesReceived != strlen(TEST_PACKET))
|
||||
{
|
||||
numBytes = client.Receive(MAX_PACKET);
|
||||
|
||||
if (numBytes > 0)
|
||||
{
|
||||
bytesReceived += numBytes;
|
||||
memset(result, 0, 1024);
|
||||
memcpy(result, client.GetData(), numBytes);
|
||||
printf("received %d bytes: '%s'\n", numBytes, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Received %d bytes\n", numBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (numBytes > 0)
|
||||
{
|
||||
bytesReceived += numBytes;
|
||||
memset(result, 0, 1024);
|
||||
memcpy(result, client.GetData(), numBytes);
|
||||
printf("received %d bytes: '%s'\n", numBytes, result);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Received %d bytes\n", numBytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user