Делаю пробный клиент/серверный проект на TCP протоколе. Не получается подключиться к серверу. WSAGetLastError выдаёт такую ошибку:
Попытка установить соединение была безуспешной, т.к. от другого компьютера за требуемое время не получен нужный отклик, или было разорвано уже установленное соединение из-за неверного отклика уже подключенного компьютера.
Сервер:
void NetworkInitialization(CMaybeChatDlg* pDlg)
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) {
pDlg->m_SystemInfo.ReplaceSel(L"Initialization failed\r\n");
WSACleanup();
}
else pDlg->m_SystemInfo.ReplaceSel(L"Initalization success\r\n");
TcpNP->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (TcpNP->socket == INVALID_SOCKET) {
pDlg->m_SystemInfo.ReplaceSel(L"Creae socket failed\r\n");
WSACleanup();
}
else pDlg->m_SystemInfo.ReplaceSel(L"Creae socket success\r\n");
TcpNP->addr.sin_port = htons(8523);
TcpNP->addr.sin_addr.s_addr = INADDR_ANY;
TcpNP->addr.sin_family = AF_INET;
if (bind(TcpNP->socket, (sockaddr*)&TcpNP->addr,
sizeof(TcpNP->addr))==SOCKET_ERROR) {
pDlg->m_SystemInfo.ReplaceSel(L"Bind socket error\r\n");
closesocket(TcpNP->socket);
int n = WSAGetLastError();
WSACleanup();
}
else pDlg->m_SystemInfo.ReplaceSel(L"Bind socket csuccess\r\n");
if (listen(TcpNP->socket, SOMAXCONN) == SOCKET_ERROR) {
pDlg->m_SystemInfo.ReplaceSel(L"Set listen error\r\n");
closesocket(TcpNP->socket);
int n = WSAGetLastError();
WSACleanup();
}
else pDlg->m_SystemInfo.ReplaceSel(L"Server wait connections\r\n");
struct ThreadParams {
CMaybeChatDlg* pDlg;
};
ThreadParams* TP = new ThreadParams();
TP->pDlg = pDlg;
CWinThread* AcceptThread = AfxBeginThread((AFX_THREADPROC)AcceptClients, pDlg);
AcceptThread->ResumeThread();
}
void AcceptClients(CMaybeChatDlg* pDlg) {
CWinThread* WaitThread = AfxBeginThread((AFX_THREADPROC)WaitClient, pDlg);
WaitThread->ResumeThread();
int addrLen = sizeof(TcpNP->addr);
SOCKET clientSocket;
while (true) {
clientSocket = accept(TcpNP->socket, (sockaddr*)&TcpNP->addr,&addrLen);
if (clientSocket == INVALID_SOCKET) {
pDlg->m_SystemInfo.ReplaceSel(L"Accept client error\r\n");
closesocket(TcpNP->socket);
WSACleanup();
}
else {
pDlg->m_SystemInfo.ReplaceSel(L"Clent accepted\r\n");
pDlg->m_WaitIndikator.ReplaceSel(L"Connected\r\n");
wait = 7;
}
}
}
void WaitClient(CMaybeChatDlg* pDlg) {
while (wait != 7) {
Sleep(250); pDlg->m_WaitIndikator.SetWindowText(L"Wait\r\n");
Sleep(250); pDlg->m_WaitIndikator.SetWindowText(L"Wait.\r\n");
Sleep(250); pDlg->m_WaitIndikator.SetWindowText(L"Wait..\r\n");
Sleep(250); pDlg->m_WaitIndikator.SetWindowText(L"Wait...\r\n");
}
pDlg->m_WaitIndikator.SetWindowText(L"Waited!\r\n");
ExitThread(0);
}
Клиент:
NetworkParams* NP = new NetworkParams;
void NetworkInitialization(CChaChaChatDlg* pDlg)
{
WSADATA wsadata;
if (WSAStartup(MAKEWORD(2, 2), &wsadata) != 0) {
pDlg->m_SystemInfo.ReplaceSel(L"Initialization failed\r\n");
WSACleanup();
}
else pDlg->m_SystemInfo.ReplaceSel(L"Initalization success\r\n");
NP->socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (NP->socket == INVALID_SOCKET) {
pDlg->m_SystemInfo.ReplaceSel(L"Create socket failed\r\n");
WSACleanup();
}
else pDlg->m_SystemInfo.ReplaceSel(L"Create socket success\r\n");
NP->addr.sin_port = htons(8523);
NP->addr.sin_addr.s_addr = inet_pton(AF_INET,"127.0.0.1", &NP->addr);
NP->addr.sin_family = AF_INET;
int errorCode = WSAGetLastError();
LPWSTR errorBuffer = nullptr;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&errorBuffer, 0, nullptr);
if (errorBuffer != nullptr) {
CString errorMessage(errorBuffer);
pDlg->m_SystemInfo.ReplaceSel(errorMessage);
}
if (connect(NP->socket, (sockaddr*)&NP->addr, sizeof(NP->addr))==SOCKET_ERROR) {
int errorCode = WSAGetLastError();
LPWSTR errorBuffer = nullptr;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
errorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPWSTR)&errorBuffer, 0, nullptr);
if (errorBuffer != nullptr) {
CString errorMessage(errorBuffer);
pDlg->m_SystemInfo.ReplaceSel(errorMessage);
}
} else pDlg->m_SystemInfo.ReplaceSel(L"Connection success");
}
NP->addr.sin_addr.s_addr = inet_pton(AF_INET,"127.0.0.1", &NP->addr);
портит память и переписывает структуруNP->addr
. Эта функция возвращает 1 или 0. И присваивание вs_addr
не имеет смысла. Скорее всего нужно так :unsigned char buf[sizeof(struct in6_addr)]; if (inet_pton(AF_INET,"127.0.0.1", &buf))...; else error ;