Reputation: 1180
In the C language, you have local variables in functions that are destroyed when the function returns (they arn't accessible anymore, unless you pass around pointers obviously).
What happens to sockets? Specifcially, if I create a socket in 1 function, can I pass around the sockid and bind it in another, listen in another, and accept in another?
From what I can see this should work, but I'm not an expert in C.
Upvotes: 0
Views: 330
Reputation: 134316
if I create a socket in 1 function, can I pass around the sockid and bind it in another, listen in another, and accept in another?
Yes, you can. when you create a socket what you get is an socket file descriptor. You can pass that value to other functions, just like you can pass any other value (excluding return
ing address of local variables, to be exact).
Upvotes: 0
Reputation: 223739
There should be no problem doing that. For example:
int sock = my_socket();
my_bind(sock);
my_listen(sock);
int connected_sock = my_connect(sock);
my_read(connected_sock);
Upvotes: 1