Странная ошибка в WinSock C++
Решил написать простой клиент-серверный чат без GUI на C++ (основные библиотеки WinSock2 + thread + future) Изначальный код был взят откуда-то с Github и тщательно изучен. Я решил добавить в него асинхронности, и после исправления всех ошибок, появилась странная ошибка с повреждением переменной хранящей IP адрес. Судя по выводу, на клиенте появляется ошибка WinSock2 #10061, а на сервере ошибка #10022. Также появляется исключение "Run-Time Check Failure #2 - Stack around the variable 'SERVER_IP' was corrupted" и аналогичное только вместо 'SERVER_IP' 'IP_SERV'. Помогите пожалуйста разобраться! Код прилагаю. Server:
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <vector>
#include <future>
#include <thread>
#pragma comment(lib, "Ws2_32.lib")
using namespace std;
int main(void)
{
//Key constants
char IP_SERV[] = ""; // Enter local Server IP address
cout << "Enter your IP: ";
cin >> IP_SERV;
cout << endl;
int PORT_NUM = 0; // Enter Open working server port
cout << "Enter server port: ";
cin >> PORT_NUM;
const short BUFF_SIZE = 1024; // Maximum size of buffer for exchange info between server and client
// Key variables for all program
int erStat; // Keeps socket errors status
//IP in string format to numeric format for socket functions. Data is in "ip_to_num"
in_addr ip_to_num;
erStat = inet_pton(AF_INET, IP_SERV, &ip_to_num);
if (erStat <= 0) {
cout << "Error in IP translation to special numeric format" << endl;
return 1;
}
// WinSock initialization
WSADATA wsData;
erStat = WSAStartup(MAKEWORD(2, 2), &wsData);
if (erStat != 0) {
cout << "Error WinSock version initializaion #";
cout << WSAGetLastError();
return 1;
}
else
cout << "WinSock initialization is OK" << endl;
// Server socket initialization
SOCKET ServSock = socket(AF_INET, SOCK_STREAM, 0);
if (ServSock == INVALID_SOCKET) {
cout << "Error initialization socket # " << WSAGetLastError() << endl;
closesocket(ServSock);
WSACleanup();
return 1;
}
else
cout << "Server socket initialization is OK" << endl;
// Server socket binding
sockaddr_in servInfo;
ZeroMemory(&servInfo, sizeof(servInfo)); // Initializing servInfo structure
servInfo.sin_family = AF_INET;
servInfo.sin_addr = ip_to_num;
servInfo.sin_port = htons(PORT_NUM);
//erStat =
bind(ServSock, (sockaddr*)&servInfo, sizeof(servInfo));
/*if (erStat != 0) {
cout << "Error Socket binding to server info. Error # " << WSAGetLastError() << endl;
closesocket(ServSock);
WSACleanup();
return 1;
}
else*/
cout << "Binding socket to Server info is OK" << endl;
//Starting to listen to any Clients
erStat = listen(ServSock, SOMAXCONN);
if (erStat != 0) {
cout << "Can't start to listen to. Error # " << WSAGetLastError() << endl;
closesocket(ServSock);
WSACleanup();
return 1;
}
else {
cout << "Listening..." << endl;
}
//Client socket creation and acception in case of connection
sockaddr_in clientInfo;
ZeroMemory(&clientInfo, sizeof(clientInfo)); // Initializing clientInfo structure
int clientInfo_size = sizeof(clientInfo);
SOCKET ClientConn = accept(ServSock, (sockaddr*)&clientInfo, &clientInfo_size);
if (ClientConn == INVALID_SOCKET) {
cout << "Client detected, but can't connect to a client. Error # " << WSAGetLastError() << endl;
closesocket(ServSock);
closesocket(ClientConn);
WSACleanup();
return 1;
}
else {
cout << "Connection to a client established successfully" << endl;
char clientIP[22];
inet_ntop(AF_INET, &clientInfo.sin_addr, clientIP, INET_ADDRSTRLEN); // Convert connected client's IP to standard string format
cout << "Client connected with IP address " << clientIP << endl;
}
//Exchange text data between Server and Client. Disconnection if a client send "xxx"
//vector <char> servBuff(BUFF_SIZE), clientBuff(BUFF_SIZE); // Creation of buffers for sending and receiving data
char servBuff[BUFF_SIZE] = "";
char clientBuff[BUFF_SIZE] = "";
short packet_size = 0; // The size of sending / receiving packet in bytes
while (true) {
auto future1 = std::async(std::launch::async, [ClientConn, servBuff] {
int temp = 0;
temp = recv(ClientConn, (char*)servBuff, 1024, 0); // Receiving packet from client. Program is waiting (system pause) until receive
cout << "Client's message: " << (char*)servBuff << endl;
});
auto future2 = std::async(std::launch::async, [clientBuff, ClientConn, ServSock] {
int temp2 = 0;
cout << "Your (host) message: ";
fgets((char*)clientBuff, 1024, stdin);
// Check whether server would like to stop chatting
if (clientBuff[0] == 'x' && clientBuff[1] == 'x' && clientBuff[2] == 'x') {
shutdown(ClientConn, SD_BOTH);
closesocket(ServSock);
closesocket(ClientConn);
WSACleanup();
return 0;
}
temp2 = send(ClientConn, (char*)clientBuff, 1024, 0);
if (temp2 == SOCKET_ERROR) {
cout << "Can't send message to Client. Error # " << WSAGetLastError() << endl;
closesocket(ServSock);
closesocket(ClientConn);
WSACleanup();
return 1;
}
});
}
closesocket(ServSock);
closesocket(ClientConn);
WSACleanup();
return 0;
}
Client:
#include <iostream>
#include <WinSock2.h>
#include <WS2tcpip.h>
#include <inaddr.h>
#include <stdio.h>
#include <vector>
#include <future>
#include <thread>
#pragma comment(lib, "ws2_32.lib")
using namespace std;
int main(void)
{
//Key constants
char SERVER_IP[] = ""; // Enter IPv4 address of Server
cout << "Enter server IP, what you need to connect: ";
cin >> SERVER_IP;
short SERVER_PORT_NUM = 0; // Enter Listening port on Server side
cout << "Enter server port: ";
cin >> SERVER_PORT_NUM;
const short BUFF_SIZE = 1024; // Maximum size of buffer for exchange info between server and client
// Key variables for all program
int erStat; // For checking errors in sockets functions
//IP in string format to numeric format for socket functions. Data is in "ip_to_num"
in_addr ip_to_num;
inet_pton(AF_INET, SERVER_IP, &ip_to_num);
// WinSock initialization
WSADATA wsData;
erStat = WSAStartup(MAKEWORD(2, 2), &wsData);
if (erStat != 0) {
cout << "Error WinSock version initializaion #";
cout << WSAGetLastError();
return 1;
}
else
cout << "WinSock initialization is OK" << endl;
// Socket initialization
SOCKET ClientSock = socket(AF_INET, SOCK_STREAM, 0);
if (ClientSock == INVALID_SOCKET) {
cout << "Error initialization socket # " << WSAGetLastError() << endl;
closesocket(ClientSock);
WSACleanup();
}
else
cout << "Client socket initialization is OK" << endl;
// Establishing a connection to Server
sockaddr_in servInfo;
ZeroMemory(&servInfo, sizeof(servInfo));
servInfo.sin_family = AF_INET;
servInfo.sin_addr = ip_to_num;
servInfo.sin_port = htons(SERVER_PORT_NUM);
erStat = connect(ClientSock, (sockaddr*)&servInfo, sizeof(servInfo));
if (erStat != 0) {
cout << "Connection to Server is FAILED. Error # " << WSAGetLastError() << endl;
closesocket(ClientSock);
WSACleanup();
return 1;
}
else
cout << "Connection established SUCCESSFULLY. Ready to send a message to Server" << endl;
//Exchange text data between Server and Client. Disconnection if a Client send "xxx"
//vector <char> servBuff(BUFF_SIZE), clientBuff(BUFF_SIZE); // Buffers for sending and receiving data
char servBuff[BUFF_SIZE] = "";
char clientBuff[BUFF_SIZE] = "";
short packet_size = 0; // The size of sending / receiving packet in bytes
while (true) {
auto future2 = std::async(std::launch::async, [packet_size, clientBuff, ClientSock] {
int temp1 = 0;
cout << "Your (Client) message to Server: ";
fgets((char*)clientBuff, 1024, stdin);
// Check whether client like to stop chatting
if (clientBuff[0] == 'x' && clientBuff[1] == 'x' && clientBuff[2] == 'x') {
shutdown(ClientSock, SD_BOTH);
closesocket(ClientSock);
WSACleanup();
return 0;
}
temp1 = send(ClientSock, (char*)clientBuff, 1024, 0);
if (temp1 == SOCKET_ERROR) {
cout << "Can't send message to Server. Error # " << WSAGetLastError() << endl;
closesocket(ClientSock);
WSACleanup();
return 1;
}});
auto future1 = std::async(std::launch::async, [packet_size, ClientSock, servBuff] {
int temp = 0;
temp = recv(ClientSock, (char*)servBuff, 1024, 0);
if (temp == SOCKET_ERROR) {
cout << "Can't receive message from Server. Error # " << WSAGetLastError() << endl;
closesocket(ClientSock);
WSACleanup();
return 1;
}
else
cout << "Server message: " << (char*)servBuff << endl;
});
}
closesocket(ClientSock);
WSACleanup();
return 0;
}
P.S Извиняюсь за большой пост. P.P.S Просьба писать только решения или советы по решению проблемы.