Advertisement
tm512

magical linked lists

Jul 31st, 2011
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.97 KB | None | 0 0
  1. // Checks for new connections via accept() and loads them into sock_t linked list.
  2. void getnewconn (void)
  3. {
  4.     struct sockaddr_storage tempAddr;
  5.     int tempSock;
  6.     static sock_t *slnext = NULL;
  7.  
  8.     // if slnext is NULL, we need to allocate some more memory for a new one.
  9.     // After using up this new one, we set it back to NULL.
  10.     if (!slnext)
  11.     {
  12.         slnext = (sock_t *) malloc (sizeof (sock_t));
  13.         memset (slnext, 0, sizeof (sock_t));
  14.     }
  15.  
  16.     if ((tempSock = accept (mastersock, (struct sockaddr *)&tempAddr, sizeof(struct sockaddr_storage))) < 0)
  17.         return; // Not this time. D:
  18.     else // YES A NEW CLIENT
  19.     {
  20.         // Fill it out, then put it in the linked list.
  21.         if (!sltail) sltail = slnext; // :|
  22.         slnext.s = tempSock;
  23.         slnext.addr = (struct sockaddr) tempAddr;
  24.         slhead.next = slnext;
  25.         slnext.prev = slhead;
  26.         slnext.next = NULL;
  27.         slhead = slnext;
  28.     }
  29.     return;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement