-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
109 lines (92 loc) · 2.76 KB
/
server.cpp
File metadata and controls
109 lines (92 loc) · 2.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
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <cstdio>
#include "packet.h"
#define PORT 4000
#define MAXPAYLOAD 1500-4*sizeof(uint16_t)
int sendfile(std::string filename) {
char buffer[MAXPAYLOAD];
std::ifstream myfile (filename, std::ifstream::in);
int i = 0;
char *memblock;
double size;
int sockfd, n;
struct sockaddr_in cli_addr;
if (myfile.is_open())
{
myfile.seekg(0, std::ios::end);
auto filesize = (double)myfile.tellg();
myfile.seekg(0, std::ios::beg);
std::cout << filesize;
int n;
int ab = (int)filesize % MAXPAYLOAD;
while (filesize > MAXPAYLOAD)
{
myfile.read(buffer, MAXPAYLOAD);
std::cout << "\nREAD:" << strlen(buffer) << "\n"
<< "SENT:";
n = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&cli_addr, sizeof(struct sockaddr_in));
if (n < 0) printf("ERROR sendto");
printf("Got an ack: %s\n", buffer);
filesize = filesize - MAXPAYLOAD;
std::cout << n << "\n";
}
myfile.read(buffer, MAXPAYLOAD);
n = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&cli_addr, sizeof(struct sockaddr_in));
std::cout << "HI" << std::endl;
}
else
{
printf("\nFILE NOT OPENED \n");
n = sendto(sockfd, buffer, strlen(buffer), 0, (struct sockaddr *)&cli_addr, sizeof(struct sockaddr_in));
std::cout << "HI" << std::endl;
}
}
int receivefile(){
return 0;
}
int deletefile(std::string filename){
// testar se arquivo está aberto por algum processo/usuario
std::remove(filename.c_str());
return 0;
}
int main(int argc, char *argv[])
{
int sockfd, n;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
char buf[MAXPAYLOAD];
if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
printf("ERROR opening socket");
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
serv_addr.sin_addr.s_addr = INADDR_ANY;
bzero(&(serv_addr.sin_zero), 8);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(struct sockaddr)) < 0)
printf("ERROR on binding");
clilen = sizeof(struct sockaddr_in);
while (1) {
/* receive from socket */
n = recvfrom(sockfd, buf, 256, 0, (struct sockaddr *) &cli_addr, &clilen);
if (n < 0)
printf("ERROR on recvfrom");
printf("Received a datagram: %s\n", buf);
std::cout << "Datagram is of type : " <<
packet(buf).type << endl;
// fazer if para descobrir o que o client enviou
/* send to socket */
n = sendto(sockfd, "Got your message\n", 17, 0,(struct sockaddr *) &cli_addr, sizeof(struct sockaddr));
if (n < 0)
printf("ERROR on sendto");
}
close(sockfd);
return 0;
}