Client serveur pour le transfert de fichier

Description

c'est un client/serveur simple qui permet de choisir un fichier sur le disque dur local et de l'envoyer vers un ordinateur appartenant au réseaux local

Source / Exemple :


le code pour le client:
HINSTANCE hInst;	
#define PARAMETERS_OK 0
#define PARAMETERS_NOT_OK 1

#define MAXLEN_USERNAME 16
#define MINLEN_USERNAME 1
#define MAXLEN_LICENCE 10
#define MINLEN_LICENCE 8
#define MAXLEN_MACHINEID 32
#define MAXLEN_FILENAME 16
#define MAXLEN_FILEINFO 256
static char StrStatus[128];
// variable contenant le Fichier 
static char StrFileName[32];
////////////////////////////////////////////////////////
void UpdateDialogVariables(HWND hDlg);
void chemin(char* p);
BOOL WINAPI BrowseLicenceFile(HWND hWndOwner, LPSTR lpszFile, LPSTR lpszInitialDir,char *filterString);
void nom_fichiers(char* p,char* res);
///////////////////////////////////////////////////////

/*//////////////////////main function\\\\\\\\\\\\\\\\\\*/
void CTfichierDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
	int nrecv=0;
	int nlecture=0;
	int taille_file=0;
	int interm=0;
	CString s;
	char *nom_fichier=new char[200];
	char *buff=new char [1024];
	char *buffer = new char[255];
	char *taille_fichier=new char[100];
	
	///////////////////////////////////pour la boite 
                ///////////////////////////////////ouvrir 
	HWND hDlg= GetSafeHwnd();
	char fileName[MAXLEN_FILEINFO];
	char strFilterString[32];
	char *res=new char[100];
	char *inter=new char[100];
	memset(fileName,0,sizeof(fileName));
	memset(StrStatus,0,sizeof(StrStatus));
	//Mise à jour du filtre de recherche de fichier
	memset(strFilterString,0,sizeof(strFilterString));
	sprintf(strFilterString," ALL FILES");
	sprintf(&strFilterString[strlen(strFilterString)+1],"*.*");
	BOOL th=BrowseLicenceFile(hDlg, fileName, ".\\",strFilterString) ;
	//////////////////////////fin du traitement pour la boite ouvrir 
	sprintf(nom_fichier,"%s",fileName);
	nom_fichiers(nom_fichier,inter);
	chemin(nom_fichier);
	FILE* f=fopen(nom_fichier,"rb");
	fseek(f,0,SEEK_END);
	taille_file=ftell(f);
	itoa(taille_file,taille_fichier,10);
	fseek(f,0,SEEK_SET);
	WSADATA WSAData;
	WSAStartup(MAKEWORD(1,1), &WSAData);
	SOCKET sock;
	SOCKADDR_IN sin;
	/* tout est configuré pour accéder au serveur qui se trouve sur l'adresse 192.168.0.52*/
	sock = socket(AF_INET, SOCK_STREAM, 0);
	sin.sin_addr.s_addr			= inet_addr("192.168.0.105");
	sin.sin_family				= AF_INET;
	sin.sin_port				= htons(21);
	connect(sock, (SOCKADDR *)&sin, sizeof(sin));
	send(sock,taille_fichier,strlen(taille_fichier),0);
	interm=strlen(inter);
	inter[interm]='\0';
	send(sock,inter,100,0);
	while(!feof(f))
	{
		nlecture=fread(buff,1,1024,f);
		send(sock,buff,nlecture,0);
	}
	MessageBox("envoi reussi","r",1);
	nrecv=recv(sock, buffer, 255, 0);
	buffer[nrecv]='\0';
	MessageBox(buffer,"reussi",0);
	closesocket(sock);
	WSACleanup();
	fclose(f);
	

}
//////////////////////////////////////////////////////////////
///////////////////derivated function////////////////////////
/////////////////////////////////////////////////////////////

