-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_client_unix.cpp
More file actions
150 lines (131 loc) · 3.38 KB
/
main_client_unix.cpp
File metadata and controls
150 lines (131 loc) · 3.38 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <memory.h>
#include <netinet/in.h>
#include <unistd.h> // for close connection
#include <cstring>
#include <stdlib.h>
#include <arpa/inet.h>
#include <unistd.h>
class Client
{
public:
Client();
int connection_to_port(sockaddr_in & dest_addr);
bool change_port(sockaddr_in & dest_addr);
int get_port();
void starting_response();
void dialog_with_server(sockaddr_in & dest_addr);
~Client();
private:
size_t port;
char buff[1024];
int client_socket;
const char *serv_addr = "127.0.0.1";
};
Client::Client() : port(7300)
{
memset(buff, 0, sizeof(buff));
}
int Client::connection_to_port(sockaddr_in & dest_addr)
{
if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) //AF_INET - internet socket SOCK_STREAM - socket for TCP
{
//if error with socket
perror("Error with socket ");
}
std::cout << "Socket created " << client_socket << std::endl;
if(dest_addr.sin_addr.s_addr = inet_addr(serv_addr) == -1)
{
perror("Error with server adress");
}
if (connect(client_socket, (struct sockaddr*)&dest_addr, sizeof(dest_addr)) < 0)
{
//if err with connect
perror("COnnecting error ");
}
std::cout << "Connected" << std::endl;
return 0;
}
bool Client::change_port(sockaddr_in & dest_addr)
{
if (close(client_socket) == 0)
{
dest_addr.sin_port = htons(port);
sleep(1); //We should w8 before server will be able to accept clients
connection_to_port(dest_addr);
return true;
}
return false;
}
int Client::get_port()
{
return port;
}
void Client::starting_response()
{
if ((recv(client_socket, &buff[0], sizeof(buff), 0)) != -1)
{
std::cout << buff;
const char* response = "Successfully connected";
send(client_socket, response, sizeof(buff), 0);
}
}
void Client::dialog_with_server(sockaddr_in & dest_addr)
{
while ((recv(client_socket, &buff[0], sizeof(buff), 0)) != -1)
{
if (strncmp(buff, "#1port", 6) == 0)
{
port = atoi(buff + 6);
if (change_port(dest_addr) != false)
send(client_socket, "success", strlen("success"), 0);
else
send(client_socket, "failure", strlen("success"), 0);
}
else
{
std::cout << "\nServer: " << buff;
std::cout << "\nClient: ";
fgets(buff, 1024, stdin);
if (strncmp(buff, "quit", 4) == 0)
{
std::cout << "Exit";
close(client_socket);
exit(0);
}
if (strncmp(buff, "NewPort-", 8) == 0)
{
port = atoi(buff + 8);
char portmsg[11] = "#1port";
memcpy(portmsg + 6, buff + 8, 4);
send(client_socket, &portmsg[0], sizeof(portmsg), 0);
change_port(dest_addr);
}
else
send(client_socket, &buff[0], sizeof(buff), 0);
}
}
}
Client::~Client()
{
close(client_socket);
}
int main(int argc, char* argv[])
{
std::cout << "TCP Client" << std::endl;
Client client;
sockaddr_in dest_addr;
dest_addr.sin_family = AF_INET;
dest_addr.sin_port = htons(client.get_port());
if (!client.connection_to_port(dest_addr) == 0)
{
perror(" was the problem");
exit(1);
}
client.starting_response();
client.dialog_with_server(dest_addr);
return -1;
}