Vous êtes sur la page 1sur 11

// ********************************************************

// Les includes
// ********************************************************
#include <windows.h>
#include <winsock2.h> // pour les fonctions socket
#include <conio.h>
#include <string.h>
#include <stdio.h> // Pour les Sprintf
// ********************************************************
// Les librairies
// ********************************************************
#pragma comment(lib,"ws2_32.lib")
// ********************************************************
// Dfinition des variables
// ********************************************************
WSADATA initialisation_win32; // Variable permettant de rcuprer la structure d'inf
ormation sur l'initialisation
char IP[15] = "127.0.0.2";
int erreur; // Variable permettant de rcuprer la valeur de retour des fonctions ut
ilises
int tempo; // Variable temporaire de type int
char choix = '1'; // choix
int nombre_de_caractere; // Indique le nombre de caractres qui a t reu ou envoy
char buffer[256]; // Tampon contenant les donnes reues ou envoyes
SOCKET id_de_la_socket; // Identifiant de la socket
SOCKET id_de_la_socket2; // socket2
SOCKET id_de_la_socketClient;
SOCKADDR_IN information_sur_la_source; // Dclaration de la structure des informat
ions li l'coute
SOCKADDR_IN information_sur_la_destination;
int port;
void winsockOpen(){
// ********************************************************
// Initialisation de Winsock
// ********************************************************
erreur=WSAStartup(MAKEWORD(2,2),&initialisation_win32);
if (erreur!=0)
printf("\nDesole, je ne peux pas initialiser Winsock du a l'erreur :
%d %d",erreur,WSAGetLastError());
else
//printf("\nWSAStartup : OK");
printf("");
}
void socketOpen(){
// ********************************************************
// Ouverture d'une Socket
// ********************************************************
id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
if (id_de_la_socket==INVALID_SOCKET)
printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d"
,WSAGetLastError());
else
//printf("\nsocket
: OK");
printf("");
}
void socketOpen2(){
// ********************************************************
// Ouverture d'une Socket

// ********************************************************
id_de_la_socket2=socket(AF_INET,SOCK_DGRAM,0);
if (id_de_la_socket==INVALID_SOCKET)
printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d"
,WSAGetLastError());
else
//
printf("Creation socket
: OK \n");
printf("");
}
void socketRead(){
// ********************************************************
// Lire la socket une ip et un port d'coute
// ********************************************************
information_sur_la_source.sin_family=AF_INET;
information_sur_la_source.sin_addr.s_addr=INADDR_ANY; // Ecoute sur toutes
les IP locales
information_sur_la_source.sin_port=htons(port); // Ecoute sur le port 3333
3
erreur=bind(id_de_la_socket,(struct sockaddr*)&information_sur_la_source,s
izeof(information_sur_la_source));
if (erreur!=0)
printf("\nDesole, je ne peux pas ecouter ce port : %d %d",erreur,WSA
GetLastError());
else
//printf("\nbind
: OK");
printf("");
}
void socketReceive(){
// ********************************************************
// Reception des donnes
// ********************************************************
tempo=sizeof(information_sur_la_source); // Passe par une variable afin d'
utiliser un pointeur
nombre_de_caractere=recvfrom(id_de_la_socket,buffer,1515,0,(struct sockadd
r*)&information_sur_la_source,&tempo);
buffer[nombre_de_caractere]='\0'; // Permet de fermer le tableau aprs le co
ntenu des data, car la fonction recvfrom ne le fait pas
printf("\n ip client : %s",inet_ntoa(information_sur_la_source.sin_addr));
choix = buffer[0];
}
void socketClose(){
// ********************************************************
// Fermeture de la socket correspondante la commande socket()
// ********************************************************
erreur=closesocket(id_de_la_socket);
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %
d %d",erreur,WSAGetLastError());
else
//printf("\nclosesocket : OK");
printf("");
}
void socketClose2(){
// ********************************************************
// Fermeture de la socket correspondante la commande socket()
// ********************************************************
erreur=closesocket(id_de_la_socket2);
if (erreur!=0)

printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %


d %d",erreur,WSAGetLastError());
else
//printf("\nclosesocket : OK");
printf("");
}
void winsockClose(){
// ********************************************************
// Quitte proprement le winsock ouvert avec la commande WSAStartup
// ********************************************************
erreur=WSACleanup(); // A appeler autant de fois qu'il a t ouvert.
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer winsock du a l'erreur : %d
%d",erreur,WSAGetLastError());
else
//printf("\nWSACleanup : OK");
printf("");
}
void sending(char *information){
// ********************************************************
// Envoi des donnes
// ********************************************************
information_sur_la_destination.sin_family=AF_INET; // Indiquez l'utilisati
on d'IPV4
information_sur_la_destination.sin_addr.s_addr=inet_addr(IP); // Indiquez
l'adresse IP de votre serveur
information_sur_la_destination.sin_port=htons(33333); // Port TCP 33333 de
stination du serveur
strcpy(buffer,information); // Copie la chaine de caractre dans buffer
nombre_de_caractere=sendto(id_de_la_socket2,buffer,strlen(buffer),0,(struc
t sockaddr*)&information_sur_la_destination,sizeof(information_sur_la_destinatio
n));
if (nombre_de_caractere==SOCKET_ERROR)
printf("\nDesole, je ne peux pas envoyer les donnees du a l'erreur :
%d",WSAGetLastError());
else
//printf("\nsending
: OK\n");
printf("");
}
void configuration(void){
port = 3333;
}
char* Date(){
char buffer[256];
time_t timestamp = time(NULL);
strftime(buffer, sizeof(buffer), "%A %d %B %Y .", localtime(&timestamp));
printf("\n****************************************************\n");
printf("*
Date envoyee : %s\n",buffer);
printf("****************************************************\n");
return(buffer);
}

char* Heure(){
char buffer[256];
time_t timestamp = time(NULL);
strftime(buffer, sizeof(buffer), "%X", localtime(&timestamp));
printf("\n****************************************************\n");
printf("*
Heure envoyee : %s\n",buffer);
printf("****************************************************\n");
return(buffer);
}
char * som(char * ch){
char s[256];
char *p;
float s1,s2,r;
p=strchr(ch,' ');
s1=atof(p);
p++;
p=strchr(p,' ');
s2=atof(p);
r=s1+s2;
sprintf(s,"%f",r);
printf("\n****************************************************\n");
printf("*
la somme envoyee : %s \n",s);
printf("****************************************************\n");
return s;
}
char * somN(char * ch){
float s[256];
char * adr ;
int i=0,j ;
float som=0;
adr = ch ;
while ( adr=strchr(adr,' ') )
{
s[i]=atof(adr);
i++ ;
adr++ ;
}
for( j=0;j<i;j++)
som+=s[j];
sprintf(s,"%f",som);
printf("\n****************************************************\n");
printf("*
la somme envoyee : %s \n",s);
printf("****************************************************\n");
return s;
}
char * dif(char * ch){
char s[256];
char *p;
float s1,s2,r;
p=strchr(ch,' ');
s1=atof(p);
p++;
p=strchr(p,' ');
s2=atof(p);
r=s1-s2;

sprintf(s,"%f",r);
printf("\n****************************************************\n");
printf("*
la difference envoyee : %s \n",s);
printf("****************************************************\n");
return s;
}
char* CodeASC(char ch){
int t1;
char t2[256];
t1=ch;
sprintf(t2, "%d", t1);
printf("\n****************************************************\n");
printf("*
Le code ASCII envoyee : %s\n",&t2);
printf("****************************************************\n");
return (&t2);
}
int main (int argc, char* argv[])
{
printf("En attente ...\n");
configuration();
winsockOpen();

while(0 == 0){
char c1, *c2 ;
char* first;
char* second;
char s1[256];
char s2[256];
socketOpen();
socketOpen2();
socketRead();
socketReceive();
switch (choix){
case '1':
Sleep(30);
sending(Date());
break;
case '2':
Sleep(30);
sending(Heure());
break;
case '3':
Sleep(30);
sending(som(buffer));
break;
case '4':
Sleep(30);
sending(dif(buffer));
break;
case '5':
Sleep(30);
sending(somN(buffer));

break;
case '6':
Sleep(30);
c1 = buffer[1];
c2 = CodeASC(c1);
sending(c2);
break;
}//fin switch
socketClose2();
socketClose();
} //fin while
winsockClose();
getch();
}
====================================================================
// ********************************************************
// Les includes
// ********************************************************
#include <winsock2.h> // pour les fonctions socket
#include <stdio.h> // Pour les Sprintf
#include <conio.h>
// ********************************************************
// Les librairies
// ********************************************************
#pragma comment(lib,"ws2_32.lib")
// ********************************************************
// Dfinition des variables
// ********************************************************
WSADATA initialisation_win32; // Variable permettant de rcuprer la structure d'inf
ormation sur l'initialisation
int choix = 1, i = 0;
char IP[15] = "127.0.0.1";
int port = 0;
char info[30] ;
int erreur; // Variable permettant de rcuprer la valeur de retour des fonctions ut
ilises
int tempo; // Variable temporaire de type int
int nombre_de_caractere; // Indique le nombre de caractres qui a t reu ou envoy
char buffer[256]; // Tampon contennant les donnes reues ou envoyes
SOCKET id_de_la_socket; // Identifiant de la socket
SOCKET id_de_la_socket2; // socket 2
SOCKADDR_IN information_sur_la_destination; // Dclaration de la structure des inf
ormations li au serveur
SOCKADDR_IN information_sur_la_source;
int quit = 0;
void menue(void){
printf("\n____________________________________________________\n");
printf("____________________________________________________\n\n");
printf("
Les services disponibles: \n\n");
printf(" Date serveur :1\n");
printf(" Heure serveur:2\n");
printf(" Somme
:3\n");

printf(" Difference :4\n");


printf(" Somme N
:5\n");
printf(" ASCII
:6\n");
printf(" Quitter
:0\n\n");
printf("____________________________________________________\n");
printf("____________________________________________________\n\n");
printf("
saisissez le numero de service !! :
");
scanf("%d",&choix);
printf("\n");
//do{
//gets(buffer);
//}while(choix !=0 && choix !=1 && choix !=2 && choix !=3 && choix !=4 &&
choix !=5 && choix !=6 );
getchar();// pour vider le buffer clavier sinon il saute le prochain scanf
}
void configuration(void){
port = 3333;
}
void winsockOpen(void){
// ********************************************************
// Initialisation de Winsock
// ********************************************************
erreur=WSAStartup(MAKEWORD(2,2),&initialisation_win32);
/*if (erreur!=0)
printf("\nDesole, je ne peux pas initialiser Winsock du a l'erreur :
%d %d",erreur,WSAGetLastError());
else
printf("initialisation Winsock : OK\n");*/
}
void socketOpen(void){
// ********************************************************
// Ouverture d'une Socket
// ********************************************************
id_de_la_socket=socket(AF_INET,SOCK_DGRAM,0);
if (id_de_la_socket==INVALID_SOCKET)
printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d"
,WSAGetLastError());
else
//printf("Creation socket
: OK \n");
printf("");
}
void socketOpen2(void){
// ********************************************************
// Ouverture d'une Socket
// ********************************************************
id_de_la_socket2=socket(AF_INET,SOCK_DGRAM,0);
if (id_de_la_socket==INVALID_SOCKET)
printf("\nDesole, je ne peux pas creer la socket du a l'erreur : %d"
,WSAGetLastError());
else
//printf("Creation socket
: OK \n\n\n\n");
printf("");
}
void sending(char *information){

// ********************************************************
// Envoi des donnes
// ********************************************************
information_sur_la_destination.sin_family=AF_INET; // Indiquez l'utilisati
on d'IPV4
information_sur_la_destination.sin_addr.s_addr=inet_addr(IP); // Indiquez
l'adresse IP de votre serveur
information_sur_la_destination.sin_port=htons(port); // Port TCP 33333 des
tination du serveur
strcpy(buffer,information); // Copie la chaine de caractre dans buffer
nombre_de_caractere=sendto(id_de_la_socket,buffer,strlen(buffer),0,(struct
sockaddr*)&information_sur_la_destination,sizeof(information_sur_la_destination
));
if (nombre_de_caractere==SOCKET_ERROR)
printf("\nDesole, je ne peux pas envoyer les donnees du a l'erreur :
%d",WSAGetLastError());
else
//printf("\n envoi des donnees
: OK\n");
printf("");
}
void socketClose(void){
// ********************************************************
// Fermeture de la socket correspondant la commande socket()
// ********************************************************
erreur=closesocket(id_de_la_socket);
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %
d %d",erreur,WSAGetLastError());
else
//printf("Fermeture du socket
: OK\n");
printf("");
}
void socketClose2(void){
// ********************************************************
// Fermeture de la socket correspondant la commande socket()
// ********************************************************
erreur=closesocket(id_de_la_socket2);
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer la socket du a l'erreur : %
d %d",erreur,WSAGetLastError());
else
//printf("\nFermeture du socket
: OK\n");
printf("");
}
void winsockClosing(void){
// ********************************************************
// Quitte proprement le winsock ouvert avec la commande WSAStartup
// ********************************************************
erreur=WSACleanup(); // A appeler autant de fois qu'il a t ouvert.
if (erreur!=0)
printf("\nDesole, je ne peux pas liberer winsock du a l'erreur : %d
%d",erreur,WSAGetLastError());
else
//printf("\nFermeture Winsock
: OK\n");
printf("");
}

void socketRead(){
// ********************************************************
// Lire la socket une ip et un port d'coute
// ********************************************************
information_sur_la_source.sin_family=AF_INET;
information_sur_la_source.sin_addr.s_addr=INADDR_ANY; // Ecoute sur toutes
les IP locales
information_sur_la_source.sin_port=htons(33333); // Ecoute sur le port 333
33
erreur=bind(id_de_la_socket2,(struct sockaddr*)&information_sur_la_source,
sizeof(information_sur_la_source));
if (erreur!=0)
printf("\nDesole, je ne peux pas ecouter ce port : %d %d",erreur,WSA
GetLastError());
else
//printf("\nbind
: OK");
printf("");
}
void socketReceive(){
// ********************************************************
// Reception des donnes
// ********************************************************
tempo=sizeof(information_sur_la_source); // Passe par une variable afin d'
utiliser un pointeur
nombre_de_caractere=recvfrom(id_de_la_socket2,buffer,1515,0,(struct sockad
dr*)&information_sur_la_source,&tempo);
buffer[nombre_de_caractere]='\0'; // Permet de fermer le tableau aprs le co
ntenu des data, car la fonction recvfrom ne le fait pas
//printf("\nVoici les donnees : %s",buffer);
}
int main (int argc, char* argv[])
{
winsockOpen();
configuration();
while(quit == 0){
socketOpen();
socketOpen2();
menue();
switch (choix){
case 1:
sending("1");
socketRead();
socketReceive();
printf("\n****************************************************\n");
printf("*
La date est : %s \n", buffer);
printf("****************************************************\n");
break;
case 2:
sending("2");
socketRead();
socketReceive();
printf("\n****************************************************\n");
printf("*
L'heure est : %s \n", buffer);
printf("****************************************************\n");

break;
case 3:
sprintf(buffer,"%d",choix);
strcat(buffer," ");
printf("\n saisissez deux nombre -separe par un espace- : ");
gets(info);
strcat(buffer,info);
sending(buffer);
socketRead();
socketReceive();
printf("\n****************************************************\n");
//printf("*
La somme de %c et %c est : %s \n",info[0],info[2],
buffer);
printf("*
La somme est : %s\n", buffer);
printf("****************************************************\n");
break;
case 4:
sprintf(buffer,"%d",choix);
strcat(buffer," ");
printf("\n saisissez deux nombre -separe par un espace- : ");
gets(info);
strcat(buffer,info);
sending(buffer);
socketRead();
socketReceive();
printf("\n****************************************************\n");
printf("*
La difference est : %s\n", buffer);
printf("****************************************************\n");
break;
case 5:
sprintf(buffer,"%d",choix);
strcat(buffer," ");
printf("\n saisissez N nombre -separe par des espaces- : ");
gets(info);
strcat(buffer,info);
sending(buffer);
socketRead();
socketReceive();
printf("\n****************************************************\n");
printf("*
La somme des nombres est : %s\n", buffer);
printf("****************************************************\n");
break;
case 6:
sprintf(buffer,"%d",choix);
printf("saisissez un caractere : ");
gets(info);
strcat(buffer,info);
sending(buffer);
socketRead();
socketReceive();
printf("\n****************************************************\n");
printf("*
Le code ASCII est : %s\n", buffer);
printf("****************************************************\n");
break;
case 0:
quit = 1;
printf("Au revoir ...\n");
} // fin switch

socketClose();
socketClose2();
} // fin while
winsockClosing();
getch();
return 0;
}

Vous aimerez peut-être aussi