0
votes

I'm making a simple client-server application using winsock2 library, but I get a "5" error code when I try to open a socket, but the first error code in the documentation is "6". The thing is, I don't understand what causes the program to exit.

int sockfd;
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
    perror("socket creation failed");
    #ifdef OS_WINDOWS
    std::cerr << WSAGetLastError() << std::endl;
    #endif
    exit(EXIT_FAILURE);
}
1
Try GetLastError instead. That is "ERROR_ACCESS_DENIED". Also, that WSA error documentation shows the large jump between error number values. I believe that is to match the same error codes in GetLastError.PaulMcKenzie
You should post a minimal reproducible example. There is no indication if you called WSAStartup before issuing other calls to create sockets.PaulMcKenzie
well, actually I didn't call WSAStartup, let me trymemesaregood
That'll probably be why then :PAsteroids With Wings

1 Answers

2
votes

The error codes returned from WSAGetLastError are a subset of the error codes returned by GetLastError.

Since GetLastError is a superset of all the errors returned by WSAGetLastError, calling GetLastError will yield the same error values as calling WSAGetLastError, plus any errors that are not Winsock-related.

A 5 return code is an ERROR_ACCESS_DENIED. Why you are getting this error is a different story.

See the following link explaining what may cause this issue.