0

Im trying to create a program using Windows sockets and i am getting an error code 0 when trying to create the socket

int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != NO_ERROR) {
    wprintf(L"WSAStartup function failed with error: %d\n", iResult);
}

csocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); 

if(csocket != INVALID_SOCKET){
    wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
}

The part that is failing is creating the socket and the output i am getting is "socket function failed with error: 0.

Thanks for the help in advance.

1 Answer 1

3

The if condition is wrong and the socket descriptor is actually being created as it does not equal INVALID_SOCKET.

Change to:

if (csocket == INVALID_SOCKET){
    wprintf(L"socket function failed with error: %ld\n", WSAGetLastError());
}
Sign up to request clarification or add additional context in comments.

1 Comment

wait never mind i had the check wrong. Thank you, that fixed it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.