Création d'un client

tomalille Messages postés 52 Date d'inscription jeudi 16 juin 2005 Statut Membre Dernière intervention 5 juin 2007 - 16 juin 2005 à 22:55
tomalille Messages postés 52 Date d'inscription jeudi 16 juin 2005 Statut Membre Dernière intervention 5 juin 2007 - 17 juin 2005 à 16:02
Bonjour,
je viens d'écrire ce code qui cré un client qui se connecte sur un serveur apache.
A la compilation (sous visual C++) j'ai 5 erreurs, je ne comprend pas pourquoi.

Merci beaucoup
Thomas

#pragma once
#include "winsock2.h"
#pragma comment(lib,"WS2_32.lib")


#include <commctrl.h>
#pragma comment(lib,"comctl32.lib")


#include <errno.h>
#include <winsock.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include
#include <conio.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <string.h>



int main (int argc,char * argv[]);
int open_tcp(char * name,unsigned short int port);



int open_tcp(char *name,unsigned short int port)
{
int fs;
struct sockaddr_in s;


fs=socket(AF_INET,SOCK_STREAM,0);
if(fs==-1)
return(-1);
s.sin_family=AF_INET;
memcpy(&s.sin_addr.s_addr,gethostbyname("localhost") -> h_addr_list[0],4);
s.sin_port=htons(port);
if(connect(fs,(struct sockaddr *) &s,sizeof(s))==-1)
{
closesocket(fs);
return(-1);
}
return(fs);
}



// function GET
int get(char *url,char *par,char *sid,char *serv,unsigned short int port,int s,int fd)


{
static char httphead[]="GET %s?%s HTTP/1.0\nContent-Type: multipart/form-data;boundary=-------------------------7d4be35a40\nCache-Control:no-cache\nCookie:PHPSESSID=%s\nHost:%s:%d\nContent-Length:%d\n\n";
static char mpstart[]="-------------------------7d4be35a40\nContent-Disposition:form-data;name="rapport";filename="d:\\Etudiant\fichier.doc"\nContent-Type:application/octet-stream\n\n";
// champ 1: url
// champ 2: parametre
// champ 3: session_id
// champ 4: serveur
// champ 5: port
// champ 6: size
static char mpstop[]="\n-------------------------7d4be35a40--\n";


char *p;
unsigned long int size;
char buffer[1024];
int len;


size=_lseek(fd,0L,SEEK_END);
size+=strlen(mpstart)+strlen(mpstop);
p=(char *)malloc(strlen(httphead)+strlen(par)+strlen(sid)+strlen(serv)+9);
if(p==NULL) return (-1);
sprintf(p,httphead,url,par,sid,serv,port,size);
send(s,p,strlen(p)+9,0);
free(p);
send(s,mpstart,strlen(mpstart),0);
_lseek(fd,0L,SEEK_SET);
while(1)
{
len=_read(fd,buffer,sizeof(buffer));
if(len==0)
break;
send(s,buffer,len,0);
}


send(s,mpstop,strlen(mpstop),0);
return(0);
}



//function POST
int post(char *url,char *par,char *sid,char *serv,unsigned short int port,int s,int fd)


{
static char httphead[]="POST %s?%s HTTP/1.0\nContent-Type: multipart/form-data; boundary=-------------------------7d4be35a40\nCache-Control: no-cache\nCookie: PHPSESSID=%s\nHost: %s:%d\nContent-Length: %d\n\n";
static char mpstart[]="-------------------------7d4be35a40\nContent-Disposition: form-data; name="rapport"; filename="d:\\basesend"\nContent-Type: application/octet-stream\n\n";


static char mpstop[]="\n-------------------------7d4be35a40--\n";


char *p;
unsigned long int size;
char buffer[1024];
int len;


size=_lseek(fd,0L,SEEK_END);
size+=strlen(mpstart)+strlen(mpstop);
p=(char *)malloc(strlen(httphead)+strlen(url)+strlen(par)+strlen(sid)+strlen(serv)+9);
if(p==NULL) return (-1);
sprintf(p,httphead,url,par,sid,serv,port,size);
send(s,p,strlen(p),0);
free(p);
send(s,mpstart,strlen(mpstart),0);
_lseek(fd,0L,SEEK_SET);
while(1)
{
len=_read(fd,buffer,sizeof(buffer));
if(len==0)
break;
send(s,buffer,len,0);
}


send(s,mpstop,strlen(mpstop),0);
return(0);
}



