-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer1.cpp
156 lines (149 loc) · 4.32 KB
/
peer1.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include<unistd.h>
#include <string.h>
#include <iostream>
#include<pthread.h>
#define BUFF_SIZE 1024
#define trd 10
#define nm 100
using namespace std;
int port, port1;
int fl = 0;
string ip = "127.0.0.1";
void *fileRead(void *sock)
{
cout<<"inside the thread"<<endl;
int client_socket = *((int *)sock);
string filename = "rfile"+to_string(++fl);
FILE *fp = fopen(filename.c_str(), "wb");
char Buffer[BUFF_SIZE];
int file_size,n;
recv(client_socket, &file_size, sizeof(file_size), 0);
cout<<"file size received "<<file_size<<endl;
while ((n=recv(client_socket,Buffer,BUFF_SIZE,0))>0 && file_size>0)
{
fwrite(Buffer, sizeof(char), n, fp);
file_size = file_size - n;
}
cout<<"file is closed"<<endl;
fclose(fp);
close(client_socket);
pthread_exit(NULL);
}
void *fileSend(void *sock)
{
char Buffer[BUFF_SIZE];
int file_size,n,size;
cout<<"inside sending thread"<<endl;
int client_socket = *((int *)sock);
recv(client_socket, Buffer, BUFF_SIZE, 0);
FILE *fp = fopen(Buffer,"rb");
fseek (fp,0,SEEK_END);
size = ftell(fp);
rewind (fp);
send (client_socket,&size,sizeof(size), 0);
cout<<Buffer<<endl;
memset(Buffer,'\0', BUFF_SIZE);
while ((n= fread(Buffer,sizeof(char),BUFF_SIZE,fp)) > 0 && size > 0 )
{
send (client_socket,Buffer, n, 0);
size = size - n ;
}
cout<<"closing the file in sending theread"<<endl;
fclose(fp);
close(client_socket);
pthread_exit(NULL);
}
void *peerServer(void *sock)
{
int svr_socket;
pthread_t thread[trd];
char response[BUFF_SIZE];
//creating socket for the communication
svr_socket = socket(AF_INET, SOCK_STREAM, 0);
//structure for defining the connection attributes
struct sockaddr_in server;
//assigning type, port and address
server.sin_family = AF_INET;
server.sin_port = htons(port);
server.sin_addr.s_addr = inet_addr(ip.c_str());
int addrlen = sizeof(sockaddr);
//binding the address
bind(svr_socket, (struct sockaddr*) &server, sizeof(server));
//listening to respond
listen(svr_socket,trd);
int temp,i=0,sts;
string filename;
while(1)
{
//accepting request from clients
cout<<"server is listening"<<endl;
int client_socket = accept(svr_socket, (struct sockaddr *)&server, (socklen_t*)&addrlen);
sts = pthread_create(&thread[i], NULL, fileSend, &client_socket);
i++;
}
//closing the socket
close(svr_socket);
pthread_exit(NULL);
}
void *peerClient(void *args)
{
char *filepath = ((char *)args);
cout<<filepath<<endl;
int svr_socket;
//creating socket for the communication
svr_socket = socket(AF_INET, SOCK_STREAM, 0);
//structure for defining the connection attributes
struct sockaddr_in server;
//assigning values to the structure like type, port and address
server.sin_family = AF_INET;
server.sin_port = htons(port1);
server.sin_addr.s_addr = inet_addr(ip.c_str());
//connecting to the server
int status = connect(svr_socket, (struct sockaddr*) &server, sizeof(server));
if(status<0)
{
cout<<"Error in connection establishment "<<endl;
}
char response[BUFF_SIZE];
cout<<"starting communication from client thread"<<endl;
FILE *fp1 = fopen("received","wb");
send (svr_socket,filepath,sizeof(filepath), 0);
char Buffer[BUFF_SIZE] ;
int n;
int file_size;
recv(svr_socket, &file_size, sizeof(file_size), 0);
while ((n=recv(svr_socket,Buffer,BUFF_SIZE,0))>0 && file_size>0)
{
fwrite(Buffer, sizeof(char), n, fp1);
file_size = file_size - n;
}
cout<<"closing the file"<<endl;
fclose(fp1);
pthread_exit(NULL);
}
int main(int argc, char* argv[])
{
ip = argv[1];
port = stoi(argv[2]);
port1 = stoi(argv[3]);
string conf_file = argv[4];
string st;
pthread_t td1,td2;
char filepath[nm];
int status = pthread_create(&td1, NULL, peerServer,NULL);
while(1)
{
cout<<"Enter download "<<endl;
cin>>st;
if(st.compare("download") == 0)
{
cout<<"Enter filepath "<<endl;
cin>>filepath;
int cstatus = pthread_create(&td1, NULL, peerClient,&filepath);
}
}
return 0;
}