Recently I was making multi-threaded server application which is capable of connecting to multiple clients. But, when I ran the client application, some run-time library exception occurred with "Debug Assertion Failed" dialog. I am not able to find out the exact reason for this error. I am making use of Winsock API in Visual Studio Ultimate 2013. Can someone please help me to look out for a solution? Any help would be greatly appreciated!
What I have tried:
Client:
#include <windows.h>
#include <winsock.h>
#include <stdio.h>
#include <iostream>
#include <signal.h>
#include <stdio.h>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
#define SIGINT 2
#define SIGKILL 9
#define SIGQUIT 3
SOCKET sock, client;
void s_handle(int s)
{
if (sock)
closesocket(sock);
if (client)
closesocket(client);
WSACleanup();
Sleep(10);
cout << "EXIT SIGNAL :" << s;
exit(0);
}
void s_cl(char *a, int x)
{
cout << a;
s_handle(x + 1000);
}
int main()
{
HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hStdout, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
DWORD poll;
int res, i = 1, port = 999;
char buf[100];
char msg[100] = "";
char ip[15];
WSADATA data;
signal(SIGINT, s_handle);
signal(SIGKILL, s_handle);
signal(SIGQUIT, s_handle);
cout << "\t\tEcho This is a Client";
cout << "\n\n\n\t\tEnter IP to connect to: ";
gets(ip);
sockaddr_in ser;
sockaddr addr;
ser.sin_family = AF_INET;
ser.sin_port = htons(123);
ser.sin_addr.s_addr = inet_addr(ip);
memcpy(&addr, &ser, sizeof(SOCKADDR_IN));
res = WSAStartup(MAKEWORD(1, 1), &data);
cout << "\n\nWSAStartup"
<< "\nVersion: " << data.wVersion
<< "\nDescription: " << data.szDescription
<< "\nStatus: " << data.szSystemStatus << endl;
if (res != 0)
s_cl("WSAStarup failed", WSAGetLastError());
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (sock == INVALID_SOCKET)
s_cl("Invalid Socket ", WSAGetLastError());
else if (sock == SOCKET_ERROR)
s_cl("Socket Error)", WSAGetLastError());
else
cout << "Socket Established" << endl;
res = connect(sock, &addr, sizeof(addr));
if (res != 0)
{
s_cl("SERVER UNAVAILABLE", res);
}
else
{
cout << "\nConnected to Server: ";
memcpy(&ser, &addr, sizeof(SOCKADDR));
}
char RecvdData[100] = "";
int ret;
while (true)
{
strcpy(buf, "");
cout << "\nEnter message to send ->\n";
fgets(buf, sizeof(buf), stdin);
Sleep(5);
res = send(sock, buf, sizeof(buf), 0);
if (res == 0)
{
printf("\nSERVER terminated connection\n");
Sleep(40);
closesocket(client);
client = 0;
break;
}
else if (res == SOCKET_ERROR)
{
printf("Socket error\n");
Sleep(40);
s_handle(res);
break;
}
ret = recv(sock, RecvdData, sizeof(RecvdData), 0);
if (ret > 0)
{
cout << endl << RecvdData;
strcpy(RecvdData, "");
}
}
closesocket(client);
WSACleanup();
}
Server:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock.h>
#include <iostream>
using namespace std;
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Mswsock.lib")
#pragma comment (lib, "AdvApi32.lib")
DWORD WINAPI receive_cmds(LPVOID lpParam)
{
cout << "thread created\r\n";
SOCKET current_client = (SOCKET)lpParam;
char buf[100];
char sendData[100];
int res;
while (true)
{
res = recv(current_client, buf, sizeof(buf), 0);
Sleep(2);
if (res == 0)
{
closesocket(current_client);
ExitThread(0);
}
if (strstr(buf, "hello"))
{
cout << "\nrecived hello cmd";
strcpy(sendData, "hello, greetings from server\n");
Sleep(2);
send(current_client, sendData, sizeof(sendData), 0);
}
else if (strstr(buf, "bye"))
{
cout << "\nrecived bye cmd\n";
strcpy(sendData, "cya\n");
Sleep(2);
send(current_client, sendData, sizeof(sendData), 0);
closesocket(current_client);
ExitThread(0);
}
else
{
strcpy(sendData, "Invalid cmd\n");
Sleep(2);
send(current_client, sendData, sizeof(sendData), 0);
}
strcpy(sendData, "");
strcpy(buf, "");
}
}
int main()
{
printf("Starting up multi-threaded TCP server");
SOCKET sock;
DWORD thread;
WSADATA wsaData;
sockaddr_in server;
int ret = WSAStartup(0x101, &wsaData);
if (ret != 0)
{
return 0;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(123);
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == INVALID_SOCKET)
{
return 0;
}
if (bind(sock, (sockaddr*)&server, sizeof(server)) != 0)
{
return 0;
}
if (listen(sock, 5) != 0)
{
return 0;
}
SOCKET client;
sockaddr_in from;
int fromlen = sizeof(from);
while (true)
{
client = accept(sock, (struct sockaddr*)&from, &fromlen);
cout << "Client connected\r\n";
CreateThread(NULL, 0, receive_cmds, (LPVOID)client, 0, &thread);
}
closesocket(sock);
WSACleanup();
return 0;
}