Skip to content

Commit

Permalink
Merge remote-tracking branch 'hayguen/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
lethosor committed Jun 12, 2016
2 parents 55b6bfb + f04e146 commit b08d765
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 72 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ if(UNIX)
if(NOT CLSOCKET_DEP_ONLY)
install(TARGETS clsocket-example DESTINATION bin)
endif()

ADD_EXECUTABLE(querydaytime-example examples/QueryDayTime.cpp)
TARGET_LINK_LIBRARIES(querydaytime-example clsocket)

ADD_EXECUTABLE(echoserver-example examples/EchoServer.cpp)
TARGET_LINK_LIBRARIES(echoserver-example clsocket)
endif()
endif()

6 changes: 3 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, char **argv)
// Create a connection to the time server so that data can be sent
// and received.
//--------------------------------------------------------------------------
if (socket.Open((const uint8 *)"time-C.timefreq.bldrdoc.gov", 13))
if (socket.Open("time-C.timefreq.bldrdoc.gov", 13))
{
//----------------------------------------------------------------------
// Send a requtest the server requesting the current time.
Expand Down Expand Up @@ -122,7 +122,7 @@ int main(int argc, char **argv)
//--------------------------------------------------------------------------
socket.Initialize();

socket.Listen((const uint8 *)"127.0.0.1", 6789);
socket.Listen("127.0.0.1", 6789);

while (true)
{
Expand All @@ -136,7 +136,7 @@ int main(int argc, char **argv)
//------------------------------------------------------------------
// Send response to client and close connection to the client.
//------------------------------------------------------------------
pClient->Send((const uint8 *)pClient->GetData(), pClient->GetBytesReceived());
pClient->Send( pClient->GetData(), pClient->GetBytesReceived() );
pClient->Close();
}

Expand Down
44 changes: 44 additions & 0 deletions examples/EchoServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include "PassiveSocket.h" // Include header for active socket object definition

#define MAX_PACKET 4096

int main(int argc, char **argv)
{
CPassiveSocket socket;
CActiveSocket *pClient = NULL;

//--------------------------------------------------------------------------
// Initialize our socket object
//--------------------------------------------------------------------------
socket.Initialize();

socket.Listen("127.0.0.1", 6789);

while (true)
{
if ((pClient = socket.Accept()) != NULL)
{
//----------------------------------------------------------------------
// Receive request from the client.
//----------------------------------------------------------------------
if (pClient->Receive(MAX_PACKET))
{
//------------------------------------------------------------------
// Send response to client and close connection to the client.
//------------------------------------------------------------------
pClient->Send( pClient->GetData(), pClient->GetBytesReceived() );
pClient->Close();
}

delete pClient;
}
}

//-----------------------------------------------------------------------------
// Receive request from the client.
//-----------------------------------------------------------------------------
socket.Close();

return 1;
}
44 changes: 44 additions & 0 deletions examples/QueryDayTime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#include <string.h>
#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];

memset(&time, 0, 50);

//--------------------------------------------------------------------------
// 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))
{
//----------------------------------------------------------------------
// Send a requtest the server requesting the current time.
//----------------------------------------------------------------------
if (socket.Send((const uint8 *)"\n", 1))
{
//----------------------------------------------------------------------
// Receive response from the server.
//----------------------------------------------------------------------
socket.Receive(49);
memcpy(&time, socket.GetData(), 49);
printf("%s\n", time);

//----------------------------------------------------------------------
// Close the connection.
//----------------------------------------------------------------------
socket.Close();
}
}


return 1;
}
26 changes: 18 additions & 8 deletions examples/RecvAsync.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
#include <pthread.h>
#include "PassiveSocket.h"

#ifdef WIN32
#include <windows.h>

// usually defined with #include <unistd.h>
static void sleep( unsigned int seconds )
{
Sleep( seconds * 1000 );
}
#endif

#define MAX_PACKET 4096
#define TEST_PACKET "Test Packet"

struct thread_data
{
char *pszServerAddr;
short int nPort;
int nNumBytesToReceive;
int nTotalPayloadSize;
const char *pszServerAddr;
short int nPort;
int nNumBytesToReceive;
int nTotalPayloadSize;
};


Expand All @@ -21,7 +31,7 @@ void *CreateTCPEchoServer(void *param)
int nBytesReceived = 0;

socket.Initialize();
socket.Listen((const uint8 *)pData->pszServerAddr, pData->nPort);
socket.Listen(pData->pszServerAddr, pData->nPort);

