-
Notifications
You must be signed in to change notification settings - Fork 5
/
file_client.cpp
77 lines (63 loc) · 2.08 KB
/
file_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
#include<iostream>
#include<fstream>
#include<stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
class Client_socket{
fstream file;
int PORT;
int general_socket_descriptor;
struct sockaddr_in address;
int address_length;
public:
Client_socket(){
create_socket();
PORT = 8050;
address.sin_family = AF_INET;
address.sin_port = htons( PORT );
address_length = sizeof(address);
if(inet_pton(AF_INET, "127.0.0.1", &address.sin_addr)<=0) {
cout<<"[ERROR] : Invalid address\n";
}
create_connection();
file.open(".//Data//Client//client_text.txt", ios::out | ios::trunc | ios::binary);
if(file.is_open()){
cout<<"[LOG] : File Creted.\n";
}
else{
cout<<"[ERROR] : File creation failed, Exititng.\n";
exit(EXIT_FAILURE);
}
}
void create_socket(){
if ((general_socket_descriptor = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("[ERROR] : Socket failed.\n");
exit(EXIT_FAILURE);
}
cout<<"[LOG] : Socket Created Successfully.\n";
}
void create_connection(){
if (connect(general_socket_descriptor, (struct sockaddr *)&address, sizeof(address)) < 0) {
perror("[ERROR] : connection attempt failed.\n");
exit(EXIT_FAILURE);
}
cout<<"[LOG] : Connection Successfull.\n";
}
void receive_file(){
char buffer[1024] = {};
int valread = read(general_socket_descriptor , buffer, 1024);
cout<<"[LOG] : Data received "<<valread<<" bytes\n";
cout<<"[LOG] : Saving data to file.\n";
file<<buffer;
cout<<"[LOG] : File Saved.\n";
}
};
int main(){
Client_socket C;
C.receive_file();
return 0;
}