Run c++ on Ubuntu. I open socket in this way:
socket(AF_PACKET, SOCK_RAW, IPPROTO_RAW))
What do i need to do in the end of the use? Some socket will be used as long as the program run , do i need to check them from a while? to see if socket still exist?
close() call shuts down the socket associated with the socket descriptor socket, and frees resources allocated to the socket. If socket refers to an open TCP connection, the connection is closed. If a stream socket is closed when there is input data queued, the TCP connection is reset rather than being cleanly closed.
One way or another, if you don't close a socket, your program will leak a file descriptor. Programs can usually only open a limited number of file descriptors, so if this happens a lot, it may turn into a problem.
When you have finished dealing with the client you must close() that socket, (that's close(cfd) in your terminology). You may also shutdown() the socket - that will influence how the socket is closed at a TCP level.
Closing a socket multiple times will not return an error. Sockets will be automatically closed when the last reference is dropped, but you might want to call this function to make sure resources are released as early as possible.
The socket exists until you call close
on its file descriptor. Since you have a raw IP socket, there's no notion of "being alive". Just close it when you're done using it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With