При такой версии (ниже) создания сокета после bind вылетает ошибка 10047 (Адрес несовместим с выбранным протоколом):
DWORD WINAPI ThreadForAccept(LPVOID lpParam)
{
SOCKET sock = socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
if (sock == SOCKET_ERROR) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), TRUE);
return 0;
}
int iOptval = 1;
int res = setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
(char *)&iOptval, sizeof(iOptval));
if (res == SOCKET_ERROR) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), TRUE);
return 0;
}
SOCKADDR_IN saLocal;
int iFamily = AF_UNSPEC;
saLocal.sin_family = (ADDRESS_FAMILY)iFamily;
saLocal.sin_port = htons((u_short)IP_PORT);
saLocal.sin_addr.s_addr = htonl(INADDR_ANY);
res = bind(sock,
(sockaddr*)&saLocal,
sizeof(saLocal));
if (res == SOCKET_ERROR) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
closesocket(sock);
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), TRUE);
return 0;
}
res = listen(sock, CONNECTION_QUEUE_SIZE);
if (res == SOCKET_ERROR) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
closesocket(sock);
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), TRUE);
return 0;
}
while (true) {
int iAddrSize = sizeof(sockaddr_in);
ClientInfo clientinfo;
clientinfo.socket = accept(
sock,
(sockaddr*)&(clientinfo.addr),
&iAddrSize);
if (clientinfo.socket == INVALID_SOCKET) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
break;
}
TCHAR str[30];
wsprintf(str, L"The client joined");
SetWindowText(ProcessManagerServerDlg::ptr->hDialog, str);
HANDLE hThread = CreateThread(0, 0, ThreadForReceive, (LPVOID)&clientinfo, 0, 0);
CloseHandle(hThread);
}
closesocket(sock);
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), true);
return 0;
}
А вот при этой (код ниже), ошибка 10013 (Сделана попытка доступа к сокету методом, запрещенным правами доступа (даже если запускаю .exe с правами администратора).
DWORD WINAPI ThreadForAccept(LPVOID lpParam)
{
SOCKET sock = socket(AF_INET,
SOCK_STREAM,
IPPROTO_TCP);
if (sock == SOCKET_ERROR) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), TRUE);
return 0;
}
sockaddr_in local;
local.sin_addr.S_un.S_addr = INADDR_ANY;
local.sin_family = AF_INET;
local.sin_port = htons(IP_PORT);
res = bind(sock,
(sockaddr*)&local,
sizeof(local));
if (res == SOCKET_ERROR) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
closesocket(sock);
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), TRUE);
return 0;
}
res = listen(sock, CONNECTION_QUEUE_SIZE);
if (res == SOCKET_ERROR) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
closesocket(sock);
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), TRUE);
return 0;
}
while (true) {
int iAddrSize = sizeof(sockaddr_in);
ClientInfo clientinfo;
clientinfo.socket = accept(
sock,
(sockaddr*)&(clientinfo.addr),
&iAddrSize);
if (clientinfo.socket == INVALID_SOCKET) {
ProcessManagerServerDlg::ptr->MessageAboutError(WSAGetLastError());
break;
}
TCHAR str[30];
wsprintf(str, L"The client joined");
SetWindowText(ProcessManagerServerDlg::ptr->hDialog, str);
HANDLE hThread = CreateThread(0, 0, ThreadForReceive, (LPVOID)&clientinfo, 0, 0);
CloseHandle(hThread);
}
closesocket(sock);
WSACleanup();
EnableWindow(GetDlgItem(ProcessManagerServerDlg::ptr->hDialog, IDC_LAUNCH), true);
return 0;
}
Можете пожалуйста помочь решить эту проблему?