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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
| #define DEBUG_ENVOIE
#include "SocketC+S.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PORT 8888
#define BUFFERSIZE 4096
#define TRUE 1
#define FALSE 0
#ifdef WIN32
#include <winsock2.h>
#define DELIMITATEUR '\\'
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <errno.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <netdb.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close(s)
#define DELIMITATEUR '/'
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
typedef struct in_addr IN_ADDR;
#else
#error not defined for this platform
#endif // WIN32
static void init(){
#ifdef WIN32
WSADATA wsa;
int err =WSAStartup⁽MAKEWORD(2, 2),&wsa);
if(err<0){
puts("WSAstartup failed !\n");
exit(EXIT_FAILURE);
}
#endif // WIN32
}
/** \brief initialisation de l'interface winsock
* \return void
*/
static void end(SOCKET sock){
closesocket(sock);
#ifdef WIN32
WSACleanup();
#endif // WIN32
}
/** \brief fin de l'interface winsock
* fermeture du socket
* \param socket a fermer (socket principal)
* \return void
*/
static void Dsend(char *buffer, SOCKET sock,int size){
if(send(sock,&size,sizeof(int),0)<0){
perror("send()");
exit(errno);
}
#ifdef DEBUG_ENVOIE
printf("size: %u\n",size);
#endif // DEBUG_ENVOIE
if(send(sock,buffer,strlen(buffer),0)<0){
perror("send()");
exit(errno);
}
#ifdef DEBUG_ENVOIE
printf("buffer: '%s'\n",buffer);
#endif // DEBUG_ENVOIE
}
/** \brief envois de la taille du buffer
* envois du buffer sur le socket
* \param buffer a envoyer
* \param socket sur lequel envoyer
* \return void
*/
static char * Drecv(SOCKET csock,int *size){
if(recv(csock, size,sizeof(int),0)<0){
perror("recv()");
exit(errno);
}
#ifdef DEBUG_ENVOIE
printf("size: %u\n",*size);
#endif // DEBUG_ENVOIE
char *data=malloc(*size);
if(recv(csock, data,*size,0)<0){
perror("recv()");
exit(errno);
}int i=*size;
data[i]='\0';
#ifdef DEBUG_ENVOIE
printf("data: '%s'\n",data);
#endif // DEBUG_ENVOIE
return data;
}
/** \brief reception des infos envoyer par Dsend
*
* \param buffer a remplir
* \param socket client sur lequel ecouter
* \return taille du buffer
*/
static int Csignal(int buffer,SOCKET sock){
int res=0;
if(send(sock,&buffer,sizeof(buffer),0)<0){
perror("send()");
exit(errno);
}
printf("signal %u sent\n",buffer);
if(recv(sock, &res,sizeof(int),0)<0){
perror("recv()");
exit(errno);
}
if(res!=1){
printf("reponse incorecte\n");
return FALSE;
}else{
return TRUE;
printf("signal %u recu\n",res);
}
}
/** \brief envoie du signal de choix
* \param signal a envoyer
* \param socket sur lequel envoyer
* \return 0 si reponse incorecte du serveur
* 1 si reponse correcte du serveur
*/
static int Rsignal(SOCKET sock){
int buffer=0;
int res=0;
if(recv(sock, &res,sizeof(int),0)<0){
perror("recv()");
exit(errno);
}
switch(res){
case 1:buffer=1;break;
case 666:buffer=1;break;
default:buffer=0;break;
}
printf("signal reçu: %u\n",res);
if(send(sock,&buffer,sizeof(buffer),0)<0){
perror("send()");
exit(errno);
}
printf("Reponse au signal: %u\n",buffer);
return res;
}
/** \brief reception du signal de Csignal
* \param socket sur lequel recevoir
* \return valeur du signal
*/
static FILE *RFopen(char *nomFichier,char *dossier){
char *location=strdup(dossier);
if( location == NULL ){
perror("Erreur allocation mémoire\n");
exit( EXIT_FAILURE );
}
char *ptr_loc=NULL;
FILE *fichier;
ptr_loc=realloc(location,(strlen(location)+sizeof(nomFichier)+1));
if( ptr_loc == NULL ){
free( location );
location = NULL;
perror("Erreur reallocation\n");
exit( EXIT_FAILURE );
}
location=ptr_loc;
strcat(location,nomFichier);
fichier=fopen(location,"wb");
if (fichier==NULL) {
perror("fopen()");
exit(errno);
}else{
printf("Fichier ouvert: %s \nDans %s\n",nomFichier,dossier);
}
free(location);
return fichier;
}
/** \brief ouverture d'un fichier en mode binaire
* \param nom du fichier
* \param dossier de stockage du fichier
* \return file descriptor
*/
static char *chNom(char *input){
char* carac=input;
int i;
char* nom;
for (i=0; i<128; i++)
{
if (!*carac) break;
nom=carac;
while(*carac && *carac!=DELIMITATEUR) carac++;
if (*carac)
{
carac++;
}
}
return nom;
}
/** \brief recuperation d'un nom de fichier dans un chemin
* non destructif
* \param chemin du fichier
* \param none
* \return nom du fichier
*/
static void Lerase(){
printf("\x0d");
printf("\033[K");
}
/** \brief efface la ligne actuel de stdout
* \param none
* \param none
* \return void
*/
static char *readFile(char *path,long *lSize){
FILE * fichier;
char * buffer;
size_t result;
fichier = fopen ( path , "rb" );
if (fichier==NULL) {
perror("fopen()");
exit(errno);
}
// obtain file size:
fseek (fichier , 0 , SEEK_END);
*lSize = ftell (fichier);
rewind (fichier);
// allocate memory to contain the whole file:
buffer = (char*)malloc(*lSize);
if (buffer == NULL) {
perror("malloc()");
exit(errno);
}
// copy the file into the buffer:
result = fread (buffer,1,*lSize,fichier);
if (result != *lSize) {
perror("fread()");
exit(errno);
}
/* the whole file is now loaded in the memory buffer. */
// terminate
fclose (fichier);
return buffer;
}
/** \brief enregistre un fichier binaire en ram
* \param chemin du fichier
* \param none
* \return pointeur sur le fichier en ram
*/
static long sizeOfFile(char *path){
long lSize;
FILE *fichier = fopen ( path , "rb" );
if (fichier==NULL) {
perror("fopen()");
exit(errno);
}
fseek (fichier , 0 , SEEK_END);
lSize = ftell (fichier);
rewind (fichier);
fclose(fichier);
return lSize;
}
/** \brief recupere le nombre de byte du fichier
* \param chemin du fichier
* \param none
* \return nombre de byte
*/
static int decoupeStream(char *buffer,char *output,long size,long byteActuel){
int i=0;
size=size-byteActuel;
buffer=buffer+byteActuel;
for(i=0;i<=BUFFERSIZE;i++)
{
if (i>=size) break;
output[i]=*buffer;
buffer++;
}
output[i]='\0';
return i;
}
/** \brief decoupe un buffer en segment de BUFFERSIZE byte
* destructif
* \param buffer a decouper
* \param
* \return partie du buffer faisant BUFFERSIZE
*/
static void sFile(char *path,SOCKET sock){
char* nom=chNom(path);
long size=0;
char *file=readFile(path,&size);
printf("le nom est '%s',le chemin est '%s'\n",nom,path);
while(!Csignal(1,sock));
if(send(sock,&size,sizeof(long),0)<0){
perror("send()");
exit(errno);
}
Dsend(nom,sock,strlen(nom));
envoieFile(path,sock,size,file);
}
/** \brief envoie d'un fichier avec prérequis:
* Csignal; Dsend(nom); envoieFile
* \param chemin du fichier
* \param socket sur lequel envoyer
* \return void
*/
static void envoieFile(char *path ,SOCKET sock,long size,char *file){
long i=0;
int sSize=0;
char *ligne=malloc(BUFFERSIZE);
printf("sending file : %s\n",path);
for(i=0;size>i;i=i+sSize)
{
sSize=decoupeStream(file,ligne,size,i);
printf(".");
Dsend(ligne,sock,sSize);
memset(ligne,'\0',BUFFERSIZE);
}
printf("\r\n");
free(ligne);
free(file);
}
/** \brief envoie du corp d'un fichier
* \param chemin du fichier
* \param socket sur lequel envoyer
* \return void
*/
static void frecv(SOCKET csock,char *dossier){
int ctrl;
long size,i=0;
if(recv(csock, &size,sizeof(long),0)<0){
perror("recv()");
exit(errno);
}
printf("taille du fichier : %lu Octets\n",size);
char *buffer=malloc(BUFFERSIZE);
char *nomFichier=Drecv(csock,&ctrl);
FILE *fichier=RFopen(nomFichier,dossier);
for(i=0;i<size;i=i+ctrl)
{
free(buffer);
buffer=Drecv(csock,&ctrl);
fwrite( buffer , 1 , ctrl, fichier);
}
fclose(fichier);
}
/** \brief reception d'un fichier
* nom + corp
* \param socket sur lequel ecouter
* \param dossier ou stocker le fichier
* \return void
*/
static SOCKET Caccept(SOCKET sock){
SOCKADDR_IN csin= {0};
SOCKET csock;
socklen_t sinsize=sizeof(csin);
csock=accept(sock,(SOCKADDR *)&csin,&sinsize);
if(csock==INVALID_SOCKET){
perror("accept()");
exit(errno);
}
return(csock);
}
/** \brief creation d'un socket client
* \param socket initial
* \param none
* \return socket client
*/ |
Partager