BOOL WINAPI BrowseLicenceFile(HWND hWndOwner, LPSTR lpszFile, LPSTR lpszInitialDir,char *filterString)    
{       
	OPENFILENAME   ofn;
	BOOL           fStatus;
	DWORD          dwError;

	if (NULL==lpszFile)       
		return FALSE;     

   memset((LPOPENFILENAME)&ofn, 0, sizeof(ofn));    

   ofn.lStructSize =sizeof(ofn);    
   ofn.hwndOwner   =hWndOwner;    
   ofn.lpstrFile   =lpszFile;    
   ofn.nMaxFile    =MAXLEN_FILEINFO - 1;    
   ofn.lpstrFilter =(LPSTR)filterString;;    
   ofn.nFilterIndex=1;    	   
   ofn.lpstrTitle  ="recherche de fichier";    
   ofn.hInstance = hInst;    
   ofn.lpTemplateName = NULL;    
   if (NULL != lpszInitialDir)      
	   ofn.lpstrInitialDir = lpszInitialDir;     
   ofn.Flags= OFN_HIDEREADONLY  ;  

   //On success, copy the chosen filename to the static display    
   fStatus = GetOpenFileName((LPOPENFILENAME)&ofn);    
   dwError = CommDlgExtendedError();
   return fStatus; 
}

// cette fonction  extrait le chemin du fichier//
void chemin(char* p)
{
	int taille= strlen(p);
	char *inter=new char[100];
	int i=0;
	int j=0;
	while(i<taille)
	{
		if(p[i]!='\\')
		{
			inter[j]=p[i];
			
		}
		else
		{
			inter[j]=p[i];
			inter[++j]=p[i];
		}
		i++;
		j++;
	}
	inter[j]='\0';
	strcpy(p,inter);
}

//cette fonctionne extrait le nom de fichier//
void nom_fichiers(char* p,char* res)
{
	int taille=strlen(p);
	int j=taille;
	int i=0;
	char *s=new char[100];
	while(p[taille]!='\\')
	{
		taille--;
	}
	taille++;
	while(taille<j)
	{
		s[i]=p[taille];
		taille++;
		i++;
	}
	s[i]='\0';
	strcpy(res,s);
}
/////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////pour  le serveur///////////////////////////
///////////////////////////////////////////////////////////////////////////////

void CServeurftpDlg::OnButton1() 
{
                int j=1;
	int nrecv=0;
	int n=0;
	int taille_fichier=0;
	char *buffer= new char[100];
	char *buff= new char[1024];
	char *nom_fichier=new char[30];
	WSADATA WSAData;
	FILE* fh;
	WSAStartup(MAKEWORD(1,1), &WSAData);
	SOCKET sock;
	SOCKET csock;
	SOCKADDR_IN sin;
	SOCKADDR_IN csin;
	sock = socket(AF_INET, SOCK_STREAM, 0);
	sin.sin_addr.s_addr			= INADDR_ANY;
	sin.sin_family				= AF_INET;
	sin.sin_port				= htons(21);
	bind(sock, (SOCKADDR *)&sin, sizeof(sin));
	listen(sock, 0);
	while(j!=0)
	{
	int sinsize = sizeof(csin);
	if((csock = accept(sock, (SOCKADDR *)&csin, &sinsize)) != INVALID_SOCKET)
	{

	nrecv=recv(csock,buffer,strlen(buffer),0);
	buffer[nrecv]='\0';
	taille_fichier=atoi(buffer);
	memset(buffer,0,strlen(buffer));
	n=recv(csock,buffer,100,0);
	buffer[n]='\0';
	MessageBox(buffer,"",1);
	fh=fopen(buffer,"wb");
	
	do
	{
		nrecv=recv(csock,buff,1024,0);
		fwrite(buff,1,nrecv,fh);
		taille_fichier-=nrecv;
	}
	while(taille_fichier>0);
	send(csock, "Hello world!\r\n", 14, 0);
	j=0;
	MessageBox(buffer,"r",0);
	}
	}
	fclose(fh);
	closesocket(sock);
	closesocket(csock);
	WSACleanup();
}

Conclusion :


pour le client et le serveur vous creer un simple bouton et vous inserer le code
si quelqu'un peut l'améliorer afin que ça marche sur internet je lui serais reconnaissant.
le serveur se bloque si vous tenter de l'utiliser deux fois.
et merci à ceux dont je me suis inspiré de leur code

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.