-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.cpp
128 lines (127 loc) · 3.48 KB
/
client.cpp
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
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <errno.h>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <string>
#define SIZE 2000
struct sockaddr_in server;
int port, sd;
char msg[SIZE];
int main(int argc, char *argv[])
{
if(argc != 3)
{
printf("Sintax: %s <ip_address> <port>\n", argv[0]);
return -1;
}
port = atoi(argv[2]);
if((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("Eroare la socket().\n");
return errno;
}
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(argv[1]);
server.sin_port = htons(port);
if(connect(sd, (struct sockaddr *) &server, sizeof(struct sockaddr)) == -1)
{
perror("[client]Eroare la connect().\n");
return errno;
}
while(1)
{
printf("[client]$ ");
fflush(stdout);
bzero(&msg, sizeof(msg));
int lungime;
read(0, msg, sizeof(msg));
lungime = strlen(msg);
if(write(sd, &lungime, sizeof(lungime)) <= 0)
{
perror("[client]Eroare la write() catre server.\n");
return errno;
}
if(write(sd, msg, lungime) <= 0)
{
perror("[client]Eroare la write() catre server.\n");
return errno;
}
msg[strlen(msg) - 1] = '\0';
if(!strcmp(msg, "quit"))
{
printf("Quitting server.\n");
fflush(stdout);
exit(0);
}
if(read(sd, &lungime, sizeof(int)) < 0)
{
perror("[client]Eroare la read() de la server.\n");
return errno;
}
int totalCitit=0;
bzero(&msg, sizeof(msg));
while(lungime > totalCitit)
{
int citit = 0;
char aux[SIZE];
bzero(&aux, sizeof(aux));
if((citit = read(sd, aux, lungime)) < 0)
{
perror("[client]Eroare la read() de la server.\n");
return errno;
}
totalCitit += citit;
strcat(msg, aux);
}
if(msg[0] == 'h' && msg[1] == 't' && msg[2] == 't' && msg[3] == 'p')
{
char url[500];
unsigned int j = 0;
while(msg[j] != '\n')
{
url[j] = msg[j];
j++;
}
url[j] = '\0';
char aux[500];
strcpy(aux, msg + j + 1);
strcpy(msg, aux);
j = 0;
char nume_fisier[500];
while(msg[j] != '\n')
{
nume_fisier[j] = msg[j];
j++;
}
nume_fisier[j] = '\0';
strcpy(aux, msg + j + 1);
strcpy(msg, aux);
CURL* curl;
FILE* fp;
CURLcode res;
curl = curl_easy_init();
if (curl)
{
fp = fopen(nume_fisier, "wb");
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
res = curl_easy_perform(curl); /*it performs the transfer as described in the options*/
curl_easy_cleanup(curl); /*close all connections this handle has used*/
fclose(fp);
}
}
printf("[client] %s\n", msg);
fflush(stdout);
}
close(sd);
return 0;
}