hi all,
i meet a problem when opening a socket, is that when the remote host not available or its port hanged ,my program still suspending untill i cancel the operation...
but when the host is ok ,my program work ok.
i handled in my code all these exceptions ,but my problem is when the host is not avaialable i want to exit with that error ???!!!!
here is the part of my code for socket programming :
void ethernet::etherSetup(char *strIP,int nPort)
{
unsigned long ip;
if((*strIP <= '9') && (*strIP >= '0'))
{
if( (int)(ip = inet_addr(strIP)) == -1 )
{
printf("\r\nIP-address must be of the form a.b.c.d\n");
exit (2);
}
}
else
{
server = gethostbyname(strIP);
if(!server)
{
printf("\nError in host Name.\n");
exit(2);
}
ip = *(unsigned long*)(server->h_addr);
}
/////printf("\r\nSERVER IP=%d\r\n ",ip);
server = gethostbyaddr((char *)&ip,sizeof(ip),AF_INET);
if (server == NULL)
{
printf("\r\nERROR, no such host .\n");
exit(1);
}
bzero((char *) &m_sockaddr_in, sizeof(m_sockaddr_in));
m_sockaddr_in.sin_family = AF_INET;
m_sockaddr_in.sin_port = htons(nPort);
m_sockaddr_in.sin_addr = *(in_addr*)&ip;
printf("remotehost : %s , port : %d \n",strIP,nPort);
}
//-----------------------------------------------------------------
int ethernet::Create()
{
sockfd = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP);
if ( sockfd <0 ) return -1;
return 0;
}
//-----------------------------------------------------------------
int ethernet::Connect()
{
int ret;
ret = connect(sockfd,(sockaddr*)&m_sockaddr_in,sizeof(m_sockaddr_in));
if ( ret <0 ) return -1; //ERROR in connection>
return 0;
}
N.B:
this code runs under UNIX sun solaris OS
thanks.