#include <WinSock2.h>
#include <iostream>
#include <vector>
#include <thread>
#pragma warning(disable:4996)
SOCKET s;
std::vector<SOCKET> client_sockv;
std::vector<SOCKADDR_IN> client_addrv;
void msgProc(char buf[20000]) { // "номер, вид, адресат, сообщение"
buf[19999] = '\n';
char number[10];
char type[10];
char recipient[10];
char msg[19993];
memset(number, 0, sizeof(number));
memset(type, 0, sizeof(type));
memset(recipient, 0, sizeof(recipient));
memset(msg, 0, sizeof(msg));
int i = 0;
int count = 0;
for (; (buf[i] != ' ') && (i < 10); i++) {
number[i] = buf[i];
count++;
}
i++;
int countsave = count;
for (; (buf[i] != ' ') && (i < 10); i++) {
type[i-countsave-1] = buf[i];
count++;
}
i++;
countsave = count;
for (; (buf[i] != ' ') && (i < 10); i++) {
recipient[i - countsave - 2] = buf[i];
count++;
}
i++;
countsave = count;
for (; buf[i] != '\n'; i++) {
msg[i-countsave-3] = buf[i];
}
std::cout << number << " " << type << " " << recipient << " " << msg << "\n";
if (!strcmp(type, "mail")) {
send(client_sockv[atoi(recipient)-1], buf, sizeof(buf), 0);
std::cout << buf; // 2 mail 1 privet!
}
}
void accept_connections() {
WSADATA ws;
WSAStartup(MAKEWORD(2, 2), &ws);
s = socket(AF_INET, SOCK_STREAM, 0);
SOCKADDR_IN sa;
memset(&sa, 0, sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(1234);
bind(s, (const sockaddr*)&sa, sizeof(sa));
listen(s, 100);
SOCKADDR_IN client_addr;
int client_addr_size = sizeof(SOCKADDR_IN);
int i = 0;
while (true) {
client_sockv.push_back(accept(s, (sockaddr*)&client_addr, &client_addr_size));
client_addrv.push_back(client_addr);
std::cout << "connect is OK! ip:port " << inet_ntoa(client_addrv[i].sin_addr) << ":" << ntohs(client_addrv[i].sin_port) << "\n";
char ichar[10];
memset(ichar, 0, sizeof(ichar));
itoa(i+1, ichar, 10);
send(client_sockv[i], ichar, sizeof(ichar), 0);
i++;
}
}
int main()
{
setlocale(LC_ALL, "");
std::thread connects(accept_connections);
char buf[20000];
memset(buf, 0, sizeof(buf));
while (1) {
for (int i = 0; i < client_addrv.size(); i++) {
if (recv(client_sockv[i], buf, sizeof(buf), 0) > 0) {
if(buf[0] != ' ')msgProc(buf);
memset(buf, 0, sizeof(buf));
}
}
}
}