can not connect to the server?

mc 5,426 Reputation points
2025-05-21T07:46:54.5+00:00

I want to connect to the server but there is error:10047

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
	WSACleanup();
	return;
}
sockaddr_in sock;
sock.sin_family = AF_INET;
sock.sin_port = htons(5000);
sock.sin_addr.S_un.S_addr = INADDR_ANY;
sock.sin_port = htons(5000);
bind(s, (SOCKADDR*)&sock, sizeof(sock));
listen(s, 5);
s = accept(s, NULL, NULL);
if (s == INVALID_SOCKET) {
	WSACleanup();
	return;
}

and then I connect it :

s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (s == INVALID_SOCKET) {
	WSACleanup();
	return;
}
sockaddr_in sock;
inet_pton(AF_INET, "127.0.0.1", &sock.sin_addr);
sock.sin_port = htons(5000);
if (connect(s, (SOCKADDR*)&sock, sizeof(sock)) == SOCKET_ERROR) {
	auto res = WSAGetLastError();
	WSACleanup();
	return;
}

it connect failed and return 10047 of WSAGetLastError();

Windows development Windows API - Win32
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. LISBOA-4826 245 Reputation points Volunteer Moderator
    2025-05-21T08:05:15.64+00:00

    Hi mc

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    I understand that you are experiencing issue with socket connection during your try, as per our documentation, you can find the details and more steps to troubleshoot.

    https://learn.microsoft.com/en-us/windows/win32/winsock/windows-sockets-error-codes-2

    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    If you have any other questions or are still running into more issues, let me know in the "comments" and I would be happy to help you.

    Thank You.

    Lisboa

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.