Reputation: 367
I would like to know if there is any way to create an already created but closed socket id. We use the following line of code to create a socket.
sock = socket(AF_INET, SOCK_STREAM, 0);
Supposing this call returned the id as 3 and this socket is closed in the course of the program. Is there anyway for me create a socket with the same id, 3?
Upvotes: 1
Views: 59
Reputation: 24905
No. You cannot control the what ids will be generated. Writing code with such an assumption has the dangerous possibility of failing often. It is also bad by design.
Intead of depending on socket handles, define your own structure / ids for identifying a connection.
Upvotes: 1
Reputation: 13877
The only control you have over the file descriptor returned by a call to open()
or socket()
etc is that the file descriptor used is the lowest positive integer not already in use. And that's only in unix. Can't speak for Windows.
If you need to have code use a new connection in place of an old one, your best bet is to add a layer of indirection - create, say, an array of socket descriptors, and refer to connections as indexes into this array, rather than passing the descriptors themselves around.
Upvotes: 1