简体   繁体   中英

Winsock; Do I need to close a socket before reconnecting it?

I'm using winsock and I open my sockets in the standard way (I handle errors correctly, but for the sake of this question I've made the code brief);

SOCKET sSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

If I then connect it like so, it may lose connection occasionally.

connect(sSocket, reinterpret_cast<SOCKADDR*>(&sinAddr), sizeof(sinAddr));

When this happens, do I need to close and re-create the socket, or should I just re-call connect() ?

After a socket has been close() ed, it can not be used anymore.

Or the other way round: As long as close() has not been called on a socket, it can be (re-)used.

The call to socket() allocates a socket descriptor to the calling process. The socket descriptor exists and stays assigned to the process until close() is called.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM