Guys im creating a client - server communication with a simple chat server with local host now.
But when im loging in with the client the server stops working
also the client is like writing black lines all the time.
the code:
Client:
#include <iostream>
#include <Windows.h>
#include <WinSock.h>
#pragma comment (lib, "ws2_32")
using namespace std;
SOCKET sConnect;
SOCKADDR_IN addr;
int Startup_WinSock()
{
WSADATA wsaData;
WORD DllVersion = MAKEWORD(2, 1);
int Val = WSAStartup(DllVersion, &wsaData);
return Val;
}
int ClientThread()
{
char *Buffer = new char[256];
int size = 0;
while (true)
{
ZeroMemory(Buffer, 256);
if ((size = recv(sConnect, Buffer, 256, NULL)) > 0);
{
cout << Buffer << endl;
}
Sleep(50);
}
}
int main()
{
system("color 0a");
int Val = Startup_WinSock();
if (Val != 0)
{
cout << "Cant start WinSock" << endl;
exit(1);
}
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(2222);
addr.sin_family = AF_INET;
cout << "Please press [ENTER]" << endl;
cin.get();
Val = connect(sConnect, (SOCKADDR*)&addr, sizeof(addr));
if (Val != 0)
{
cout << "Cant login to the server" << endl;
main();
}
else
{
system("cls");
int ID;
char *nID = new char[64];
char *hello = new char[256];
ZeroMemory(hello, 256);
ZeroMemory(nID, 64);
recv(sConnect, nID, 64, NULL);
recv(sConnect, hello, 64, NULL);
ID = atoi(nID);
cout << hello << endl;
cout << "Your ID:" << " " << ID << endl;
cout << " If you are ready press [ENTER]" << endl;
cin.get();
system("cls");
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ClientThread, NULL, NULL, NULL);
while (true)
{
char *MSG = new char[256];
ZeroMemory(MSG, 256);
cin.getline(MSG, 256);
send(sConnect, MSG, 256, NULL);
Sleep(50);
}
}
return 0;
}
Server:
#include <iostream>
#include <Windows.h>
#include <WinSock.h>
using namespace std;
#pragma comment (lib, "ws2_32")
SOCKADDR_IN addr; // this saves the ip and port of the server
int addrlen = sizeof(addr);
int Counter; // this counts how many clients are connected in the server
SOCKET sConnect; // 1 socket for incoming connections
SOCKET sListen; // 1 socket for listening
SOCKET *Connections; // 1 which saves the connections
int InitWinSock()
{
int Val = 0;
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
Val = WSAStartup(DllVersion, &wsaData);
return Val;
}
int ServerThread(int ID)
{
char *Buffer = new char[256];
int size = 0;
while (true)
{
ZeroMemory(Buffer, 256);
for (int a = 0; a < Counter; a++)
{
if ((size = recv(Connections[ID], Buffer, 256, NULL)) > 0)
{if (Connections[a] == Connections[ID])
ZeroMemory(Buffer, 256);
printf(Buffer, "Client[%i]: %s", ID, Buffer);
cout << "New Message: " << Buffer << endl;
}
else {
ZeroMemory(Buffer, 256);
printf(Buffer, "Client[%i]: %s", ID, Buffer);
send(Connections[a], Buffer, 256, NULL);
}
}
}
}
int main ()
{
system("color 0a");
cout << "Server Started Successfully" << endl;
int Val = InitWinSock();
if (Val != 0)
{
MessageBoxA(NULL, "Error while attempting to load WinSock", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
Connections = (SOCKET*)calloc(64, sizeof(SOCKET));
sListen = socket(AF_INET, SOCK_STREAM, NULL);
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet_addr("127.0.0.1");
addr.sin_port = htons(2222);
addr.sin_family = AF_INET;
bind(sListen, (SOCKADDR*)&addr, sizeof(addr));
listen(sListen, 64);
while (true)
{
if (sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen))
{
Connections[Counter] = sConnect;
char *Name = new char[64];
ZeroMemory(Name, 64);
printf(Name, "%i", Counter);
send(Connections[Counter], Name, 64, NULL);
send(Connections[Counter], "Welcome To The Mysteries Of Genesys", 64, NULL);
cout << "New Connection" << endl;
Counter++;
CreateThread(NULL, NULL, NULL,(LPTHREAD_START_ROUTINE)ServerThread, NULL, NULL);
}
Sleep(50);
}
}
Could u help me? :3
Thx in advance
But if i use the one created some hours before:
#include <WinSock.h>
#include <iostream>
#include <Windows.h>
using namespace std;
#pragma comment (lib, "ws2_32.lib") //<-- we need this lib
SOCKADDR_IN addr; // this saves the ip and port of the server
int addrlen = sizeof(addr);
int Counter; // this counts how many clients are connected in the server
SOCKET sConnect; // 1 socket for incoming connections
SOCKET sListen; // 1 socket for listening
SOCKET *Connections; // 1 which saves the connections
int InitWinSock()
{
int Val = 0; // this will say to us when the action will fail
WSAData wsaData;
WORD DllVersion = MAKEWORD(2, 1);
Val = WSAStartup(DllVersion, &wsaData); //here the winsock will be initialized
return Val;
}
int ServerThread(int ID)
{
char *Buffer = new char[256];
int size = 0;
while (true)
{
ZeroMemory(Buffer, 256);
if ((size = recv(Connections[ID], Buffer, 256, NULL)) > 0); // the following steps will be followed only if the msg isnt empty
{
for (int a = 0; a < Counter; a++)
{
if (Connections[a] == Connections[ID])
{
ZeroMemory(Buffer, 256);
printf(Buffer, " Client[%i]: %s", ID, Buffer);
cout << Buffer << endl;
}
else
{
ZeroMemory(Buffer, 256);
printf(Buffer, " Client[%i]: %s", ID, Buffer);
send(Connections[a], Buffer, 256, NULL);
}
}
}
}
}
int main()
{
cout << "Server started" << endl;
system("Color 0a");
int Val = InitWinSock();
if (Val != 0)
{
MessageBoxA(NULL, " Error while attempting to Startup Winsock!", "Error", MB_OK | MB_ICONERROR);
exit(1);
}
Connections = (SOCKET*)calloc(64, sizeof(SOCKET));
sListen = socket(AF_INET, SOCK_STREAM, NULL);
sConnect = socket(AF_INET, SOCK_STREAM, NULL);
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // server ip
addr.sin_port = htons(2222); // server port
addr.sin_family = AF_INET; // this is the kind of connection
bind(sListen, (SOCKADDR*)&addr, sizeof(addr)); // first we bind the server to the ip and port
listen(sListen, 64);
while (true) // in this endless loop we search for connections
{
if (sConnect = accept(sListen, (SOCKADDR*)&addr, &addrlen)) // here the connection is accepted
{
Connections[Counter] = sConnect;
char *Name = new char[64]; // the name of the client
ZeroMemory(Name, 64); // we make the char empty
printf(Name, "%i", Counter);
send(Connections[Counter], Name, 64, NULL);
send(Connections[Counter], "Welcome", 64, NULL); // welcome msg
cout << "New Connection" << Counter << endl;
Counter++; // +1 client :D
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)ServerThread, (LPVOID)(Counter - 1), NULL, NULL);
}
Sleep(50); // the server waits for 50 miliseconds
}
}
is responding well, but the messages from the clients arent sent to the others also the client is like writing black lines all the time again