Skip to content

Commit

Permalink
bugfixes and enhancements on local/peer and bind()
Browse files Browse the repository at this point in the history
* fixed CSimpleSocket::GetClientPort()
* fixed peer/local address in CPassiveSocket::Accept()
* fixed Linux compilation in CPassiveSocket::Listen()
* added CSimpleSocket::Bind() for tcp/udp client connections
* enhanced all examples: all print local/peer address and port
* fixed output of GetLocalAddr() and GetPeerAddr() in examples
* examples txtoserver and txtoudpserver with optional local binding

Signed-off-by: hayati ayguen <[email protected]>
  • Loading branch information
hayguen committed Aug 12, 2020
1 parent 2e20bfb commit f011324
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 49 deletions.
17 changes: 16 additions & 1 deletion examples/DelayedEchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,31 @@ int main(int argc, char **argv)
CPassiveSocket socket;
CSimpleSocket *pClient = NULL;

const char * bindAddr = 0;
unsigned bindPort = 6789;
if ( argc <= 1 )
fprintf(stderr, "usage: %s [<bind-address> [<bind-port>]]\n", argv[0] );
if ( 1 < argc )
bindAddr = argv[1];
if ( 2 < argc )
bindPort = atoi(argv[2]) & 65535;

//--------------------------------------------------------------------------
// Initialize our socket object
//--------------------------------------------------------------------------
socket.Initialize();
socket.Listen( 0, 6789 ); // NULL (not "127.0.0.1") to allow testing with remotes
fprintf(stderr, "binding to %s:%u\n", bindAddr, bindPort);
socket.Listen( bindAddr, uint16(bindPort) ); // not "127.0.0.1" to allow testing with remotes

