· 7 years ago · Feb 27, 2018, 08:42 PM
1#include<stdio.h>
2#include<unistd.h>
3#include<stdlib.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#include<sys/ioctl.h>
17
18int send_image(int n, int secret_key);
19//#define PORT 1234
20#define BUF_SIZE 2000
21
22
23
24
25int main(int argc,char *argv[])
26 {
27 int client,x,fileLen,msg, secret_key;
28 unsigned int cl_len;
29 int PORT;
30 struct sockaddr_in cl_addr;
31 int ret;
32 char buffer2[BUF_SIZE];char *buffer;char data[]="More FILES need to be transferred";
33 char ans[BUF_SIZE];char data2[]="No more FILES to be transferred";
34 char msgserv[]="\n\t\t\tError opening file!";
35 FILE *file;
36 char * serverAddr;
37
38
39
40 if (argc < 4)
41 {
42 printf("\n\t\tUsage: <./APPLICATION_NAME> <SERVER's IP_ADDRESS> < SERVER's_PORT > <3 Digit SECRET_KEY>\n");
43 exit(1);
44 }
45
46 serverAddr = argv[1];
47 PORT = atoi(argv[2]);
48 secret_key = atoi(argv[3]);
49
50 //printf("\t\t\t%s and - PORT: %d -argc: %d \n", serverAddr,PORT,argc);
51
52 client = socket(AF_INET, SOCK_STREAM, 0);
53
54 cl_addr.sin_family = AF_INET;
55 cl_addr.sin_addr.s_addr = inet_addr(serverAddr);
56 cl_addr.sin_port = htons(PORT);
57
58 cl_len = sizeof(cl_addr);
59
60 ret = connect(client, (struct sockaddr *) &cl_addr, cl_len);
61
62 if(ret == -1)
63 {
64 perror("\n\n\n\t\t\t\ERROR!");
65 exit(1);
66 }
67 memset(buffer2, 0,2000);
68
69 system("clear");
70 printf("\n\n\n\t\t PRESS ENTER KEY to CONTINUE ...");
71
72
73 //send message
74 if(fgets(buffer2,2000, stdin) != NULL) {
75 ret = sendto(client, buffer2, 2000, 0, (struct sockaddr *) &cl_addr, sizeof(cl_addr));
76 if (ret < 0)
77 {
78 printf("Error sending data!\n\t-%s", buffer2);
79 }}
80 //send file
81 send_image(client,secret_key);
82
83 //ask for more file transfer
84 while(1)
85 {
86 printf("\n\n\tDo you want to continue with file transfer? [Yes/No] ::: ");
87 scanf("%s",ans);
88 x=strcmp(ans,"yes");
89 if(x==0)
90 {
91 //ret = connect(client, (struct sockaddr *) &cl_addr, cl_len);
92 /// encryption algo run on DATA here
93
94 ret = sendto(client, data, 2000, 0, (struct sockaddr *) &cl_addr, sizeof(cl_addr));
95 if (ret < 0)
96 {
97 printf("Error sending data!\n\t-%s",data );
98 exit(0);
99 }
100 else
101 {
102 //printf("Sending images.....................");
103 send_image(client,secret_key);
104
105 }
106
107 }
108
109 else
110 {
111 printf("\n\n\t Thank you for Transferring the file!\n\n");
112 ret = sendto(client, data2, 2000, 0, (struct sockaddr *) &cl_addr, sizeof(cl_addr));
113 break;
114
115 }
116
117 } // end of while
118
119 close(client);
120
121 return(0);
122}
123
124
125
126 int send_image(int socket, int secret_key)
127 {
128
129 FILE *picture;
130 long size, read_size=0,total_size=0,status;
131 long lim_size=10241;int x,i,j=0,count,y;
132 char send_buffer[10241], send_buffer_encrypted [10241], read_buffer[256];
133 char name[2000];char actual_name[2000];char name_recv[2000];char temp_name[2000];
134 char msgserv[2000];
135 int ASCII;
136
137 printf("\n\tEnter the file path to be transferred: ");
138 scanf("%s",name);
139 picture = fopen(name, "rb");
140 while(picture==NULL)
141 {
142 printf("File doesn't exist\n");
143 printf("Please enter valid name\n");
144 scanf("%s",name);
145 picture = fopen(name, "rb");
146 }
147
148 //printf("Picture/Video/File name:%s\n",name);
149 //printf("Sending Picture/Video/File .....................\n");
150 x=strlen(name);
151 //printf("String length: %d\n",x);
152 //printf("%c",name[x-1]);
153 x=x-1;
154 count=x-1;
155
156 for(i=0;i<=count;i++)
157 {
158 name_recv[i]=name[x];
159 x=x-1;
160 }
161 printf("%s",name_recv);
162
163 y=count;
164 for(i=0;i<=count;i++)
165 {
166 if(name_recv[i]=='/')
167 break;
168 else{
169 temp_name[i]=name_recv[i];
170
171 }
172 }
173 printf("%s",temp_name);
174 x=strlen(temp_name);
175 count=x-1;j=0;
176 for(i=count;i>=0;i--)
177 {
178 actual_name[j]=temp_name[i];
179 j++;
180 }
181 //printf("Getting Picture Size\n");
182
183 fseek(picture, 0, SEEK_END);
184 size = ftell(picture);
185 fseek(picture, 0, SEEK_SET);
186 printf("Total FILE size: %ld\n",size);
187 //send picture name
188 //printf("Sending picture name\n");
189 write (socket, &actual_name,2000);
190
191 //Send Picture Size
192 //printf("Sending Picture Size\n");
193 write(socket, (void *)&size, sizeof(long));
194
195 //Send Picture as Byte Array
196
197 do
198 { //Read while we get errors that are due to signals.
199 status=read(socket, &read_buffer , 255);
200 printf("Bytes read: %ld\n",status);
201 } while (status < 0);
202
203
204 //printf("Received data in socket\n");
205
206
207 //temp=(size/lim_size);
208 //printf("%ld",temp);
209 //count=temp;
210 //printf("%ld",count);
211
212 if(size>lim_size)
213 {
214
215 while(total_size!=size) {
216 //Read from the file into our send buffer
217 read_size = fread(send_buffer, 1, sizeof(send_buffer)-1, picture);
218
219 //printf("The content of file: %s",send_buffer);
220 //Send data through our socket
221 printf("\n\n =========== The content of file [START] ===========\n\n");
222 printf("%s",send_buffer);
223 printf("\n\n =========== The content of file [END]===========\n\n");
224
225
226 for(int init;init<read_size; init++)
227 {
228
229 ASCII=send_buffer[init];
230 send_buffer_encrypted[init]=send_buffer[init]+secret_key;
231 //printf("%d [%d] ", ASCII, ASCII+secret_key);
232 }
233 printf("\n\n ++++++++++++ ENCRYPTED FILE [START] +++++++++++\n\n");
234 printf("%s",send_buffer_encrypted);
235 printf("\n\n ++++++++++++ ENCRYPTED FILE [END] +++++++++++\n\n");
236
237 do{
238 status = write(socket, send_buffer,read_size);
239 }while (status < 0);
240 total_size+=read_size;
241 bzero(send_buffer, sizeof(send_buffer));
242 }
243 }
244 else
245 {
246 while(!feof(picture)) {
247 //Read from the file into our send buffer
248 read_size = fread(send_buffer, 1, sizeof(send_buffer)-1, picture);}
249 printf("\n\n =========== The content of file [START] ===========\n\n");
250 printf("%s",send_buffer);
251 printf("\n\n =========== The content of file [END] ===========\n\n");
252
253
254 for(int init;init<read_size; init++)
255 {
256
257 //ASCII=send_buffer[init];
258 send_buffer_encrypted[init]=send_buffer[init]+secret_key; // EXample: SIMPLY adding the KEY to the READ ASCII value of each character of the TEXT to Encrypt
259 //printf("%d [%d] ", ASCII, ASCII+secret_key);
260 }
261 printf("\n\n ++++++++++++ ENCRYPTED FILE [START] +++++++++++\n\n");
262 printf("%s",send_buffer_encrypted);
263 printf("\n\n ++++++++++++ ENCRYPTED FILE [END] +++++++++++\n\n");
264
265
266
267 //printf("The content of file: %s\n",send_buffer);
268 //Send data through our socket
269 do{
270 //status = write(socket, send_buffer, size);
271 status = write(socket, send_buffer_encrypted, size);
272 }while (status < 0);
273
274
275
276 //Zero out our send buffer
277 bzero(send_buffer, sizeof(send_buffer));
278 }
279 return 0;
280 }