-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientS.cpp
More file actions
62 lines (49 loc) · 1.53 KB
/
ClientS.cpp
File metadata and controls
62 lines (49 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#pragma comment(lib, "ws2_32.lib") // обеспечивает доступ к некоторым функциям
#include <winsock2.h>
#include <string>
#pragma warning(disable: 4996)
SOCKET Connection;
void clientHandler() { // func to get serv message
//char msg[256];
int msgSize;
while (true) {
recv(Connection, (char*)&msgSize, sizeof(int), NULL);
char* msg = new char[msgSize + 1];
msg[msgSize] = '\0';
recv(Connection, msg, msgSize, NULL);
std::cout << msg << std::endl;
delete[] msg;
}
}
int main() {
WSAData wsaData;
WORD DLLVersion = MAKEWORD(2, 1);
if (WSAStartup(DLLVersion, &wsaData) != 0) {
std::cout << "Error" << std::endl;
exit(1);
}
//структура для хранения адреса
SOCKADDR_IN addr; // for internet protocol are used sockaddr_in
int sizeOfAddr = sizeof(addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // collect ip address
addr.sin_port = htons(1234);
Connection = socket(AF_INET, SOCK_STREAM, NULL);
if (connect(Connection, (SOCKADDR*)&addr, sizeof(addr)) != 0) {
std::cout << "Error connect";
return 1;
}
std::cout << "Connected" << std::endl;
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)clientHandler, NULL, NULL, NULL);
std::string msgClient;
while (true) {
std::getline(std::cin, msgClient);
int lenMsgClient = msgClient.size();
send(Connection, (char*)&lenMsgClient, sizeof(int), NULL);
send(Connection, msgClient.c_str(), lenMsgClient, NULL);
Sleep(100);
}
system("pause");
return 0;
}