Code:
void handle2(int signo)
{
printf("signal number: %d\n",signo);
return;
}
main(int argc,char **argv)
{
int sockfd,newsockfd,cli_len;
struct sockaddr_in cli_addr,serv_addr;
int maxseg,sendbufsize,recvbufsize,optlen;
int bufsize;
int ret;
struct servent *sp; // point of services
char command[500];
char hostname[34];
// Open a socket of TCP(an internet stream socket)
signal(SIGALRM,handle2);
signal(SIGINT,handle2);
signal(SIGABRT,handle2);
signal(SIGFPE,handle2);
signal(SIGSEGV,handle2);
gethostname(hostname,34);
if ((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
err_dump("server: can't open stream socket!");
// Bind our local address so that the clint can connect to us
bzero((char*)&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr =htonl(INADDR_ANY);
if((sp=getservbyname("rtereset","tcp"))==NULL){
perror("tcp_init:unkown service\n");
return(-1);
}
serv_addr.sin_port=sp->s_port;
bufsize=131072;
if(setsockopt(sockfd,SOL_SOCKET,SO_SNDBUF,&bufsize,sizeof(bufsize))<0)
{
perror("tcp_init setopt err:");
return(-1);
}
if(setsockopt(sockfd,SOL_SOCKET,SO_RCVBUF,&bufsize,sizeof(bufsize))<0)
{
perror("tcp_init setopt err:");
return(-1);
}
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&bufsize,sizeof(bufsize))<0)
{
perror("tcp_init setopt err:");
return -1;
}
if(setsockopt(sockfd,SOL_SOCKET,SO_REUSEPORT,&bufsize,sizeof(bufsize))<0)
{
perror("tcp_init setopt err:");
return -1;
}
if (bind(sockfd,
(struct sockaddr *)&serv_addr,
sizeof(struct sockaddr_in))<0)
{
err_dump("server: can't bind local address!");
return(-1);
}
listen(sockfd,8);
for(;;){
// waiting for connection from clint process.
newsockfd=accept(sockfd,
(struct sockaddr *)&cli_addr,
&cli_len);
if (newsockfd<0) // Here it returned IP 0.0.0.0 and no error report.
{
if (errno == EINTR)
continue;
else
err_dump("server :accept error!");
return(-1);
}
ret=recv(newsockfd,(char *)command ,500,0);
if (ret < 0)
{
err_dump("server : recv error!");
return -1;
}
}
close(sockfd);
close(newsockfd);
}