@@ -69,6 +69,8 @@ int main(int argc, char const *argv[])
69
69
return 1 ;
70
70
}
71
71
72
+ sendFile (mSocketUDP , & clientAddress , clientLengthUDP );
73
+
72
74
if ((recvfrom (mSocketUDP , userInput , RCVSIZE , 0 , (struct sockaddr * )& clientAddress , (socklen_t * )& clientLengthUDP )) == -1 )
73
75
{
74
76
perror ("recvfrom failed \n" );
@@ -116,3 +118,50 @@ int handshake(int mSocketUDP, struct sockaddr_in *clientAddress, int clientLengt
116
118
117
119
return 1 ;
118
120
}
121
+
122
+ int sendFile (int mSocketUDP , struct sockaddr_in * clientAddress , int clientLengthUDP )
123
+ {
124
+ info ("SENDFILE" , "sending file..." );
125
+ FILE * in_file = fopen ("../assets/mock.pdf" , "rb" ); // read only
126
+
127
+ if (!in_file ) // equivalent to saying if ( in_file == NULL )
128
+ {
129
+ printf ("oops, file can't be read\n" );
130
+ exit (-1 );
131
+ }
132
+ fseek (in_file , 0L , SEEK_END );
133
+ long size = ftell (in_file ) / 8 ;
134
+ rewind (in_file );
135
+
136
+ unsigned char buffer [1024 ]; // to be sent bufffer
137
+ unsigned char fileBuffer [size ]; // contains the whole file
138
+ bzero (buffer , sizeof (buffer )); //set to zero
139
+ bzero (fileBuffer , sizeof (fileBuffer )); //set to zero
140
+
141
+ fread (fileBuffer , sizeof (fileBuffer ), 1 , in_file );
142
+
143
+ // segment
144
+ long nbOfSegment = size / 1020 ;
145
+ for (int i = 0 ; i < nbOfSegment ; i ++ )
146
+ {
147
+ buffer [0 ] = (char )i / 100 ;
148
+ buffer [1 ] = (char )(i / 100 ) % 10 ;
149
+ buffer [2 ] = (char )i % 10 ;
150
+ // buffer[3]="-";
151
+ for (int j = 0 ; j < 1020 ; j ++ )
152
+ {
153
+ buffer [j + 3 ] = fileBuffer [i * 1020 + j ];
154
+ info ("SENDFILE" , "file segmented..." );
155
+
156
+ if (sendto (mSocketUDP , buffer , sizeof (buffer ), 0 , clientAddress , clientLengthUDP ) > 0 )
157
+ {
158
+ info ("SENDFILE" , "segment sent 🎉..." );
159
+ }
160
+ else
161
+ {
162
+ perror ("[CONNECT] SYNACK couldn't be sent...\n" );
163
+ return 0 ;
164
+ }
165
+ }
166
+ }
167
+ }
0 commit comments