· 7 years ago · Feb 27, 2018, 08:42 PM
1#include<stdio.h>
2#include<stdlib.h>
3#include<unistd.h>
4#include<sys/socket.h>
5#include<netinet/in.h>
6#include<arpa/inet.h>
7#include"stdio.h"
8#include"stdlib.h"
9#include"sys/types.h"
10#include"sys/socket.h"
11#include"string.h"
12#include"netinet/in.h"
13#include"time.h"
14#include <arpa/inet.h>
15#include <unistd.h>
16
17int receive_image(int n,char *name,int secret_key);
18//#define PORT 2222
19#define BUF_SIZE 2000
20#define CLADDR_LEN 100
21
22//#define PORT 1234
23#define BKLOG 5
24
25int main(int argc,char *argv[])
26{
27 int server, client,ret;
28 int PORT;int x;int z,get, secret_key;
29
30 unsigned int srv_len, cl_len;
31 struct sockaddr_in srv_addr, cl_addr;
32 int count = 0;
33 char buffer[BUF_SIZE], alt[BUF_SIZE], ans[160];
34 char check2[]="No more FILES to be transferred";
35 char clientAddr[CLADDR_LEN];
36 char msg[]="There is an Error Opening the file!";
37 pid_t childpid;
38 char * Server_Address;
39
40 if (argc < 3)
41 {
42 printf("\n\t\tUsage: <./APPLICATION_NAME> < PORT_NO > < 3 Digit SECRET_KEY >\n");
43 exit(1);
44 }
45 //printf("%s",argv[1]);
46 /*Server_Address = argv[1];
47 PORT = atoi(argv[2]);
48 secret_key=atoi(argv[3]);
49 */
50
51 PORT = atoi(argv[1]);
52 secret_key=atoi(argv[2]);
53
54 server = socket(AF_INET, SOCK_STREAM, 0);
55
56 srv_addr.sin_family = AF_INET;
57 srv_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); // If a static address is given and provided HERE
58 //srv_addr.sin_addr.s_addr = inet_addr(Server_Address); // IF server's address is provided HERE
59 //srv_addr.sin_addr.s_addr = INADDR_ANY; // IF it PICKS up ITS own Address from the Machine
60 srv_addr.sin_port = htons(PORT);
61
62
63 srv_len = sizeof(srv_addr);
64
65 bind(server, (struct sockaddr *)&srv_addr, srv_len);
66
67 listen(server, BKLOG);
68
69
70
71
72 while(1) {
73
74 system("clear");
75 printf("\n\t\t######################################################\n");
76 printf("\n\t\t# The server is waiting for CONNECTION ... #\n");
77 printf("\n\t\t######################################################\n");
78
79 //create socket for accepting clients
80 cl_len = sizeof(cl_addr);
81 client = accept(server, (struct sockaddr *) &cl_addr, &cl_len);
82 inet_ntop(AF_INET, &(cl_addr.sin_addr), clientAddr, CLADDR_LEN);
83 printf("\n\n\t\tConnected to client ... %s\n",clientAddr);
84 childpid= fork();
85 if (childpid==0) {
86 //creating a child process to accept files
87
88 while(1){
89 memset(buffer, 0, BUF_SIZE);
90 ret = recvfrom(client, buffer, BUF_SIZE, 0, (struct sockaddr *) &cl_addr, &cl_len);
91 if(ret < 0)
92 {
93 printf("Error receiving data!\n");
94 exit(1);
95 }
96
97 //printf("\n\tReceived Message from IP %s: %s\n",clientAddr, buffer);
98
99 z=strcmp(buffer,check2);
100 if(z==0)
101 {
102 printf("\n\n\tServer waiting for connections.......\n");
103 exit(1);
104 }
105
106 else
107 {
108
109 receive_image(client,clientAddr,secret_key);
110 memset(buffer, 0, BUF_SIZE);
111
112 }
113
114 } // end of while(1)
115
116 } // end of if (childpid==0)
117
118 else
119 {
120 close (client);}
121 }
122
123 close(server);
124 return(0);
125 }
126
127
128
129
130 int receive_image(int socket,char *clientaddr, int secret_key)
131 { // Start function
132
133 long buffersize = 0, recv_size = 0,size, read_size, write_size, packet_index =1,stat,lim_size=80000,rem_size;
134 int x,i,length,j,no1,no2,no3,no4;
135 char imagearray[80000], imagearray_decrypted[80000], verify = '1';
136 char recv_name[2000];
137 char buffer[] = "Got it";
138 FILE *image;char play[]="vlc ";
139 char command[100];char ext[3];
140 pid_t childpid2;
141
142
143 //find the picture name
144 do{
145 stat = read(socket,recv_name, 2000);
146 }while(stat<0);
147 //concating the recived file name with the particular client address to avoid duplicate file name in the server
148 strcat(clientaddr,recv_name);
149 length=strlen(recv_name);
150 length=length-1;
151
152 //getting the format of the file to be recived
153 for(j=2;j>=0;j--)
154 {
155 ext[j]=recv_name[length];
156 length=length-1;
157 }
158 no1=strcmp("wmv",ext);
159 no2=strcmp("mp4",ext);
160 no3=strcmp("avi",ext);
161 no4=strcmp("ogg",ext);
162
163 /* PACKET RECEIVED SIZE and FILE NAME*/
164 //printf("Packet received.\n");
165 //printf("Packet size: %ld\n",stat);
166 //printf("File name: %s\n",recv_name);
167 //printf(" \n");
168 /* ---------------------------------*/
169
170
171 //Find the size of the image
172 do{
173 stat = read(socket, &size, sizeof(long));
174 }while(stat<0);
175
176
177 /* PACKET RECEIVED SIZE and FILE NAME*/
178 //printf("Packet received.\n");
179 //printf("Packet size: %ld\n",stat);
180 //printf("Image size: %ld\n",size);
181 //printf(" \n");
182 /* ---------------------------------*/
183
184
185
186
187
188 //Send our verification signal
189 do{
190 stat = write(socket, &buffer, sizeof(int));
191 }while(stat<0);
192
193 //printf("Reply sent\n");
194 //printf(" \n");
195
196 image = fopen(clientaddr, "wb");
197 fseek(image, 0, SEEK_SET);
198
199 if( image == NULL)
200 {
201 printf("Error has occurred. file could not be opened\n");
202 return -1;
203 }
204
205 //Loop while we have not received the entire file yet
206
207
208 int need_exit = 0;
209 struct timeval timeout = {864000,0};
210
211 fd_set fds;
212 int buffer_fd, buffer_out;
213 printf("receiving file");
214 rem_size=size;
215 while(recv_size <size) {
216
217
218 FD_ZERO(&fds);
219 FD_SET(socket,&fds);
220
221 buffer_fd = select(FD_SETSIZE,&fds,NULL,NULL,&timeout);
222
223 if (buffer_fd < 0)
224 printf("Error: Bad file descriptor set!\n");
225
226 if (buffer_fd == 0)
227 printf("Error: Buffer read timeout expired.\n");
228
229 if (buffer_fd > 0)
230 {
231 if(size<=lim_size)
232 {
233 do{
234 read_size=read(socket,imagearray,size);
235 }while(read_size<0);
236 }
237 else
238 {
239 if(rem_size<=lim_size){
240
241 do{
242 read_size = read(socket,imagearray, rem_size);
243 }while(read_size <0);
244
245 }
246 else{
247 do{
248 read_size = read(socket,imagearray, lim_size);
249 }while(read_size <0);}
250
251 }
252
253 //printf("Packet number received: %ld\n",packet_index);
254 //printf("Packet size: %ld\n",read_size);
255
256
257 //Write the currently read data into our image file
258
259 printf("\n\n +++++++++++ ENCRYPTED FILE ++++++++++++\n\n");
260 printf("%s", imagearray);
261 printf("\n\n +++++++++++ ENCRYPTED FILE ++++++++++++\n\n");
262
263 for(int init;init<read_size;init++)
264 {
265 imagearray_decrypted[init]=imagearray[init]-secret_key; // Subtracting the offset of secret key value from the received value to get the corresponding original ASCII
266 }
267
268 printf("\n\n +++++++++++ DECRYPTED FILE ++++++++++++\n\n");
269 printf("%s", imagearray_decrypted);
270 printf("\n\n +++++++++++ DECRYPTED FILE ++++++++++++\n\n");
271
272 //write_size = fwrite(imagearray,1,read_size, image);
273 write_size = fwrite(imagearray_decrypted,1,read_size, image);
274 //printf("Written file size: %ld\n",write_size);
275
276 if(read_size !=write_size) {
277 printf("error in read write\n"); }
278
279
280 //Increment the total number of bytes read
281 recv_size += read_size;
282 rem_size=(size-recv_size);
283
284 printf("\n\n\t\tTotal received file size: %ld Bytes\n",recv_size);
285 printf(" \n");
286 printf(" \n");
287 } // end of if (buffer_fd > 0
288
289 } // end of while(recv_size <size)
290
291 fclose(image);
292 printf("\n\n\t\tFile received Successfully!\n");
293
294 //if its a video file it should be played automatically
295 /*if(no1==0||no2==0||no3==0||no4==0)
296 {
297 childpid2=fork();
298 if(childpid2==0)
299 {
300 strcat(play,recv_name);
301 strcpy(command,play);
302 system(command);
303 exit(1);
304 }} */
305
306 return 1;
307 } // end of int receive_image(int socket,char *clientaddr)