-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.cpp
More file actions
69 lines (56 loc) · 1.46 KB
/
Copy pathClient.cpp
File metadata and controls
69 lines (56 loc) · 1.46 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
63
64
65
66
67
68
69
#include "Client.h"
Client::Client()
{
fd = socket(AF_INET,SOCK_STREAM,0);
adr={0};
adr.sin_family=AF_INET;
inet_pton(AF_INET,"127.0.0.1",&adr.sin_addr);
adr.sin_port=htons(80);
}
bool Client::ClientStart()
{
int res = connect(fd, (struct sockaddr *) &adr, sizeof adr);{
if(res==-1)
{
perror("Error connection");
exit(EXIT_FAILURE);
}
}
return true;
}
int Client::Handle(){
try
{
do
{
getline(std::cin,user_input); // считываем сообщение пользователя
if(user_input=="!exit") break; // условие выхода
int sendRes = send(fd, user_input.c_str(),user_input.size()+1,0); // отправляем сообщение на сервер
if(sendRes==-1)
{
std::cerr<<"sending error"<<std::endl;
continue;
}
memset(buf,0,1024); // чистим буфер
int Byte_recv=recv(fd,buf,1024,0); // принимаем ответ от сервера
std::cout<<"Server: "<<std::string(buf,Byte_recv)<<std::endl; // выводим ответ от сервера
}
while (true);
}
catch (std::exception &e)
{
std::cerr<<&e;
}
return 0;
}
void Client::ClientStop()
{
delete[] buf;
close(fd);
}
void Client::Run()
{
ClientStart();
Handle();
ClientStop();
}