/* passivesock allocate & bind a server socket using tcp or ucp */ int PassiveSock(const char *service,const char *transport,int qlen) { struct servent *pse; struct protoent *ppe; struct sockaddr_in sin; int s,type,optval,optlen;
memset(&sin,0,sizeof(sin)); sin.sin_family=AF_INET; sin.sin_addr.s_addr=INADDR_ANY;//?(u_long)0x00000000 if (pse=getservbyname(service,transport)) sin.sin_port=htons(ntohs((u_short)pse->s_port)); else if ((sin.sin_port=htons((u_short)atoi(service)))==0) WriteErrLog("Warning:Can't get service!!:%s\n",service); /* map protocol name to protocol number */ if ((ppe=getprotobyname(transport))==0) WriteErrLog("Warning:Can't get protocol number:%d!!\n",transport);
if (!strcmp(transport,"udp")) type=SOCK_DGRAM; else type=SOCK_STREAM;
/* allocate a socket */ s=socket(PF_INET,type,ppe->p_proto); if (s<0) WriteErrLog("Can't create socket\r\n");
/* bind the socket */ if (bind(s,(struct sockaddr *)&sin,sizeof(sin))<0) WriteErrLog("Warning:Can't bind to %s port: %s!!\n",service,strerror(errno));
if (type==SOCK_STREAM && listen(s,qlen)<0) WriteErrLog("Warning:Can't listen on port!!:%s\n",strerror(errno)); WriteErrLog("SockRecvServer:Listening on port %s.......\n",service); return s; }