while (true)
{
if ((pClient = socket.Accept()) != NULL)
{
fprintf(stderr, "\nLocal is %s. Local: %s:%u "
, ( pClient->IsServerSide() ? "Server" : "Client" )
, pClient->GetLocalAddr(), (unsigned)pClient->GetLocalPort());
fprintf(stderr, "Peer: %s:%u\n", pClient->GetPeerAddr(), (unsigned)pClient->GetPeerPort());

//----------------------------------------------------------------------
// Receive request from the client.
//----------------------------------------------------------------------
Expand Down
23 changes: 16 additions & 7 deletions examples/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,31 @@ int main(int argc, char **argv)
CPassiveSocket socket;
CActiveSocket *pClient = NULL;

const char * bindAddr = 0;
unsigned bindPort = 6789;
if ( argc <= 1 )
fprintf(stderr, "usage: %s [<bind-address> [<bind-port>]]\n", argv[0] );
if ( 1 < argc )
bindAddr = argv[1];
if ( 2 < argc )
bindPort = atoi(argv[2]) & 65535;

//--------------------------------------------------------------------------
// Initialize our socket object
//--------------------------------------------------------------------------
socket.Initialize();
socket.Listen( 0, 6789 ); // NULL (not "127.0.0.1") to allow testing with remotes
fprintf(stderr, "binding to %s:%u\n", bindAddr, bindPort);
socket.Listen( bindAddr, uint16(bindPort) ); // not "127.0.0.1" to allow testing with remotes

while (true)
{
if ((pClient = socket.Accept()) != NULL)
{
fprintf(stderr, "\nLocal is %s. Local: %s:%u "
, ( pClient->IsServerSide() ? "Server" : "Client" )
, pClient->GetLocalAddr(), (unsigned)pClient->GetLocalPort());
fprintf(stderr, "Peer: %s:%u\n", pClient->GetPeerAddr(), (unsigned)pClient->GetPeerPort());

//----------------------------------------------------------------------
// Receive request from the client.
//----------------------------------------------------------------------
Expand All @@ -27,12 +42,6 @@ int main(int argc, char **argv)

if (pClient->Receive(MAX_PACKET))
{
fprintf(stderr, "\n%s. Local: %s:%u Peer: %s:%u\n"
, ( pClient->IsServerSide() ? "Local is Server" : "Local is Client" )
, pClient->GetLocalAddr(), (unsigned)pClient->GetLocalPort()
, pClient->GetPeerAddr(), (unsigned)pClient->GetPeerPort()
);

//------------------------------------------------------------------
// Send response to client and close connection to the client.
//------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions examples/QueryDayTime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ int main(int argc, char **argv)
fprintf(stderr, "trying to connect to timeserver %s:%d ..\n", timeServer, PortNo);
if (socket.Open(timeServer, (uint16)PortNo))
{
fprintf(stderr, "\nLocal is %s. Local: %s:%u "
, ( socket.IsServerSide() ? "Server" : "Client" )
, socket.GetLocalAddr(), (unsigned)socket.GetLocalPort());
fprintf(stderr, "Peer: %s:%u\n", socket.GetPeerAddr(), (unsigned)socket.GetPeerPort());

//----------------------------------------------------------------------
// Send a requtest the server requesting the current time.
//----------------------------------------------------------------------
Expand Down
18 changes: 12 additions & 6 deletions examples/TxToServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(int argc, char **argv)

if ( argc < 3 )
{
fprintf(stderr, "usage: %s <host> <text>\n", argv[0] );
fprintf(stderr, "usage: %s <host> <text> [<bind-address> <bind-port>]\n", argv[0] );
return 10;
}

Expand All @@ -20,18 +20,24 @@ int main(int argc, char **argv)
//--------------------------------------------------------------------------
socket.Initialize();

if ( 4 < argc )
{
unsigned bindPort = atoi(argv[4]);
bool ret = socket.Bind( argv[3], uint16(bindPort) );
fprintf(stderr, "bind to %s:%u %s\n", argv[3], bindPort, (ret ? "successful":"failed") );
}

bool bConnOK = socket.Open( argv[1], 6789 );
if ( !bConnOK )
{
fprintf(stderr, "error connecting to '%s'!\n", argv[1] );
return 10;
}

fprintf(stderr, "\n%s. Local: %s:%u Peer: %s:%u\n"
, ( socket.IsServerSide() ? "Local is Server" : "Local is Client" )
, socket.GetLocalAddr(), (unsigned)socket.GetLocalPort()
, socket.GetPeerAddr(), (unsigned)socket.GetPeerPort()
);
fprintf(stderr, "\nLocal is %s. Local: %s:%u "
, ( socket.IsServerSide() ? "Server" : "Client" )
, socket.GetLocalAddr(), (unsigned)socket.GetLocalPort());
fprintf(stderr, "Peer: %s:%u\n", socket.GetPeerAddr(), (unsigned)socket.GetPeerPort());

uint32 nSendBufSize = socket.GetSendWindowSize();
fprintf(stderr, "default Send Buffer Size %u\n", nSendBufSize );
Expand Down
18 changes: 12 additions & 6 deletions examples/TxToUdpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ int main(int argc, char **argv)

if ( argc < 3 )
{
fprintf(stderr, "usage: %s <host> <text>\n", argv[0] );
fprintf(stderr, "usage: %s <host> <text> [<bind-address> <bind-port>]\n", argv[0] );
return 10;
}

Expand All @@ -20,18 +20,24 @@ int main(int argc, char **argv)
//--------------------------------------------------------------------------
socket.Initialize();

if ( 4 < argc )
{
unsigned bindPort = atoi(argv[4]);
bool ret = socket.Bind( argv[3], uint16(bindPort) );
fprintf(stderr, "bind to %s:%u %s\n", argv[3], bindPort, (ret ? "successful":"failed") );
}

bool bConnOK = socket.Open( argv[1], 6789 );
if ( !bConnOK )
{
fprintf(stderr, "error connecting to '%s'!\n", argv[1] );
return 10;
}

fprintf(stderr, "\n%s. Local: %s:%u Peer: %s:%u\n"
, ( socket.IsServerSide() ? "Local is Server" : "Local is Client" )
, socket.GetLocalAddr(), (unsigned)socket.GetLocalPort()
, socket.GetPeerAddr(), (unsigned)socket.GetPeerPort()
);
fprintf(stderr, "\nLocal is %s. Local: %s:%u "
, ( socket.IsServerSide() ? "Server" : "Client" )
, socket.GetLocalAddr(), (unsigned)socket.GetLocalPort());
fprintf(stderr, "Peer: %s:%u\n", socket.GetPeerAddr(), (unsigned)socket.GetPeerPort());

size_t sendSize = strlen( argv[2] );
socket.Send( argv[2], (int32)sendSize );
Expand Down
26 changes: 20 additions & 6 deletions examples/UdpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ int main(int argc, char **argv)
char buffer[ MAX_PACKET ];
CPassiveSocket passiveSocket( CSimpleSocket::SocketTypeUdp );

const char * bindAddr = 0;
unsigned bindPort = 6789;
if ( argc <= 1 )
fprintf(stderr, "usage: %s [<bind-address> [<bind-port>]]\n", argv[0] );
if ( 1 < argc )
bindAddr = argv[1];
if ( 2 < argc )
bindPort = atoi(argv[2]) & 65535;

//--------------------------------------------------------------------------
// Initialize our socket object
//--------------------------------------------------------------------------
passiveSocket.Initialize();
passiveSocket.Bind( 0, 6789 ); // NULL (not "127.0.0.1") to allow testing with remotes
fprintf(stderr, "binding to %s:%u\n", bindAddr, bindPort);
passiveSocket.Bind( bindAddr, uint16(bindPort) ); // not "127.0.0.1" to allow testing with remotes
CSimpleSocket &socket = passiveSocket;

fprintf(stderr, "\nLocal is %s. Local: %s:%u "
, ( socket.IsServerSide() ? "Server" : "Client" )
, socket.GetLocalAddr(), (unsigned)socket.GetLocalPort());
fprintf(stderr, "(invalid) Peer: %s:%u\n", socket.GetPeerAddr(), (unsigned)socket.GetPeerPort());

socket.SetReceiveTimeoutMillis(500);

while (true)
Expand All @@ -31,11 +46,10 @@ int main(int argc, char **argv)
fprintf( stderr, "GetNumReceivableBytes() = %d != %d = Receive() !\n", peekRx, rx );
if (rx > 0)
{
fprintf(stderr, "\n%s. Local: %s:%u Peer: %s:%u\n"
, ( passiveSocket.IsServerSide() ? "Local is Server" : "Local is Client" )
, passiveSocket.GetLocalAddr(), (unsigned)passiveSocket.GetLocalPort()
, passiveSocket.GetPeerAddr(), (unsigned)passiveSocket.GetPeerPort()
);
fprintf(stderr, "\nLocal is %s. Local: %s:%u "
, ( socket.IsServerSide() ? "Server" : "Client" )
, socket.GetLocalAddr(), (unsigned)socket.GetLocalPort());
fprintf(stderr, "Peer: %s:%u\n", socket.GetPeerAddr(), (unsigned)socket.GetPeerPort());

fprintf(stderr, "\nreceived %d bytes:\n", rx );
for ( int k = 0; k < rx; ++k )
Expand Down
90 changes: 70 additions & 20 deletions src/SimpleSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,15 @@ CSimpleSocket::CSimpleSocket(CSocketType nType) :
m_bIsServerSide(false),
m_bPeerHasClosed(true)
{
SetConnectTimeout(1, 0); // default timeout for connection
memset(&m_stConnectTimeout, 0, sizeof(struct timeval));
memset(&m_stRecvTimeout, 0, sizeof(struct timeval));
memset(&m_stSendTimeout, 0, sizeof(struct timeval));
memset(&m_stLinger, 0, sizeof(struct linger));
SetConnectTimeout(1, 0); // default timeout for connection

memset(&m_stServerSockaddr, 0, sizeof(struct sockaddr));
memset(&m_stClientSockaddr, 0, sizeof(struct sockaddr));
memset(&m_stMulticastGroup, 0, sizeof(struct sockaddr));

switch(nType)
{
Expand Down Expand Up @@ -834,6 +839,63 @@ bool CSimpleSocket::Open(const char *pAddr, uint16 nPort)
}


//------------------------------------------------------------------------------
//
// Bind()
//
//------------------------------------------------------------------------------
bool CSimpleSocket::Bind(const char *pInterface, uint16 nPort)
{
bool bRetVal = false;
#ifdef WIN32
ULONG inAddr;
#else
in_addr_t inAddr;
#endif

memset(&m_stClientSockaddr,0,sizeof(m_stClientSockaddr));
m_stClientSockaddr.sin_family = AF_INET;
m_stClientSockaddr.sin_port = htons(nPort);

//--------------------------------------------------------------------------
// 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 && pInterface[0])
{
inet_pton(AF_INET, pInterface, &inAddr);
}
else
{
inAddr = INADDR_ANY;
}

m_stClientSockaddr.sin_addr.s_addr = inAddr;

ClearSystemError();

//--------------------------------------------------------------------------
// Bind to the specified port
//--------------------------------------------------------------------------
if (bind(m_socket, (struct sockaddr *)&m_stClientSockaddr, sizeof(m_stClientSockaddr)) == 0)
{
bRetVal = true;
}

//--------------------------------------------------------------------------
// If there was a socket error then close the socket to clean out the
// connection in the backlog.
//--------------------------------------------------------------------------
TranslateSocketError();

if (bRetVal == false)
{
Close();
}

return bRetVal;
}

//------------------------------------------------------------------------------
//
// BindInterface()
Expand Down Expand Up @@ -957,7 +1019,7 @@ const char * CSimpleSocket::GetClientAddr() const
/// @return client port number.
uint16 CSimpleSocket::GetClientPort() const
{
return m_stClientSockaddr.sin_port;
return ntohs(m_stClientSockaddr.sin_port);
}

/// Returns server Internet host address as a string in standard numbers-and-dots notation.
Expand Down Expand Up @@ -1204,25 +1266,13 @@ int32 CSimpleSocket::Send(const uint8 *pBuf, size_t bytesToSend)
// Check error condition and attempt to resend if call
// was interrupted by a signal.
//---------------------------------------------------------
// if (GetMulticast())
// {
// do
// {
// m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0, (const sockaddr *)&m_stMulticastGroup,
// sizeof(m_stMulticastGroup));
// TranslateSocketError();
// } while (GetSocketError() == CSimpleSocket::SocketInterrupted);
// }
// else
do
{
do
{
m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0,
(const sockaddr *)&m_stServerSockaddr,
sizeof(m_stServerSockaddr));
TranslateSocketError();
} while (GetSocketError() == CSimpleSocket::SocketInterrupted);
}
m_nBytesSent = SENDTO(m_socket, pBuf, bytesToSend, 0,
(const sockaddr *)&m_stServerSockaddr,
sizeof(m_stServerSockaddr));
TranslateSocketError();
} while (GetSocketError() == CSimpleSocket::SocketInterrupted);

m_timer.SetEndTime();
}
Expand Down
Loading

0 comments on commit f011324

Please sign in to comment.