if ((pClient = socket.Accept()) != NULL)
{
Expand Down Expand Up @@ -61,7 +71,7 @@ int main(int argc, char **argv)
client.Initialize();
client.SetNonblocking();

if (client.Open((uint8 *)"127.0.0.1", 6789))
if (client.Open("127.0.0.1", 6789))
{
if (client.Send((uint8 *)TEST_PACKET, strlen(TEST_PACKET)))
{
Expand All @@ -79,11 +89,11 @@ int main(int argc, char **argv)
bytesReceived += numBytes;
memset(result, 0, 1024);
memcpy(result, client.GetData(), numBytes);
printf("received: %s\n", result);
printf("received %d bytes: '%s'\n", numBytes, result);
}
else
{
printf("Recevied %d bytes\n", numBytes);
printf("Received %d bytes\n", numBytes);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/ActiveSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CActiveSocket::CActiveSocket(CSocketType nType) : CSimpleSocket(nType)
// ConnectTCP() -
//
//------------------------------------------------------------------------------
bool CActiveSocket::ConnectTCP(const uint8 *pAddr, int16 nPort)
bool CActiveSocket::ConnectTCP(const char *pAddr, uint16 nPort)
{
bool bRetVal = false;
struct in_addr stIpAddress;
Expand Down Expand Up @@ -131,7 +131,7 @@ bool CActiveSocket::ConnectTCP(const uint8 *pAddr, int16 nPort)
// ConnectUDP() -
//
//------------------------------------------------------------------------------
bool CActiveSocket::ConnectUDP(const uint8 *pAddr, int16 nPort)
bool CActiveSocket::ConnectUDP(const char *pAddr, uint16 nPort)
{
bool bRetVal = false;
struct in_addr stIpAddress;
Expand Down Expand Up @@ -190,7 +190,7 @@ bool CActiveSocket::ConnectUDP(const uint8 *pAddr, int16 nPort)
// ConnectRAW() -
//
//------------------------------------------------------------------------------
bool CActiveSocket::ConnectRAW(const uint8 *pAddr, int16 nPort)
bool CActiveSocket::ConnectRAW(const char *pAddr, uint16 nPort)
{
bool bRetVal = false;
struct in_addr stIpAddress;
Expand Down Expand Up @@ -249,7 +249,7 @@ bool CActiveSocket::ConnectRAW(const uint8 *pAddr, int16 nPort)
// Open() - Create a connection to a specified address on a specified port
//
//------------------------------------------------------------------------------
bool CActiveSocket::Open(const uint8 *pAddr, int16 nPort)
bool CActiveSocket::Open(const char *pAddr, uint16 nPort)
{
bool bRetVal = false;

Expand Down
8 changes: 4 additions & 4 deletions src/ActiveSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ class CActiveSocket : public CSimpleSocket {
/// @param pAddr specifies the destination address to connect.
/// @param nPort specifies the destination port.
/// @return true if successful connection made, otherwise false.
virtual bool Open(const uint8 *pAddr, int16 nPort);
virtual bool Open(const char *pAddr, uint16 nPort);

private:
/// Utility function used to create a TCP connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectTCP(const uint8 *pAddr, int16 nPort);
bool ConnectTCP(const char *pAddr, uint16 nPort);

/// Utility function used to create a UDP connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectUDP(const uint8 *pAddr, int16 nPort);
bool ConnectUDP(const char *pAddr, uint16 nPort);

/// Utility function used to create a RAW connection, called from Open().
/// @return true if successful connection made, otherwise false.
bool ConnectRAW(const uint8 *pAddr, int16 nPort);
bool ConnectRAW(const char *pAddr, uint16 nPort);

private:
struct hostent *m_pHE;
Expand Down
17 changes: 12 additions & 5 deletions src/Host.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,16 @@ extern "C"
#endif

#ifdef WIN32
#define UINT8_MAX (UCHAR_MAX)
#define UINT16_MAX (USHRT_MAX)
#define UINT32_MAX (ULONG_MAX)

#ifndef UINT8_MAX
#define UINT8_MAX (UCHAR_MAX)
#endif
#ifndef UINT16_MAX
#define UINT16_MAX (USHRT_MAX)
#endif
#ifndef UINT32_MAX
#define UINT32_MAX (ULONG_MAX)
#endif

#if __WORDSIZE == 64
#define SIZE_MAX (18446744073709551615UL)
Expand Down Expand Up @@ -166,7 +173,7 @@ extern "C"
#define WRITEV(a,b,c) Writev(b, c)
#define GETSOCKOPT(a,b,c,d,e) getsockopt(a,b,c,(char *)d, (int *)e)
#define SETSOCKOPT(a,b,c,d,e) setsockopt(a,b,c,(char *)d, (int)e)
#define GETHOSTBYNAME(a) gethostbyname((const char *)a)
#define GETHOSTBYNAME(a) gethostbyname(a)
#endif

#if defined(_LINUX) || defined(_DARWIN)
Expand All @@ -189,7 +196,7 @@ extern "C"
#define WRITEV(a,b,c) writev(a, b, c)
#define GETSOCKOPT(a,b,c,d,e) getsockopt((int)a,(int)b,(int)c,(void *)d,(socklen_t *)e)
#define SETSOCKOPT(a,b,c,d,e) setsockopt((int)a,(int)b,(int)c,(const void *)d,(int)e)
#define GETHOSTBYNAME(a) gethostbyname((const char *)a)
#define GETHOSTBYNAME(a) gethostbyname(a)
#endif


Expand Down
14 changes: 7 additions & 7 deletions src/PassiveSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ CPassiveSocket::CPassiveSocket(CSocketType nType) : CSimpleSocket(nType)
{
}

bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup, int16 nPort)
bool CPassiveSocket::BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort)
{
bool bRetVal = false;
#ifdef WIN32
Expand All @@ -73,13 +73,13 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
// If no IP Address (interface ethn) is supplied, or the loop back is
// specified then bind to any interface, else bind to specified interface.
//--------------------------------------------------------------------------
if ((pInterface == NULL) || (!strlen((const char *)pInterface)))
if ((pInterface == NULL) || (!strlen(pInterface)))
{
m_stMulticastGroup.sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
if ((inAddr = inet_addr((const char *)pInterface)) != INADDR_NONE)
if ((inAddr = inet_addr(pInterface)) != INADDR_NONE)
{
m_stMulticastGroup.sin_addr.s_addr = inAddr;
}
Expand All @@ -93,7 +93,7 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
//----------------------------------------------------------------------
// Join the multicast group
//----------------------------------------------------------------------
m_stMulticastRequest.imr_multiaddr.s_addr = inet_addr((const char *)pGroup);
m_stMulticastRequest.imr_multiaddr.s_addr = inet_addr(pGroup);
m_stMulticastRequest.imr_interface.s_addr = m_stMulticastGroup.sin_addr.s_addr;

if (SETSOCKOPT(m_socket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
Expand Down Expand Up @@ -130,7 +130,7 @@ bool CPassiveSocket::BindMulticast(const uint8 *pInterface, const uint8 *pGroup,
// Listen() -
//
//------------------------------------------------------------------------------
bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBacklog)
bool CPassiveSocket::Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog)
{
bool bRetVal = false;
#ifdef WIN32
Expand Down Expand Up @@ -160,13 +160,13 @@ bool CPassiveSocket::Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBa
// If no IP Address (interface ethn) is supplied, or the loop back is
// specified then bind to any interface, else bind to specified interface.
//--------------------------------------------------------------------------
if ((pAddr == NULL) || (!strlen((const char *)pAddr)))
if ((pAddr == NULL) || (!strlen(pAddr)))
{
m_stServerSockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
}
else
{
if ((inAddr = inet_addr((const char *)pAddr)) != INADDR_NONE)
if ((inAddr = inet_addr(pAddr)) != INADDR_NONE)
{
m_stServerSockaddr.sin_addr.s_addr = inAddr;
}
Expand Down
6 changes: 3 additions & 3 deletions src/PassiveSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class CPassiveSocket : public CSimpleSocket {
/// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
bool BindMulticast(const uint8 *pInterface, const uint8 *pGroup, int16 nPort);
bool BindMulticast(const char *pInterface, const char *pGroup, uint16 nPort);

/// Create a listening socket at local ip address 'x.x.x.x' or 'localhost'
/// if pAddr is NULL on port nPort.
Expand All @@ -92,10 +92,10 @@ class CPassiveSocket : public CSimpleSocket {
/// @param nConnectionBacklog specifies connection queue backlog (default 30,000)
/// @return true if a listening socket was created.
/// If not successful, the false is returned and one of the following error
/// condiitions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
/// conditions will be set: CPassiveSocket::SocketAddressInUse, CPassiveSocket::SocketProtocolError,
/// CPassiveSocket::SocketInvalidSocket. The following socket errors are for Linux/Unix
/// derived systems only: CPassiveSocket::SocketInvalidSocketBuffer
virtual bool Listen(const uint8 *pAddr, int16 nPort, int32 nConnectionBacklog = 30000);
virtual bool Listen(const char *pAddr, uint16 nPort, int32 nConnectionBacklog = 30000);

/// Attempts to send a block of data on an established connection.
/// @param pBuf block of data to be sent.
Expand Down
Loading

0 comments on commit b08d765

Please sign in to comment.