Skip to content

Commit 5551c30

Browse files
author
Leo
committed
+ srv can now add and remove chatrooms
1 parent 1b6bafd commit 5551c30

File tree

4 files changed

+102
-7
lines changed

4 files changed

+102
-7
lines changed

Chatclient/src/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@
66
Client client;
77
Interface gui;
88

9-
109
static std::string rcvMsg;
1110
static bool rcvdMsg = false;
1211

1312
static std::string stopOptimizeThisWhileLoop;
1413

1514
//other Threads task
16-
void waitingForMsg(Client client)
15+
void waitingForMsg(Client& client)
1716
{
1817
while (true)
1918
{

Chatserver/src/Server.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,41 @@ void Server::addCr(int count)
126126
for (int i = 0; i < count; i++)
127127
{
128128
m_Chatrooms.push_back(new Chatroom(m_Chatrooms.size()));
129-
//send client chatroomchoices
130129
}
131-
for(SOCKET client : m_Clients)
130+
for (SOCKET client : m_Clients)
131+
{
132132
sendMsgTo(client, "Chatroom:" + std::to_string(m_Chatrooms.size())); //send new Chatroomcount to all connected clients
133+
}
134+
}
135+
136+
void Server::removeCr(int count)
137+
{
138+
if (m_Chatrooms.size() >= count)
139+
{
140+
int oldSize = m_Chatrooms.size();
141+
for (int i = 1; i <= count; i++)
142+
{
143+
auto clientsOnCr = m_Chatrooms[oldSize - i]->getClients();
144+
for(SOCKET client : clientsOnCr)
145+
sendMsgTo(client, "Server: Chatroom " + std::to_string(oldSize - i) + " got closed"); //send disconnect
146+
std::cout << "Chatroom " + std::to_string(oldSize - i) + " got closed" << std::endl;
147+
m_Chatrooms.erase(m_Chatrooms.begin() + oldSize - i);
148+
}
149+
for (SOCKET client : m_Clients)
150+
{
151+
sendMsgTo(client, "Chatroom:" + std::to_string(m_Chatrooms.size())); //send new Chatroomcount to all connected clients
152+
153+
}
154+
}
155+
else
156+
{
157+
std::cout << "There are only " + std::to_string(m_Chatrooms.size()) + " Chatrooms left" << std::endl;
158+
}
159+
}
160+
161+
int Server::getCrCount()
162+
{
163+
return m_Chatrooms.size();
133164
}
134165

135166
bool Server::sendMsgCr() //sends rcv message to other clients

Chatserver/src/Server.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Server
2424
bool sendMsgTo(SOCKET s, std::string msg);
2525

2626
void addCr(int count);
27+
void removeCr(int count);
28+
int getCrCount();
2729
bool sendMsgCr();
2830

2931
std::string getMessage();

Chatserver/src/main.cpp

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,75 @@
1-
#include <iostream>
21
#include "Server.h"
32

3+
#include <thread>
4+
5+
6+
//other Threads task
7+
void waitingForUserInput(Server& srv)
8+
{
9+
std::string userInput;
10+
while (true)
11+
{
12+
std::getline(std::cin, userInput);
13+
if (userInput.substr(0, 4) == "/add")
14+
{
15+
srv.addCr(std::stoi(userInput.substr(5, 1)));
16+
std::cout << "Chatrooms: " + std::to_string(srv.getCrCount()) << std::endl;
17+
}
18+
else if (userInput.substr(0, 7) == "/remove")
19+
{
20+
srv.removeCr(std::stoi(userInput.substr(8, 1)));
21+
std::cout << "Chatrooms: " + std::to_string(srv.getCrCount()) << std::endl;
22+
}
23+
else
24+
{
25+
std::cout << "You can only add a chatroom with /add and remove a chatroom with /remove" << std::endl;
26+
}
27+
}
28+
}
29+
430
int main()
531
{
6-
Server srv("192.168.1.4", 54000);
32+
std::string serverIp;
33+
std::string serverPort;
34+
35+
bool bindSrv = false;
36+
while (!bindSrv)
37+
{
38+
char tmpBuf[4096];
39+
std::cout << "Type in the network ip of your device to bind the srv to that ip. The clients can than connect to this ip." << std::endl;
40+
std::cout << "Server-IP: ";
41+
std::getline(std::cin, serverIp);
42+
if (inet_pton(AF_INET, serverIp.c_str(), tmpBuf) <= 0)
43+
{
44+
std::cout << "Thats not a valid ip address" << std::endl;
45+
bindSrv = false;
46+
continue;
47+
}
48+
else
49+
bindSrv = true;
50+
std::cout << "Type in a portnumber between 49152 - 65535. The clients can than connect to this port." << std::endl;
51+
std::cout << "Server-Port: ";
52+
std::getline(std::cin, serverPort);
53+
int srvPort = std::stoi(serverPort);
54+
if (srvPort == -1 && srvPort >= 49152 && srvPort <= 65535)
55+
{
56+
std::cout << "Thats not a valid port" << std::endl;
57+
bindSrv = false;
58+
continue;
59+
}
60+
else
61+
bindSrv = true;
62+
}
63+
64+
65+
Server srv(serverIp, std::stoi(serverPort));
766

867
if (!srv.init())
968
std::cout << "Couldnt Init Winsock" << std::endl;
1069

11-
srv.addCr(5);
70+
std::cout << "You can add a chatroom with /add and remove a chatroom with /remove" << std::endl;
71+
std::thread userInputWorker = std::thread(waitingForUserInput, std::ref(srv));
72+
1273
if (srv.createListeningSocket())
1374
{
1475
while (true)
@@ -23,5 +84,7 @@ int main()
2384
}
2485

2586
std::cin.get();
87+
userInputWorker.detach();
88+
srv.cleanUp();
2689
return 0;
2790
}

0 commit comments

Comments
 (0)