创建UNIX域服务端
dts_uint32 COperatingStatusNotify::CreateUnixSocket()
{m_iFd = socket( PF_UNIX,SOCK_STREAM,0 );
if ( m_iFd < 0 )
{
GAL_ERROR_PRINT << "create PF_UNIX SOCK_STREAM error : %m";
GAL_ERROR << "create PF_UNIX SOCK_STREAM error : " << errno;
return -1;
}
CDTS_Auxiliary::DTS_socket_set_option( m_iFd,DTS_SOCKOPT_SNDBUF,40960 );
struct sockaddr_un stUnaddr;
memset( &stUnaddr,0,sizeof(stUnaddr) );
stUnaddr.sun_family = AF_LOCAL;
strncpy( stUnaddr.sun_path+1,m_szPath,sizeof(stUnaddr.sun_path)-1 );
unlink( m_szPath );
socklen_t tLen = sizeof(stUnaddr);//strlen(stUnaddr.sun_path) + offsetof( struct sockaddr_un, sun_path );
if ( bind( m_iFd,(struct sockaddr*)&stUnaddr,tLen ) == -1 )
{
GAL_ERROR_PRINT << "AF_LOCAL bind " << stUnaddr.sun_path+1 << " error : %m";
GAL_ERROR << "AF_LOCAL bind " << stUnaddr.sun_path+1 << errno;
close( m_iFd );
m_iFd = DTS_SOCKET_NULL;
return -1;
}
if ( listen( m_iFd,5 ) != 0 )
{
GAL_ERROR_PRINT << "AF_LOCAL listen " << stUnaddr.sun_path+1 << " error : %m";
GAL_ERROR << "AF_LOCAL listen " << stUnaddr.sun_path+1 << " error : " << errno;
close( m_iFd );
m_iFd = DTS_SOCKET_NULL;
return -1;
}
GAL_DEBUG_PRINT << "AF_LOCAL listen on " << stUnaddr.sun_path << " success";
return 0;
}
创建UNIX域客户端
dts_uint32 CRead_Notifyd::ConnectUnixSocket()
{
m_iFd = socket( PF_UNIX,SOCK_STREAM,0 );
if ( m_iFd < 0 )
{
std::cout << "cannot create PF_UNIX socket error = " << errno << std::endl;
return -1;
}
//std::cout << "create PF_UNIX socket = " << m_iFd << std::endl;
struct sockaddr_un srv_addr;
memset( &srv_addr,0,sizeof(srv_addr) );
srv_addr.sun_family = AF_LOCAL;
strncpy( srv_addr.sun_path+1,m_szPath,sizeof(srv_addr.sun_path)-1 );
unlink( m_szPath );
CDTS_Auxiliary::DTS_socket_set_option( m_iFd,DTS_SOCKOPT_RCVBUF,40960 );
socklen_t tLen = sizeof( srv_addr );//strlen(srv_addr.sun_path) + offsetof( struct sockaddr_un, sun_path );
if ( connect( m_iFd,(struct sockaddr*)&srv_addr,tLen ) == -1 )
{
std::cout << m_iFd << " cannot connect AF_LOCAL error = " << errno << " len = " << tLen << " " << srv_addr.sun_path+1 << std::endl;
close( m_iFd );
m_iFd = DTS_SOCKET_NULL;
return -1;
}
return 0;
}