@@ -71,7 +71,7 @@ Stages for Client
7171#include  <netdb.h> //For gethostbyname function 
7272
7373#define  MAX_FILE_NAME  255
74- #define  ALLOC_SLAB  1024 
74+ #define  EXT_SIZE   4 
7575
7676enum  errcodes  {
7777 SOCKET_FAIL  =  -1 ,
@@ -119,7 +119,9 @@ void error(char* msg, int errno) {
119119 return 0; 
120120}*/ 
121121
122- 
122+ /* Find the size of a file using fseek() and ftell() and then read 
123+  * the file in a buffer of size found. 
124+  */ 
123125int  read_image_file (char  * file_name , char  * * buffer , int  * sizeof_buffer ) {
124126 int  c , i ;
125127 int  char_read  =  0 ;
@@ -138,6 +140,15 @@ int read_image_file(char *file_name, char **buffer, int *sizeof_buffer) {
138140 return  0 ;
139141}
140142
143+ int  send_to_server (int  sockfd , void  * buffer , int  size ) {
144+  int  n  =  write (sockfd , buffer , size );//Write to server 
145+ 
146+  if  (n  <  0 ) {
147+  error ("\t Error writing to server\n" , WRITE_FAIL );
148+  free (buffer );
149+  }
150+ }
151+ 
141152int  main (int  argc , char  * argv []){
142153int  sockfd , portno ;//File descriptor for Sockets 
143154struct  sockaddr_in  serv_addr ;//Structure to hold Server and Client details 
@@ -179,29 +190,32 @@ int main(int argc, char *argv[]){
179190do  {
180191 int  n ;
181192 char  file_name [MAX_FILE_NAME ];
193+  char  extension [EXT_SIZE ];
182194 int  size_of_file  =  0 ;
183195 char  * buffer  =  NULL ;
184196
185197 printf ("****************** WELCOME TO IMAGE TRANSFER WITHOUT FTP *****************\n" );
186-  //printf("\t Please enter a image to send : "); 
187- //scanf("%s", &file_name); 
198+  printf ("\t Please enter a image to send : " );
199+ scanf ("%s" , & file_name );
200+  printf ("\t Extension : " );
201+ scanf ("%s" , & extension );
188202
189-  if  (read_image_file ("linux-bsd.gif" , & buffer , & size_of_file )) {
203+  if  (read_image_file (file_name , & buffer , & size_of_file )) {
190204 error ("\t Reading Image Failed" , READ_FAIL );
191205 goto safe_exit ;
192206 }
193-  //printf("------"); 
194-  printf ("%d" , size_of_file );
195- n  =  write (sockfd , & size_of_file , sizeof (int ));//Write to server 
207+  printf ("\t Size of file : %d\n" , size_of_file );
208+ n  =  send_to_server (sockfd , & size_of_file , sizeof (int ));//Write to server 
209+ if  (n  <  0 ) {
210+  goto safe_exit ;
211+  }
212+ n  =  send_to_server (sockfd , extension , EXT_SIZE );//Write to server 
196213if  (n  <  0 ) {
197- error ("\t Error writing to server\n" , WRITE_FAIL );
198-  free (buffer );
199214 goto safe_exit ;
200215 }
201- n  =  write (sockfd , buffer , size_of_file );//Write to server 
216+  printf ("\t Sending File\n" );
217+ n  =  send_to_server (sockfd , buffer , size_of_file );//Write to server 
202218if  (n  <  size_of_file ) {
203- error ("\t Error writing to server\n" , WRITE_FAIL );
204-  free (buffer );
205219 goto safe_exit ;
206220 }
207221} while (0 );
0 commit comments