Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close socket [closed]

Tags:

c++

sockets

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?

like image 535
Avihai Marchiano Avatar asked Aug 17 '12 12:08

Avihai Marchiano


People also ask

How do I properly close a socket?

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.

What if socket is not 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.

How do you close a socket from a client?

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.

Do sockets close automatically?

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.


1 Answers

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.

like image 195
Kerrek SB Avatar answered Oct 09 '22 19:10

Kerrek SB