librnr/sock.c
Socket_T *SocketOpenTcpListener(int ipPortListener, const char *sLocalHostName)
{
	int									sd;
	struct sockaddr_in	addrInet;
	int									backlog = 8;
	int									nVal;
  Socket_T           *pSocket;

	LOGDIAG4CALL(_TINT(ipPortListener), _TSTR(sLocalHostName));

	//
	// Create socket in protocal familiy INET (IPv4), of type STREAM (TCP).
	//
	if( (sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
	{
		LOGSYSERROR("socket(%d,%d,%d)", PF_INET, SOCK_STREAM, IPPROTO_TCP);
		return NULL;
	}

	//
	// Disallow binding if another local task already listening on this address.
  // After socking binding and listen, then this sock option will disallow 
  // binding from any task to this port on any local address. 
	// Note: Non-critacal, so continue on errors.
	//
	nVal = 1;
	if( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
				             &nVal, (socklen_t)sizeof(nVal)) < 0 )
	{
		LOGSYSERROR("setsockopt(%d,SOL_SOCKET,SO_REUSEADDR)",  sd);
	}

	//
	// Enable sending of keep-alive messages on connection-oriented sockets.
	// Note: Non-critacal, so continue on errors.
	//
	nVal = 1;
	if( setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE,
				             &nVal, (socklen_t)sizeof(nVal)) < 0 )
	{
		LOGSYSERROR("setsockopt(%d,SOL_SOCKET,SO_KEEPALIVE)",  sd);
	}

	//
	// Bind this socket to application family INET (IP) at the given port.
	//
  addrInet.sin_family = AF_INET;				        // ip application family
	addrInet.sin_port 	= htons_nowarn((uint16_t)ipPortListener);
                                                // listen port number

  //
  // Let bind() automatically fill in the IP address of the machine where
  // the calling process is running on.
  // 
  if( sLocalHostName == NULL )
  {
	  addrInet.sin_addr.s_addr = htonl(INADDR_ANY);
  }

  //
  // Bind to a specific local address
  //
  else if( SockResolveHostName(sLocalHostName, &(addrInet.sin_addr)) < 0 )
  {
    LOGERROR("SockResolveHostName() failed");
    close(sd);
    return NULL;
  }

	//
	// Bind socket to the address.
	//
	if( bind(sd, (struct sockaddr *)&addrInet, (socklen_t)sizeof(addrInet)) < 0 )
	{
		LOGSYSERROR("bind(%d, ...)", sd);
		close(sd);
		return NULL;
	}

	//
	// Mark this bound socket as a listener for new incoming connections that 
	// can then be accepted. The backlog is the maximum queue length of pending
	// connection requests.
	//
	if( listen(sd, backlog) < 0 )
	{
		LOGSYSERROR("listen(%d,%d)", sd, backlog);
		close(sd);
		return NULL;
	}

  //
  // Now create a new socket and set the appropriate attributes
  //
  pSocket = SocketNew();

  SocketAttrSetSd(pSocket, sd);
  SocketAttrSetFlavor(pSocket, 
      SockFlavorDomainIPv4|SockFlavorProtocolTcp|SockFlavorFuncListener);
  SocketAttrSetLocalName(pSocket, NULL);
  SocketAttrSetRemoteName(pSocket, NULL, SockStrAnyHost);

  LOGDIAG4("Opened Socket Listener on %s", SocketAttrGetLocalName(pSocket));

	return pSocket;
}
/*!
 * \brief Create and open a new TCP/IP passive listener socket on this host.
 *
 * \param ipPortListener  Port number to listen on.
 * \param sLocalHostName  String name of local host to bind to. If NULL, then
 *                        this host will automatically choose the IP address.
 *
 * \return:
 *  Returns a new listener Socket_T pointer on success, NULL on failure.
 */
\brief Create and open a new TCP/IP passive listener socket on this host.
\param ipPortListener  Port number to listen on.
\param sLocalHostName  String name of local host to bind to. If NULL, then
this host will automatically choose the IP address.

