-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.cpp
74 lines (54 loc) · 1.49 KB
/
server.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
#include<fstream>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#define CHUNK_SIZE 64000
#define PORT 3456
using namespace std;
void recvFile(int cSpec, string filename);
int main(){
int sockfd, cSpec, len;
struct sockaddr_in servaddr, cli;
string fileName="received.pdf";
sockfd = socket(AF_INET, SOCK_STREAM, 0);
len = sizeof(cli);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(PORT);
bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
listen(sockfd, 1);
cSpec = accept(sockfd, (struct sockaddr*)&cli, (socklen_t*)&len);
recvFile(cSpec, fileName);
close(sockfd);
}
void recvFile(int cSpec, string filename){
char buffer[CHUNK_SIZE];
int fileSize;
char end_sp[]="ED";
fstream fw(filename, ios::out);
bzero(buffer, CHUNK_SIZE);
//get file size
if (recv(cSpec, &fileSize, sizeof(fileSize), 0)<0){
cout<<"ERROR"<<endl;
}
cout<<fileSize<<endl;
//receive file chunk by chunk
int n_bytes=fileSize;
int count;
while(n_bytes>0){
count=recv(cSpec, buffer, CHUNK_SIZE, 0);
if (count>0){
fw.write(buffer, count);
n_bytes-=count;
bzero(buffer, CHUNK_SIZE);
}
}
fw.close();
send(cSpec, end_sp, 2, 0);
}