-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.cpp
More file actions
147 lines (122 loc) · 3.76 KB
/
client.cpp
File metadata and controls
147 lines (122 loc) · 3.76 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
#include <cstring>
#include <iostream>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fstream>
#include <unistd.h>
using namespace std;
int main(int argc, char *argv[])
{
int buffer_size = 1024;
char client_password[buffer_size];
char ip_addr[] = "127.0.0.1";
// args: ip_addr, file, password
if (argc > 1)
{
if (argv[1] == std::string("--help") || argv[1] == std::string("-h"))
{
cout << argv[0] << " <password>" << " <filename>" << endl;
return 0;
}
strncpy(client_password, argv[1], buffer_size);
} /* else {
cout << argv[0] << " <password>" << " <filename>" << endl;
return 0;
} */
// connection
int clientSocket = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in serverAddress;
serverAddress.sin_family = AF_INET;
serverAddress.sin_port = htons(9999);
// serverAddress.sin_addr.s_addr = INADDR_ANY;
// Convert IPv4 and IPv6 addresses from text to binary form
if (inet_pton(AF_INET, ip_addr, &serverAddress.sin_addr) <= 0)
{
perror("Invalid address/Address not supported \n");
exit(EXIT_FAILURE);
}
if (connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)))
{
perror("Connection Failed \n");
exit(EXIT_FAILURE);
}
// comunication
char server_message[buffer_size];
if (recv(clientSocket, server_message, sizeof(server_message), 0) <= 0)
{
perror("recv failed or connection closed");
close(clientSocket);
exit(EXIT_FAILURE);
};
cout << server_message;
// clear buffer
memset(server_message, 0, sizeof(server_message));
// client password
if (argc == 1)
{
read(0, client_password, buffer_size);
}
if (send(clientSocket, client_password, strlen(client_password), 0) < 0)
{
perror("send failed");
close(clientSocket);
exit(EXIT_FAILURE);
}
if (recv(clientSocket, server_message, sizeof(server_message), 0) <= 0)
{
perror("recv failed or connection closed");
close(clientSocket);
exit(EXIT_FAILURE);
};
cout << server_message;
if (string(server_message) == "Wrong password!")
{
return 0;
}
else
{
if (argc <= 2)
{
char command[buffer_size];
while (1)
{
memset(command, 0, sizeof(command));
read(0, command, buffer_size);
send(clientSocket, command, strlen(command), 0);
memset(server_message, 0, sizeof(server_message));
if (std::string(command) == "bye" || std::string(command) == "bye\n")
{
break;
}
recv(clientSocket, server_message, sizeof(server_message), 0);
cout << server_message << endl;
}
}
else
{
ifstream file(argv[2]);
string line;
while (getline(file, line))
{
if (send(clientSocket, line.c_str(), line.length(), 0) < 0)
{
perror("send failed");
close(clientSocket);
exit(EXIT_FAILURE);
}
memset(server_message, 0, sizeof(server_message));
if (recv(clientSocket, server_message, sizeof(server_message), 0) <= 0)
{
perror("Command-file recv failed or connection closed");
close(clientSocket);
exit(EXIT_FAILURE);
}
cout << server_message << endl;
}
file.close();
}
}
close(clientSocket);
return 0;
}