NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include <sys/stat.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdbool.h>


#define M 30
#define N 14

/* la premiere fonction qui va creer les fichiers au répertoire */
void cree_fichiers(char *rep)

{

char ch[M];

strcpy(ch,rep);

FILE *fp1;

strcat(rep,"/fich1.txt");

fp1 = fopen(rep,"w");

fclose(fp1);

strcat(ch,"/fich2.txt");

FILE *fp2;

fp2 = fopen(ch,"w");

fclose(fp2);

}
/* la fonction qui va creer le repertoire au repertoire courant*/
bool creerSousRepertoire(const char *rep,const char *nom)

{

char ch[M];

char tmp[M];

char tmp2[M];

strcpy(tmp,rep);

strcpy(tmp2,nom);

strcat(tmp,"/");

strcpy(ch,tmp);

strcat(ch,tmp2);

if(mkdir(ch,0777) != 0)

{

return false;
}

else{
cree_fichiers(ch);
return true;
}
}

/* la supprission du repertoire */

void concat(char * dest,const char *rep, const char *nom_rep, const char *nom_fich)

{

char ch[M];

char tmp[M];

char tmp2[M];

strcpy(tmp,rep);

strcpy(tmp2,nom_rep);

strcat(tmp,"/");

strcat(tmp2,"/");

strcpy(ch,tmp);

strcat(ch,tmp2);

strcat(ch,nom_fich);

strcpy(dest,ch);

}

int fich_exist(const char *nom)

{

FILE *file;

if (file = fopen(nom, "r")){

fclose(file);

return 1;

}

return 0;

}

void supp_fich(const char *rep,const char *nom_rep,const char *nom_fich)

{

char ch[M];

char dest[M];

concat(dest,rep,nom_rep,nom_fich);

strcpy(ch,dest);

if(fich_exist(ch))

remove(ch);

else

printf("Le fichier %s n'existe pas !!n",nom_fich);



}


bool supp_dir(const char *rep,const char *nom)

{

char ch[M];

char tmp[M];

char tmp2[M];

strcpy(ch,rep);

strcat(ch,"/");

strcat(ch,nom);

char dest[M];

concat(dest,rep,nom,"fich1.txt");

char dest2[M];

concat(dest2,rep,nom,"fich2.txt");

if(fich_exist(dest))

supp_fich(rep,nom,"fich1.txt");

if(fich_exist(dest2))

supp_fich(rep,nom,"fich2.txt");

if(rmdir(ch) != 0)

{
return false;

}

else

return true;
}

/* la suppression des fichiers */

bool supp_fich_4(const char *rep,const char *nom_rep,const char *nom_fich)

{

char ch[30];

char dest[14];

concat(dest,rep,nom_rep,nom_fich);

strcpy(ch,dest);

if(fich_exist(ch)){
remove(ch);
return true;
}

else
return false;
}



int main(int argc, char** argv )
{ char datas[] = "hellon";
int sockServer,sockClient,clilen,chilpid,ok,nleft,nbwriten;
char c,c1,nomRepertoire[30],nom_fich[30];
struct sockaddr_in cli_addr,serv_addr;
bool resultat;

if (argc!=2) {printf ("usage: socket_server portn");exit(0);}

printf ("server starting...n");

/* ouverture du socket */
sockServer = socket (AF_INET,SOCK_STREAM,0);
if (sockServer<0) {printf ("impossible d'ouvrir le socketn");exit(0);}

/* initialisation des parametres */
bzero((char*) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(atoi(argv[1]));

/* effecture le bind */
if (bind(sockServer,(struct sockaddr*)&serv_addr,sizeof(serv_addr))<0)
{printf ("impossible de faire le bindn");exit(0);}

/* petit initialisation */
listen(sockServer,1);

/* attend la connection d'un client */
clilen = sizeof (cli_addr);
sockClient = accept (sockServer,(struct sockaddr*) &cli_addr, &clilen);
if (sockClient<0) {printf ("accept errorn"); exit(0);}
printf ("connection acceptedn");
do{
while (read(sockClient,&c,1)!=1);

switch(c){
case '1':
printf("votre choix est 1n");
break;
case '2':
recv(sockClient,nomRepertoire,30,0);
resultat=creerSousRepertoire(".",nomRepertoire);
if(resultat){
send(sockClient,"REPERTOIRE A ETE CREER AVEC SUCCESn",80,0);
}

else{

send(sockClient,"le reperoire donnee deja exist vous dever le supprimmer...n",80,0);

}
break;
case '3':
recv(sockClient,nomRepertoire,30,0);
resultat=supp_dir(".",nomRepertoire);
if(resultat){
send(sockClient,"REPERTOIRE A ETE SUPPRIMER AVEC SUCCEEn",80,0);
}

else{
send(sockClient,"le reperoire donnee n existe pas ...n",80,0);

}

break;
case '4':
recv(sockClient,nomRepertoire,30,0);
while (read(sockClient,&c1,1)!=1);
printf("%c",c1);
switch(c1)

{

case '1':

{

strcpy(nom_fich,"fich1.txt");

break;

}

case '2':

{

strcpy(nom_fich,"fich2.txt");

break;

}

}

resultat= supp_fich_4(".",nomRepertoire, nom_fich);
if(resultat){
send(sockClient,"LE FICHIER A ETE SUPPRIMER AVEC SUCCEEn",80,0);
}

else{
send(sockClient,"le fichier donnee n existe pas ...n",80,0);

}

break;
case '6':
printf("La connection entre le serveur et le client est stopé....n");
break;

default:
printf("LA COMMANDE A ETE PASSER AVEC SUCCEESn");
break;

}

}while(1);
return 0;
}
     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.