Skip to content

Commit 8493048

Browse files
authored
Add files via upload
1 parent 0452010 commit 8493048

File tree

3 files changed

+176
-0
lines changed

3 files changed

+176
-0
lines changed

Makefile

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
all: read server client
2+
3+
read: readChunk.cpp
4+
g++ -o read readChunk.cpp
5+
6+
server: server.cpp
7+
g++ -o server server.cpp
8+
9+
client: client.cpp
10+
g++ -o client client.cpp
11+
12+

client.cpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include<fstream>
2+
#include<iostream>
3+
#include<string.h>
4+
#include<stdio.h>
5+
6+
#include <arpa/inet.h>
7+
#include <netdb.h>
8+
#include <netinet/in.h>
9+
#include <stdlib.h>
10+
#include <sys/socket.h>
11+
#include <sys/types.h>
12+
#include <unistd.h>
13+
14+
#define CHUNK_SIZE 64000
15+
#define PORT 3456
16+
#define SERVER_IP "127.0.0.1"
17+
18+
using namespace std;
19+
20+
void sendFile(int sockfd, string fileName);
21+
22+
int main(){
23+
int sockfd, connfd;
24+
struct sockaddr_in servaddr, cli;
25+
string fileName;
26+
27+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
28+
29+
bzero(&servaddr, sizeof(servaddr));
30+
servaddr.sin_family = AF_INET;
31+
servaddr.sin_addr.s_addr = inet_addr(SERVER_IP);
32+
servaddr.sin_port = htons(PORT);
33+
34+
if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr))!= 0) {
35+
printf("connection with the server failed...\n");
36+
exit(0);
37+
}
38+
cout<<"Enter File Name: ";
39+
cin>>fileName;
40+
41+
sendFile(sockfd, fileName);
42+
// close the socket
43+
close(sockfd);
44+
45+
}
46+
47+
void sendFile(int sockfd, string fileName){
48+
int fileSize;
49+
int n_chunks;
50+
int n_bytes;
51+
int offset=0;
52+
int count=0;
53+
char buffer[CHUNK_SIZE];
54+
55+
fstream f(fileName, ios::in|ios::binary);
56+
57+
//get file size
58+
f.seekg(0, ios::end);
59+
fileSize=f.tellg();
60+
f.seekg(0, ios::beg);
61+
n_chunks=fileSize/CHUNK_SIZE+1;
62+
63+
//send file size
64+
send(sockfd, &fileSize, sizeof(fileSize), 0);
65+
66+
n_chunks=fileSize/CHUNK_SIZE+1;
67+
int size_to_read;
68+
for(int i=0;i<n_chunks;i++){
69+
offset=0;
70+
count=0;
71+
if (i!=n_chunks-1)
72+
size_to_read=CHUNK_SIZE;
73+
else
74+
size_to_read=fileSize%CHUNK_SIZE;
75+
f.read(buffer, size_to_read);
76+
n_bytes=size_to_read;
77+
while (n_bytes>0){
78+
count=send(sockfd, buffer+offset, n_bytes, 0);
79+
if (count>0){
80+
offset+=count;
81+
n_bytes-=count;
82+
//cout<<"Buffer Sent";
83+
}
84+
}
85+
}
86+
87+
f.close();
88+
//sleep(5);
89+
recv(sockfd, buffer, 2, 0);
90+
}

server.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include<fstream>
2+
#include<iostream>
3+
#include<string.h>
4+
#include <stdlib.h>
5+
6+
7+
#include <netdb.h>
8+
#include <netinet/in.h>
9+
#include <sys/socket.h>
10+
#include <sys/types.h>
11+
#include <unistd.h>
12+
13+
14+
15+
16+
#define CHUNK_SIZE 64000
17+
#define PORT 3456
18+
19+
using namespace std;
20+
21+
void recvFile(int cSpec, string filename);
22+
23+
int main(){
24+
int sockfd, cSpec, len;
25+
struct sockaddr_in servaddr, cli;
26+
string fileName="received.pdf";
27+
28+
29+
sockfd = socket(AF_INET, SOCK_STREAM, 0);
30+
len = sizeof(cli);
31+
32+
bzero(&servaddr, sizeof(servaddr));
33+
servaddr.sin_family = AF_INET;
34+
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
35+
servaddr.sin_port = htons(PORT);
36+
bind(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr));
37+
listen(sockfd, 1);
38+
39+
cSpec = accept(sockfd, (struct sockaddr*)&cli, (socklen_t*)&len);
40+
recvFile(cSpec, fileName);
41+
close(sockfd);
42+
43+
}
44+
45+
void recvFile(int cSpec, string filename){
46+
char buffer[CHUNK_SIZE];
47+
int fileSize;
48+
char end_sp[]="ED";
49+
fstream fw(filename, ios::out);
50+
51+
bzero(buffer, CHUNK_SIZE);
52+
//get file size
53+
if (recv(cSpec, &fileSize, sizeof(fileSize), 0)<0){
54+
cout<<"ERROR"<<endl;
55+
}
56+
cout<<fileSize<<endl;
57+
58+
//receive file chunk by chunk
59+
int n_bytes=fileSize;
60+
int count;
61+
while(n_bytes>0){
62+
count=recv(cSpec, buffer, CHUNK_SIZE, 0);
63+
if (count>0){
64+
fw.write(buffer, count);
65+
n_bytes-=count;
66+
bzero(buffer, CHUNK_SIZE);
67+
68+
}
69+
}
70+
fw.close();
71+
send(cSpec, end_sp, 2, 0);
72+
73+
74+
}

0 commit comments

Comments
 (0)