//fonction qui permet de recuperer un fichier sur un serveur
void get_file(int s)
{
char buffer[1024];
ZeroMemory(buffer,1024);
int len,j=0,k=0;
FILE * fichier;


while(1)
{
fichier=fopen("d:\\Etudiant\\loupe1.gif","w+b");
len=recv(s,buffer,sizeof(buffer),0);
while(buffer[j]!='\n' || buffer[j+1]!='\r')
{
j++;
}
//ceci sert a trouver a partir de quel caractere le fichier commence
fwrite(buffer+j+3,1,128,fichier);
if(len==0)
break;
_write(1,buffer,len);
}
fclose(fichier);
}


//fonction qui permet d'obtenir un accuse de reception du serveur dans le cas d'un POST
void reponse_serveur(int s)
{
char buffer[1024];
int len=0;

while(1)
{


len=recv(s,buffer,sizeof(buffer),0);
if(len==0)
break;
_write(1,buffer,len);
}
}


int main(int argc, char *argv[])
{
char serveur[]="localhost";
unsigned short int port=80;
char fname[]="d:\\Etudiant\\e.txt";
int fs,fd;
char z;


WSADATA wsaData;
WORD wVersionRequested;


wVersionRequested=MAKEWORD(1,1);
if(WSAStartup(wVersionRequested,&wsaData)!=0) return(0);


fs=open_tcp(serveur,port);
if(fs==-1)
{
printf("ERROR OPENING SERVER\n");
WSACleanup();
return (1);
}


fd=_open(fname,_O_RDONLY|_O_BINARY);
if(fd==-1)
{ closesocket(fs);
WSACleanup();
return(1);
}


printf("Que voulez-vous faire?\n");
printf("a:recuperer un fichier sur le serveur\n");
printf("b:envoyer un fichier au serveur\n");
scanf("%c",&z);



switch(z)
{
case 'a' : //on va recuperer loupe1.gif sur le serveur
if(get("/loupe1.gif","","1c7f2b514b215afa4f7394313eab1872",serveur,port,fs,fd) == -1)
{
close(fd);
closesocket(fs);
WSACleanup();
return(1);
}
close(fd);
get_file(fs);
break;



case 'b': //on envoie fichier.doc au serveur
if(post("/serveur.php","a=1","1c7f2b514b215afa4f7394313eab1872",serveur,port,fs,fd) == -1)
{
close(fd);
closesocket(fs);
WSACleanup();
return(1);
}
close(fd);
reponse_serveur(fs);
break;
}


closesocket(fs);
WSACleanup();
return 0;



}

3 réponses

darfeuille Messages postés 63 Date d'inscription vendredi 14 mai 2004 Statut Membre Dernière intervention 25 juillet 2005
17 juin 2005 à 08:06
indique les erreurs
0
ncoder Messages postés 244 Date d'inscription vendredi 6 mai 2005 Statut Membre Dernière intervention 6 avril 2008 1
17 juin 2005 à 11:21
Moi Dev Cpp me met aucune erreur compile impec et crée bien l'exe ya pas de pb...
0
tomalille Messages postés 52 Date d'inscription jeudi 16 juin 2005 Statut Membre Dernière intervention 5 juin 2007
17 juin 2005 à 16:02
mes erreurs:
Compiling...
client.c
C:\Disque D\Projet ITI\essai\client.c(129) : error C2143: syntax error : missing ';' before 'type'
C:\Disque D\Projet ITI\essai\client.c(130) : error C2275: 'FILE' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\stdio.h(156) : see declaration of 'FILE'
C:\Disque D\Projet ITI\essai\client.c(130) : error C2065: 'fichier' : undeclared identifier
C:\Disque D\Projet ITI\essai\client.c(134) : warning C4047: '=' : 'int ' differs in levels of indirection from 'struct _iobuf *'
C:\Disque D\Projet ITI\essai\client.c(135) : error C2065: 'len' : undeclared identifier
C:\Disque D\Projet ITI\essai\client.c(136) : error C2065: 'j' : undeclared identifier
C:\Disque D\Projet ITI\essai\client.c(141) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
C:\Disque D\Projet ITI\essai\client.c(141) : warning C4024: 'fwrite' : different types for formal and actual parameter 4
C:\Disque D\Projet ITI\essai\client.c(146) : warning C4047: 'function' : 'struct _iobuf *' differs in levels of indirection from 'int '
C:\Disque D\Projet ITI\essai\client.c(146) : warning C4024: 'fclose' : different types for formal and actual parameter 1
Error executing cl.exe.


client.obj - 5 error(s), 5 warning(s)
0
Rejoignez-nous