-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathftpClient.cpp
More file actions
128 lines (81 loc) · 2.27 KB
/
ftpClient.cpp
File metadata and controls
128 lines (81 loc) · 2.27 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
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/socket.h>
#include <string>
#include <memory>
#include <cstdlib>
#include <sys/types.h>
#include <sstream>
#include <cstring>
#include <iostream>
int converse(int sockfd);
int acceptFile(int sockfd);
int main(){
struct addrinfo *adinfo, hints, *servinfo;
struct addrinfo *p;
std::stringstream port_str;
//funkcija koja dohvata povezanu listu struktura adresa na koje moze da
//se primeni bind, connect
//
//getaddrinfo() - ima nekoliko karakteristika kako se zove
int sockfd;
memset(&hints, 0, sizeof hints);
hints.ai_family=AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
//hints.ai_flags = AI_PASSIVE;
int rv = getaddrinfo("127.0.0.1", "23456", &hints, &servinfo);
std::cout << rv << '\n';
std::cout << sizeof(servinfo);
for(p=servinfo;p!=NULL;p=p->ai_next){
std::cout << (*p).ai_canonname;
std::cout << "Client:obilazak";
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
if(::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1){
perror("client:connect failed");
close(sockfd);
continue;
}
break;
}
const char *msg = "input";
send(sockfd, msg, strlen(msg), 0);
acceptFile(sockfd);
return 0;
}
int converse(int sockfd){
while(1){
std::cout << "Enter a message for server:\n";
std::string line;
getline(std::cin, line);
const char * buff = line.c_str();
if(send(sockfd, buff, strlen(buff), 0)>0){ //size_t 3 argument treba da dodje broj karaktera koji se salje ,
// a povratna vrednost je broj karaktera koji je stigao
if(strncmp(buff, "exit", 4) == 0){
std::cout << "exiting..."; return 0;
}
}
}
}
/* ovako izgleda kada prihvatamo l
*
* */
int acceptFile(int sockfd){
std::cout << "acceptFile()";
char buff[10];
printf("%li", sizeof buff);
bzero(buff, sizeof(buff));
FILE *fd;
fd = fopen("transfered.txt", "w");
while(recv(sockfd, buff, sizeof(buff),0)){
printf("%s", buff);
fputs(buff, fd);
bzero(buff, sizeof(buff)); //potrebno je pocistiti prostor, da tu legnu novi karakteri
memset((char*)buff, 0, sizeof(buff));
}
fclose(fd);
return 0;
}