\return:
Returns a new listener Socket_T pointer on success, NULL on failure.
\ brief Create and open a new TCP / IP passive listener socket on this host . \ param ipPortListener Port number to listen on . \ param sLocalHostName String name of local host to bind to . If NULL then this host will automatically choose the IP address . \ return : Returns a new listener Socket_T pointer on success NULL on failure .
Socket_T *SocketOpenTcpListener(int ipPortListener, const char *sLocalHostName)
{
	int									sd;
	struct sockaddr_in	addrInet;
	int									backlog = 8;
	int									nVal;
  Socket_T           *pSocket;
	LOGDIAG4CALL(_TINT(ipPortListener), _TSTR(sLocalHostName));
	 Create socket in protocal familiy INET (IPv4), of type STREAM (TCP).
	if( (sd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0 )
	{
		LOGSYSERROR("socket(%d,%d,%d)", PF_INET, SOCK_STREAM, IPPROTO_TCP);
		return NULL;
	}
	 Disallow binding if another local task already listening on this address.
   After socking binding and listen, then this sock option will disallow 
   binding from any task to this port on any local address. 
	 Note: Non-critacal, so continue on errors.
	nVal = 1;
	if( setsockopt(sd, SOL_SOCKET, SO_REUSEADDR,
				             &nVal, (socklen_t)sizeof(nVal)) < 0 )
	{
		LOGSYSERROR("setsockopt(%d,SOL_SOCKET,SO_REUSEADDR)",  sd);
	}
	 Enable sending of keep-alive messages on connection-oriented sockets.
	 Note: Non-critacal, so continue on errors.
	nVal = 1;
	if( setsockopt(sd, SOL_SOCKET, SO_KEEPALIVE,
				             &nVal, (socklen_t)sizeof(nVal)) < 0 )
	{
		LOGSYSERROR("setsockopt(%d,SOL_SOCKET,SO_KEEPALIVE)",  sd);
	}
	 Bind this socket to application family INET (IP) at the given port.
  addrInet.sin_family = AF_INET;				         ip application family
	addrInet.sin_port 	= htons_nowarn((uint16_t)ipPortListener);
                                                 listen port number
   Let bind() automatically fill in the IP address of the machine where
   the calling process is running on.
  if( sLocalHostName == NULL )
  {
	  addrInet.sin_addr.s_addr = htonl(INADDR_ANY);
  }
   Bind to a specific local address
  else if( SockResolveHostName(sLocalHostName, &(addrInet.sin_addr)) < 0 )
  {
    LOGERROR("SockResolveHostName() failed");
    close(sd);
    return NULL;
  }
	 Bind socket to the address.
	if( bind(sd, (struct sockaddr *)&addrInet, (socklen_t)sizeof(addrInet)) < 0 )
	{
		LOGSYSERROR("bind(%d, ...)", sd);
		close(sd);
		return NULL;
	}
	 Mark this bound socket as a listener for new incoming connections that 
	 can then be accepted. The backlog is the maximum queue length of pending
	 connection requests.
	if( listen(sd, backlog) < 0 )
	{
		LOGSYSERROR("listen(%d,%d)", sd, backlog);
		close(sd);
		return NULL;
	}
   Now create a new socket and set the appropriate attributes
  pSocket = SocketNew();
  SocketAttrSetSd(pSocket, sd);
  SocketAttrSetFlavor(pSocket, 
      SockFlavorDomainIPv4|SockFlavorProtocolTcp|SockFlavorFuncListener);
  SocketAttrSetLocalName(pSocket, NULL);
  SocketAttrSetRemoteName(pSocket, NULL, SockStrAnyHost);
  LOGDIAG4("Opened Socket Listener on %s", SocketAttrGetLocalName(pSocket));
	return pSocket;
}
Socket_T * SocketOpenTcpListener ( int ipPortListener , const char * sLocalHostName ) { int sd ; struct sockaddr_in addrInet ; int backlog = 8 ; int nVal ; Socket_T * pSocket ; LOGDIAG4CALL ( _TINT ( ipPortListener ) , _TSTR ( sLocalHostName ) ) ; if ( ( sd = socket ( PF_INET , SOCK_STREAM , IPPROTO_TCP ) ) < 0 ) { LOGSYSERROR ( " " , PF_INET , SOCK_STREAM , IPPROTO_TCP ) ; return NULL ; } nVal = 1 ; if ( setsockopt ( sd , SOL_SOCKET , SO_REUSEADDR , & nVal , ( socklen_t ) sizeof ( nVal ) ) < 0 ) { LOGSYSERROR ( " " , sd ) ; } nVal = 1 ; if ( setsockopt ( sd , SOL_SOCKET , SO_KEEPALIVE , & nVal , ( socklen_t ) sizeof ( nVal ) ) < 0 ) { LOGSYSERROR ( " " , sd ) ; } addrInet . sin_family = AF_INET ; addrInet . sin_port = htons_nowarn ( ( uint16_t ) ipPortListener ) ; if ( sLocalHostName == NULL ) { addrInet . sin_addr . s_addr = htonl ( INADDR_ANY ) ; } else if ( SockResolveHostName ( sLocalHostName , & ( addrInet . sin_addr ) ) < 0 ) { LOGERROR ( " " ) ; close ( sd ) ; return NULL ; } if ( bind ( sd , ( struct sockaddr * ) & addrInet , ( socklen_t ) sizeof ( addrInet ) ) < 0 ) { LOGSYSERROR ( " " , sd ) ; close ( sd ) ; return NULL ; } if ( listen ( sd , backlog ) < 0 ) { LOGSYSERROR ( " " , sd , backlog ) ; close ( sd ) ; return NULL ; } pSocket = SocketNew ( ) ; SocketAttrSetSd ( pSocket , sd ) ; SocketAttrSetFlavor ( pSocket , SockFlavorDomainIPv4 | SockFlavorProtocolTcp | SockFlavorFuncListener ) ; SocketAttrSetLocalName ( pSocket , NULL ) ; SocketAttrSetRemoteName ( pSocket , NULL , SockStrAnyHost ) ; LOGDIAG4 ( " " , SocketAttrGetLocalName ( pSocket ) ) ; return pSocket ; }
\brief Create and open a new TCP/IP passive listener socket on this host.
\ brief Create and open a new TCP / IP passive listener socket on this host .
// // Create socket in protocal familiy INET (IPv4), of type STREAM (TCP). // // // Disallow binding if another local task already listening on this address. // After socking binding and listen, then this sock option will disallow  // binding from any task to this port on any local address.  // Note: Non-critacal, so continue on errors. // // // Enable sending of keep-alive messages on connection-oriented sockets. // Note: Non-critacal, so continue on errors. // // // Bind this socket to application family INET (IP) at the given port. // // ip application family // listen port number // // Let bind() automatically fill in the IP address of the machine where // the calling process is running on. //  // // Bind to a specific local address // // // Bind socket to the address. // // // Mark this bound socket as a listener for new incoming connections that  // can then be accepted. The backlog is the maximum queue length of pending // connection requests. // // // Now create a new socket and set the appropriate attributes //