1

I wonder how system assign socket number in C. Is that random assignment or incrementally (first client has socket number 1, second client has socket number 2 , ...).

1
  • 1
    Which "system"? Generally, there is no guarantee for any behaviour. The system can do what the system likes, and you as userland programmer have to bear with it.
    – deviantfan
    Commented Feb 23, 2014 at 0:08

1 Answer 1

1

On UNIX systems, socket assignment is done like with other calls that return a new file descriptor. As long as the system has resources to create the socket, the lowest available file descriptor is used for the new socket. Otherwise, an error is returned instead.

3
  • And note that 'lowest available' is per-process, not global. On Windows, socket handles are pointers.
    – user207421
    Commented Feb 23, 2014 at 0:39
  • @EJP Microsoft seems to disagree with you: msdn.microsoft.com/en-us/library/windows/desktop/… Commented Feb 23, 2014 at 2:02
  • @JeremyFriesner There is no disagreement that I can see. MS aren't giving their secrets away but if you look at the value of a SOCKET you will see for yourself that it is clearly a pointer, like all other handles in Windows. They play with the upper bits a bit for various purposes.
    – user207421
    Commented Feb 23, 2014 at 2:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.