sin_util.c
changeset 3 1223f413e054
parent 0 ae252e0fd9b8
equal deleted inserted replaced
2:19bf8e678c42 3:1223f413e054
   151   // TODO allow random port... Needs new input parameter to function interface!
   151   // TODO allow random port... Needs new input parameter to function interface!
   152   if (sin_initaddr(&sin, host, 1, service, 0, protocol) < 0) return -1;
   152   if (sin_initaddr(&sin, host, 1, service, 0, protocol) < 0) return -1;
   153   if ((type = gettypebyname(protocol)) == SOCK_PACKET)       return -1;
   153   if ((type = gettypebyname(protocol)) == SOCK_PACKET)       return -1;
   154   /* create the socket */
   154   /* create the socket */
   155   if ((s = socket(PF_INET, type, 0)) < 0)                   {perror("socket()");  return -1;}   
   155   if ((s = socket(PF_INET, type, 0)) < 0)                   {perror("socket()");  return -1;}   
       
   156   if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &(int){ 1 }, sizeof(int)) < 0) {
       
   157       perror("setsockopt(SO_REUSEADDR) failed");
       
   158       return -1;
       
   159   }
   156   /* bind the socket */
   160   /* bind the socket */
   157   if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0)   {perror("bind()");    return -1;}
   161   if (bind(s, (struct sockaddr *)&sin, sizeof (sin)) < 0)   {perror("bind()");    return -1;}
   158   
   162   
   159   return(s);
   163   return(s);
   160 }
   164 }