Windows socket WSACleanup C++

5.9k views Asked by At

I am using sockets on my program. Due to I added the WSAStartup. My application runs fine (It is always up till it gets a signal to stop). After getting the signal it stops te problem that if I write the WSACleanup function at the end of my program it crashes and if I remove it it terminates fine.

Thanks

2

There are 2 answers

0
Boris Raznikov On BEST ANSWER

Couldn't resovle and find the problem.The application is using more than just sockets. Although Microsoft reference sais that onevery WSAStartup you must use WSACleanup, well this is not true and the system is releasing things regards that.

4
Andrejs Cainikovs On

This is excerpt from one of my projects.

Initialisation:

#if defined(WIN32)
WSADATA wsaData;
WORD wVersionRequested = MAKEWORD(2, 0);
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
  LOG("WSAStartup() error");
  return false;
}
#endif

serv_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);

...

Cleanup:

#if defined(WIN32)
  /* winsock requires a special function for sockets */
  shutdown(serv_socket, SD_BOTH);
  closesocket(serv_socket);
  /* clean up winsock */
  WSACleanup();  
#else
  close(serv_socket);
#endif

Hope this helps.