Accueil > > > COMMUNICATION CLIENT SERVEUR , TRANSMITION DE DONNÉES PAR TRAMES , SOCKETS EN LANGAGE C
COMMUNICATION CLIENT SERVEUR , TRANSMITION DE DONNÉES PAR TRAMES , SOCKETS EN LANGAGE C
Information sur la source
Description
Ceci est le code source portable d'un Client Serveur simple en langage C. Ce code source permet tout simplement d'envoyer des chaines de caractère en du client au serveur; Le client envoi ces donnée par petit blocs (Trames) en mode texte, Et le serveur reçois la taille de données, puis reçois et reconstitue le texte à partir des Trames reçu.
Source
- /* Vous pouvez aussi télécharger le ZIP qui contiens les sources */
-
- /********************************************************************
- * Client simple portable (Socket en langage C)
- * Compilé avec GCC (sous Code::Blocks)
- *
- * Programed by Bug_Bug: one_piece_555(AT)hotmail.fr
- *
- * P.S. N'oubliez pas de linker avec la blibiothèque -lws2_32
- * voir libwsock32.a ou libws2_32.a ou ws2_32.lib ...
- *********************************************************************/
-
- /* Verifier que le compilateur est bien un compilateur C et pas C++ */
- #ifdef __cplusplus
- #error Be sure you are using a C compiler...
- #endif
-
- /**==================[ Si Windows ]================**/
- #if defined (WIN32)
- #include <winsock2.h>
-
- /**================[ Si Gnu/Linux ]================**/
- #elif defined (linux)
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
-
- #define INVALID_SOCKET -1
- #define SOCKET_ERROR -1
-
- #define closesocket(s) close (s)
- typedef int SOCKET;
- typedef struct sockaddr_in SOCKADDR_IN;
- typedef struct sockaddr SOCKADDR;
-
- /**===============[ Si Autre Systems ]==============**/
- #else
- #error not defined for this platform
- #endif /**========[ Fin teste portabilite' ]========**/
-
- /* Inclure les fichiers d'en-tete dont on a besoin */
- #include <stdio.h>
- #include <stdlib.h>
-
- #define SERVER_PORT_CONNEXION 3113
- char ip_server[15] = "127.0.0.1"; /* localhost */
-
- int App (void);
- int ConnectToServer (SOCKET*);
- int CommunicatWithServer (SOCKET);
- int SendData (SOCKET, char*);
-
-
- /**************************************************************************
- * Fonction main():
- * La fonction principale ( le point d'entrer )...
- ***************************************************************************/
- int main (void)
- {
- int flag_main_exit = EXIT_SUCCESS;
-
- /**==================[ Si Windows ]================**/
- #if defined (WIN32)
- /* Initialiser WSAStartup() */
- WSADATA wsa_data;
- if ( WSAStartup (MAKEWORD (2, 2), &wsa_data) )
- {
- perror("Error: init WSAStartup()");
- return EXIT_FAILURE;
- }
-
- puts ("WIN: winsock2: OK");
- #endif /**===========[ Fin teste Windows ]==========**/
-
-
- /* si app() retourne 1 c'est qu'elle a echoué */
- if ( App () )
- flag_main_exit = EXIT_FAILURE;
-
- /**==================[ Si Windows ]================**/
- #if defined (WIN32)
- /* Fermer ce qu'on a initialiser avec WSAStartup */
- WSACleanup ();
- #endif /**===========[ Fin teste Windows ]==========**/
-
- puts("\nPress any key to exit this program ...");
- getchar();
- return flag_main_exit;
- }
-
-
- /**************************************************************************
- * Fonction App():
- ** Le Client se connecte au serveur: ConnectToServer()
- ** Si connexion etablie, passer a' la communication CommunicatWithServer()
- ***************************************************************************/
- int App (void)
- {
- SOCKET fdsock;
- int sock_err;
-
- if ( ConnectToServer (&fdsock) )
- return 1; /* retourne 1 s'il y eu probleme */
-
- if ( CommunicatWithServer(fdsock) )
- return 1; /* retourne 1 s'il y eu probleme */
-
- /* Fin de communication, fermer socket proprement */
- shutdown (fdsock, 2);
- sock_err = closesocket (fdsock), fdsock = INVALID_SOCKET;
- if (sock_err == SOCKET_ERROR)
- {
- perror("Error: closesocket()");
- return 1;
- }
-
- return 0; /* Success */
- }
-
-
-
- /******************************************************************************
- * Fonction: ConnectToServer()
- ** Connexion avec le serveur (ip_server) sur le port SERVER_PORT_CONNEXION
- ******************************************************************************/
- int ConnectToServer ( SOCKET* fdSock )
- {
- /* Declaration d'une structure sin de type SOCKADDR_IN
- Qui contiendra les infos consernant le serveur, (IP, port, type) */
- SOCKADDR_IN sin;
-
- /* Creation du socket: */
- if ( (*fdSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
- {
- perror("socket.open");
- return 1; /* Creation du socket a echouer */
- }
-
- printf ("socket %d est maintenant ouvert en mode TCP/IP\n", *fdSock);
-
- /* l'IP du serveur avec lequel on va ce connecter: */
- sin.sin_addr.s_addr = inet_addr(ip_server);
- /* le port avec lequel on va ce connecter sur le serveur: */
- sin.sin_port = htons (SERVER_PORT_CONNEXION);
- /* famille du protocol (IP): */
- sin.sin_family = AF_INET;
-
-
- if ( connect(*fdSock, (SOCKADDR *)&sin, sizeof sin) == SOCKET_ERROR )
- {
- perror("Error: connect");
- return 1;
- }
-
- printf ( "Vous etes maintenant connecter au serveur %s sur le port %d\n",
- ip_server, SERVER_PORT_CONNEXION );
-
- return 0;
- }
-
-
-
-
- /******************************************************************************
- * Fonction: CommunicatWithServer()
- ** Premet d'echanger des donnees avec le Serveur. Recevoir ou Envoyer
- ******************************************************************************/
- int CommunicatWithServer ( SOCKET fdSock )
- {
- char data[500 + 1], *p = NULL;
- int sock_err, c;
-
- puts("\nVous pouvez saisir votre chaine :\n");
-
- do
- {
- printf("> "); fflush(stdout);
- fgets (data, sizeof(data), stdin);
- p = strchr(data,'\n');
- if (p == NULL)
- while ((c = fgetc(stdin)) != '\n' && c != EOF);
- else
- *p = '\0';
-
- /* Comande pour quitter */
- if( strcmp(data, "/quit") == 0 )
- break;
-
- /* envoyer data */
- if( (sock_err = SendData (fdSock, data)) == SOCKET_ERROR )
- return 1;
- }
- while (1);
-
- return 0;
- }
-
-
-
-
- /******************************************************************************
- * Fonction: SendData()
- ** Permet d'envoyer proprement un Bloc de données. Elle Retourne la taille
- de donnee envoye'
- ******************************************************************************/
- int SendData ( SOCKET socket, char *data )
- {
- int const len_data = strlen (data);
- int data_sent = 0, n;
- char textmode_lendata[25];
-
- sprintf (textmode_lendata, "%d\n", len_data);
-
- /* Envoi de la taille de data, (en mode texte) */
- n = send (socket, textmode_lendata, strlen(textmode_lendata), 0);
-
- if ( n < 0)
- {
- perror("Error: sending data size");
- return SOCKET_ERROR;
- }
-
- /* Envoyer le reste de donnees, s'il en reste */
- while (data_sent < len_data)
- {
- n = send (socket, data + data_sent, len_data - data_sent, 0);
-
- if (n >= 0)
- data_sent += n;
- else
- return SOCKET_ERROR;
- }
-
- return (data_sent);
- }
-
-
-
-
- /********************************************************************
- * Serveur simple portable (Socket en langage C)
- * Compilé avec GCC (sous Code::Blocks)
- *
- * Programed by Bug_Bug: one_piece_555(AT)hotmail.fr
- *
- * P.S. N'oubliez pas de linker avec la blibiothèque -lws2_32
- * voir libwsock32.a ou libws2_32.a ou ws2_32.lib ...
- *********************************************************************/
- /* Verification que le compilateur est bien un compilateur C et pas C++ */
- #ifdef __cplusplus
- #error Be sure you are using a C compiler...
- #endif
-
- /**==================[ Si Windows ]================**/
- #if defined (WIN32) /* Si on compile ce code source sous Windows, alors: */
- #include <winsock2.h>
-
- /**================[ Si Gnu/Linux ]================**/
- #elif defined (linux) /* Sinon si on compile sous linux, alors: */
-
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- #include <unistd.h>
-
- #define INVALID_SOCKET -1
- #define SOCKET_ERROR -1
-
- #define closesocket(s) close (s)
- typedef int SOCKET;
- typedef struct sockaddr_in SOCKADDR_IN;
- typedef struct sockaddr SOCKADDR;
-
- /**===============[ Si Autre Systems ]==============**/
- #else
- #error not defined for this platform
- #endif /**========[ Fin teste portabilite' ]========**/
-
- /* Inclure les fichiers d'en-tete dont on a besoin */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- #define DEBUG 0 /** Aclive: 1, Desactive: 0 **/
- #define SERVER_PORT_LISTENING 3113
-
- int App (void);
- int ConnectWithClient ( SOCKET*, SOCKET* );
- int CommunicatWithClient ( SOCKET );
- int RecvData ( SOCKET socket, char *data );
-
-
- /**************************************************************************
- * Fonction main():
- * La fonction principale ( le point d'entrer )...
- ***************************************************************************/
- int main (void)
- {
- int flag_main_exit = EXIT_SUCCESS;
-
- /**==================[ Si Windows ]================**/
- #if defined (WIN32)
- WSADATA wsa_data;
- /* Si l'initialisation sous windows echoue */
- if ( WSAStartup (MAKEWORD (2, 2), &wsa_data) )
- return EXIT_FAILURE;
-
- puts ("Windows: winsock2: OK");
- #endif /**===========[ Fin teste Windows ]==========**/
-
-
- /* si app() retourne 1 c'est qu'elle a echoue' */
- if ( App () )
- flag_main_exit = EXIT_FAILURE;
-
-
- /**==================[ Si Windows ]================**/
- #if defined (WIN32)
- /* Fermer ce qu'on a initialiser avec WSAStartup */
- WSACleanup ();
- #endif /**===========[ Fin teste Windows ]==========**/
-
- puts("\nPress any key to exit this program ...");
- getchar();
- return flag_main_exit;
- }
-
-
- /**************************************************************************
- * Fonction App():
- ** Le Serveur se connecte avec le client: ConnectToServer()
- ** Si connexion etablie, passer a' la communication CommunicatWithClient()
- ***************************************************************************/
- int App (void)
- {
- SOCKET sock, csock;
- int sock_err;
-
- if( ConnectWithClient (&csock, &sock) )
- return 1; /* retourne 1 s'il y eu probleme */
-
- if ( CommunicatWithClient(csock) )
- return 1; /* retourne 1 s'il y eu probleme */
-
- /* Fin de communication, fermer socket proprement */
- shutdown (sock, 2); shutdown (csock, 2);
- sock_err = closesocket (sock), sock = INVALID_SOCKET;
- if (sock_err == SOCKET_ERROR)
- {
- perror("Error: sock.closesocket()");
- return 1;
- }
-
- return 0;
- }
-
-
-
- /******************************************************************************
- * Fonction: ConnectWithClient()
- ** Connexion avec le client (avec sock), et recuperation d'infos
- ** sur le client, sur csock ...
- ******************************************************************************/
- int ConnectWithClient ( SOCKET *csock, SOCKET *sock )
- {
- /* Declaration de 2 structures de type SOCKADDR_IN */
- SOCKADDR_IN sin; /* pour le serveur */
- SOCKADDR_IN csin; /* pour le client */
-
- int sock_err, recsize;
-
- FILE* fichier = fopen("log.txt", "a");
-
- /* ouverture du socket */
- if ( (*sock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
- {
- perror("socket.open");
- return 1; /* Creation du socket a echouer */
- }
-
- printf ("socket %d est maintenant ouvert en mode TCP/IP\n", *sock);
-
- sin.sin_addr.s_addr = htonl (INADDR_ANY); /* Pas besoin d'IP */
- sin.sin_port = htons (SERVER_PORT_LISTENING); /* port d'ecoute */
- sin.sin_family = AF_INET; /* famille du protocol (IP) */
-
- /* bind du socket serveur (sock) avec la structure sin du serveur */
- sock_err = bind (*sock, (SOCKADDR *) &sin, sizeof sin);
- if (sock_err == SOCKET_ERROR)
- {
- perror("Error: bind");
- return 1;
- }
-
- /* ecoute sur le port */
- if ( (sock_err = listen (*sock, 5)) == SOCKET_ERROR)
- {
- perror("Error: listen");
- return 1;
- }
- printf ("ecoute sur le port %d...\n", SERVER_PORT_LISTENING);
-
- /* attendre et accepter la connection du client en recuperant les infos
- sur le client dans csin, et en créant le socket du client csock */
- recsize = (int) sizeof csin;
- if( (*csock = accept(*sock, (SOCKADDR*) &csin, &recsize)) == INVALID_SOCKET)
- {
- perror("Error: accept");
- return 1;
- }
- printf ("client connecte avec socket: %d de: %s port: %d\n",
- *csock, inet_ntoa (csin.sin_addr) ,htons (csin.sin_port));
-
- if(fichier != NULL)
- {
- fputs("-----------------------------------\n", fichier);
- fprintf(fichier, "client connecte avec socket: %d de: %s port: %d\n",
- *csock, inet_ntoa (csin.sin_addr) ,htons (csin.sin_port));
- fputs("-----------------------------------\n", fichier);
-
- fclose(fichier);
- }
-
- return 0;
- }
-
-
-
- /******************************************************************************
- * Fonction: CommunicatWithClient()
- ** Premet d'echanger des donnees avec le Client. Recevoir ou Envoyer
- ******************************************************************************/
- int CommunicatWithClient ( SOCKET csock )
- {
- char data[500 + 1];
- int sock_err, err_close;
- FILE* fichier = fopen("log.txt", "a");
-
- do
- {
- /* Attendre et recevoire des données envoyé par le client */
- if ( (sock_err = RecvData (csock, data)) == SOCKET_ERROR )
- break;
-
- else
- {
- data[sock_err] = '\0';
-
- printf("> %s\n", data);
-
- if(fichier != NULL)
- fprintf(fichier, "> %s\n", data);
- }
- }
- while (1);
-
- if(fichier != NULL)
- fclose(fichier);
-
- /* fermeture du socket client (csock), fin de la communication */
- shutdown (csock, 2);
- err_close = closesocket (csock), csock = INVALID_SOCKET;
- if (err_close == SOCKET_ERROR)
- {
- perror("Error: csock.closesocket()");
- return 1;
- }
-
- return 0;
- }
-
-
-
-
- /******************************************************************************
- * Fonction: RecvData()
- ** Permet de recevoir proprement un Bloc de données
- ** Retourne la taille de data reçu
- ******************************************************************************/
- int RecvData ( SOCKET sock, char *data )
- {
- size_t len_data;
- size_t data_receved = 0;
- char textmode_lendata[25], *pend;
- int n;
-
- /* Recevoir la taille de data */
- n = recv(sock, textmode_lendata, sizeof textmode_lendata - 1, 0);
-
- if (n <= 0)
- {
- if( n == 0 )
- printf("Le Client 'a Quitter...\n");
- return SOCKET_ERROR;
- }
- else
- {
- textmode_lendata[n] = '\0';
- len_data = strtol (textmode_lendata, &pend, 10);
- if (*pend != '\n')
- {
- printf("Invalid data size receved !\n");
- return -1;
- }
- }
-
- /* TantQu'on as pas recu tout, on continue ... */
- while (data_receved < len_data)
- {
- n = recv (sock, data + data_receved, sizeof data - 1, 0);
-
- if (n > 0)
- data_receved += n;
- else
- return SOCKET_ERROR;
- }
-
- return ((int)data_receved);
- }
-
/* Vous pouvez aussi télécharger le ZIP qui contiens les sources */
/********************************************************************
* Client simple portable (Socket en langage C)
* Compilé avec GCC (sous Code::Blocks)
*
* Programed by Bug_Bug: one_piece_555(AT)hotmail.fr
*
* P.S. N'oubliez pas de linker avec la blibiothèque -lws2_32
* voir libwsock32.a ou libws2_32.a ou ws2_32.lib ...
*********************************************************************/
/* Verifier que le compilateur est bien un compilateur C et pas C++ */
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
/**==================[ Si Windows ]================**/
#if defined (WIN32)
#include <winsock2.h>
/**================[ Si Gnu/Linux ]================**/
#elif defined (linux)
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close (s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
/**===============[ Si Autre Systems ]==============**/
#else
#error not defined for this platform
#endif /**========[ Fin teste portabilite' ]========**/
/* Inclure les fichiers d'en-tete dont on a besoin */
#include <stdio.h>
#include <stdlib.h>
#define SERVER_PORT_CONNEXION 3113
char ip_server[15] = "127.0.0.1"; /* localhost */
int App (void);
int ConnectToServer (SOCKET*);
int CommunicatWithServer (SOCKET);
int SendData (SOCKET, char*);
/**************************************************************************
* Fonction main():
* La fonction principale ( le point d'entrer )...
***************************************************************************/
int main (void)
{
int flag_main_exit = EXIT_SUCCESS;
/**==================[ Si Windows ]================**/
#if defined (WIN32)
/* Initialiser WSAStartup() */
WSADATA wsa_data;
if ( WSAStartup (MAKEWORD (2, 2), &wsa_data) )
{
perror("Error: init WSAStartup()");
return EXIT_FAILURE;
}
puts ("WIN: winsock2: OK");
#endif /**===========[ Fin teste Windows ]==========**/
/* si app() retourne 1 c'est qu'elle a echoué */
if ( App () )
flag_main_exit = EXIT_FAILURE;
/**==================[ Si Windows ]================**/
#if defined (WIN32)
/* Fermer ce qu'on a initialiser avec WSAStartup */
WSACleanup ();
#endif /**===========[ Fin teste Windows ]==========**/
puts("\nPress any key to exit this program ...");
getchar();
return flag_main_exit;
}
/**************************************************************************
* Fonction App():
** Le Client se connecte au serveur: ConnectToServer()
** Si connexion etablie, passer a' la communication CommunicatWithServer()
***************************************************************************/
int App (void)
{
SOCKET fdsock;
int sock_err;
if ( ConnectToServer (&fdsock) )
return 1; /* retourne 1 s'il y eu probleme */
if ( CommunicatWithServer(fdsock) )
return 1; /* retourne 1 s'il y eu probleme */
/* Fin de communication, fermer socket proprement */
shutdown (fdsock, 2);
sock_err = closesocket (fdsock), fdsock = INVALID_SOCKET;
if (sock_err == SOCKET_ERROR)
{
perror("Error: closesocket()");
return 1;
}
return 0; /* Success */
}
/******************************************************************************
* Fonction: ConnectToServer()
** Connexion avec le serveur (ip_server) sur le port SERVER_PORT_CONNEXION
******************************************************************************/
int ConnectToServer ( SOCKET* fdSock )
{
/* Declaration d'une structure sin de type SOCKADDR_IN
Qui contiendra les infos consernant le serveur, (IP, port, type) */
SOCKADDR_IN sin;
/* Creation du socket: */
if ( (*fdSock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
{
perror("socket.open");
return 1; /* Creation du socket a echouer */
}
printf ("socket %d est maintenant ouvert en mode TCP/IP\n", *fdSock);
/* l'IP du serveur avec lequel on va ce connecter: */
sin.sin_addr.s_addr = inet_addr(ip_server);
/* le port avec lequel on va ce connecter sur le serveur: */
sin.sin_port = htons (SERVER_PORT_CONNEXION);
/* famille du protocol (IP): */
sin.sin_family = AF_INET;
if ( connect(*fdSock, (SOCKADDR *)&sin, sizeof sin) == SOCKET_ERROR )
{
perror("Error: connect");
return 1;
}
printf ( "Vous etes maintenant connecter au serveur %s sur le port %d\n",
ip_server, SERVER_PORT_CONNEXION );
return 0;
}
/******************************************************************************
* Fonction: CommunicatWithServer()
** Premet d'echanger des donnees avec le Serveur. Recevoir ou Envoyer
******************************************************************************/
int CommunicatWithServer ( SOCKET fdSock )
{
char data[500 + 1], *p = NULL;
int sock_err, c;
puts("\nVous pouvez saisir votre chaine :\n");
do
{
printf("> "); fflush(stdout);
fgets (data, sizeof(data), stdin);
p = strchr(data,'\n');
if (p == NULL)
while ((c = fgetc(stdin)) != '\n' && c != EOF);
else
*p = '\0';
/* Comande pour quitter */
if( strcmp(data, "/quit") == 0 )
break;
/* envoyer data */
if( (sock_err = SendData (fdSock, data)) == SOCKET_ERROR )
return 1;
}
while (1);
return 0;
}
/******************************************************************************
* Fonction: SendData()
** Permet d'envoyer proprement un Bloc de données. Elle Retourne la taille
de donnee envoye'
******************************************************************************/
int SendData ( SOCKET socket, char *data )
{
int const len_data = strlen (data);
int data_sent = 0, n;
char textmode_lendata[25];
sprintf (textmode_lendata, "%d\n", len_data);
/* Envoi de la taille de data, (en mode texte) */
n = send (socket, textmode_lendata, strlen(textmode_lendata), 0);
if ( n < 0)
{
perror("Error: sending data size");
return SOCKET_ERROR;
}
/* Envoyer le reste de donnees, s'il en reste */
while (data_sent < len_data)
{
n = send (socket, data + data_sent, len_data - data_sent, 0);
if (n >= 0)
data_sent += n;
else
return SOCKET_ERROR;
}
return (data_sent);
}
/********************************************************************
* Serveur simple portable (Socket en langage C)
* Compilé avec GCC (sous Code::Blocks)
*
* Programed by Bug_Bug: one_piece_555(AT)hotmail.fr
*
* P.S. N'oubliez pas de linker avec la blibiothèque -lws2_32
* voir libwsock32.a ou libws2_32.a ou ws2_32.lib ...
*********************************************************************/
/* Verification que le compilateur est bien un compilateur C et pas C++ */
#ifdef __cplusplus
#error Be sure you are using a C compiler...
#endif
/**==================[ Si Windows ]================**/
#if defined (WIN32) /* Si on compile ce code source sous Windows, alors: */
#include <winsock2.h>
/**================[ Si Gnu/Linux ]================**/
#elif defined (linux) /* Sinon si on compile sous linux, alors: */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#define closesocket(s) close (s)
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
/**===============[ Si Autre Systems ]==============**/
#else
#error not defined for this platform
#endif /**========[ Fin teste portabilite' ]========**/
/* Inclure les fichiers d'en-tete dont on a besoin */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define DEBUG 0 /** Aclive: 1, Desactive: 0 **/
#define SERVER_PORT_LISTENING 3113
int App (void);
int ConnectWithClient ( SOCKET*, SOCKET* );
int CommunicatWithClient ( SOCKET );
int RecvData ( SOCKET socket, char *data );
/**************************************************************************
* Fonction main():
* La fonction principale ( le point d'entrer )...
***************************************************************************/
int main (void)
{
int flag_main_exit = EXIT_SUCCESS;
/**==================[ Si Windows ]================**/
#if defined (WIN32)
WSADATA wsa_data;
/* Si l'initialisation sous windows echoue */
if ( WSAStartup (MAKEWORD (2, 2), &wsa_data) )
return EXIT_FAILURE;
puts ("Windows: winsock2: OK");
#endif /**===========[ Fin teste Windows ]==========**/
/* si app() retourne 1 c'est qu'elle a echoue' */
if ( App () )
flag_main_exit = EXIT_FAILURE;
/**==================[ Si Windows ]================**/
#if defined (WIN32)
/* Fermer ce qu'on a initialiser avec WSAStartup */
WSACleanup ();
#endif /**===========[ Fin teste Windows ]==========**/
puts("\nPress any key to exit this program ...");
getchar();
return flag_main_exit;
}
/**************************************************************************
* Fonction App():
** Le Serveur se connecte avec le client: ConnectToServer()
** Si connexion etablie, passer a' la communication CommunicatWithClient()
***************************************************************************/
int App (void)
{
SOCKET sock, csock;
int sock_err;
if( ConnectWithClient (&csock, &sock) )
return 1; /* retourne 1 s'il y eu probleme */
if ( CommunicatWithClient(csock) )
return 1; /* retourne 1 s'il y eu probleme */
/* Fin de communication, fermer socket proprement */
shutdown (sock, 2); shutdown (csock, 2);
sock_err = closesocket (sock), sock = INVALID_SOCKET;
if (sock_err == SOCKET_ERROR)
{
perror("Error: sock.closesocket()");
return 1;
}
return 0;
}
/******************************************************************************
* Fonction: ConnectWithClient()
** Connexion avec le client (avec sock), et recuperation d'infos
** sur le client, sur csock ...
******************************************************************************/
int ConnectWithClient ( SOCKET *csock, SOCKET *sock )
{
/* Declaration de 2 structures de type SOCKADDR_IN */
SOCKADDR_IN sin; /* pour le serveur */
SOCKADDR_IN csin; /* pour le client */
int sock_err, recsize;
FILE* fichier = fopen("log.txt", "a");
/* ouverture du socket */
if ( (*sock = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
perror("socket.open");
return 1; /* Creation du socket a echouer */
}
printf ("socket %d est maintenant ouvert en mode TCP/IP\n", *sock);
sin.sin_addr.s_addr = htonl (INADDR_ANY); /* Pas besoin d'IP */
sin.sin_port = htons (SERVER_PORT_LISTENING); /* port d'ecoute */
sin.sin_family = AF_INET; /* famille du protocol (IP) */
/* bind du socket serveur (sock) avec la structure sin du serveur */
sock_err = bind (*sock, (SOCKADDR *) &sin, sizeof sin);
if (sock_err == SOCKET_ERROR)
{
perror("Error: bind");
return 1;
}
/* ecoute sur le port */
if ( (sock_err = listen (*sock, 5)) == SOCKET_ERROR)
{
perror("Error: listen");
return 1;
}
printf ("ecoute sur le port %d...\n", SERVER_PORT_LISTENING);
/* attendre et accepter la connection du client en recuperant les infos
sur le client dans csin, et en créant le socket du client csock */
recsize = (int) sizeof csin;
if( (*csock = accept(*sock, (SOCKADDR*) &csin, &recsize)) == INVALID_SOCKET)
{
perror("Error: accept");
return 1;
}
printf ("client connecte avec socket: %d de: %s port: %d\n",
*csock, inet_ntoa (csin.sin_addr) ,htons (csin.sin_port));
if(fichier != NULL)
{
fputs("-----------------------------------\n", fichier);
fprintf(fichier, "client connecte avec socket: %d de: %s port: %d\n",
*csock, inet_ntoa (csin.sin_addr) ,htons (csin.sin_port));
fputs("-----------------------------------\n", fichier);
fclose(fichier);
}
return 0;
}
/******************************************************************************
* Fonction: CommunicatWithClient()
** Premet d'echanger des donnees avec le Client. Recevoir ou Envoyer
******************************************************************************/
int CommunicatWithClient ( SOCKET csock )
{
char data[500 + 1];
int sock_err, err_close;
FILE* fichier = fopen("log.txt", "a");
do
{
/* Attendre et recevoire des données envoyé par le client */
if ( (sock_err = RecvData (csock, data)) == SOCKET_ERROR )
break;
else
{
data[sock_err] = '\0';
printf("> %s\n", data);
if(fichier != NULL)
fprintf(fichier, "> %s\n", data);
}
}
while (1);
if(fichier != NULL)
fclose(fichier);
/* fermeture du socket client (csock), fin de la communication */
shutdown (csock, 2);
err_close = closesocket (csock), csock = INVALID_SOCKET;
if (err_close == SOCKET_ERROR)
{
perror("Error: csock.closesocket()");
return 1;
}
return 0;
}
/******************************************************************************
* Fonction: RecvData()
** Permet de recevoir proprement un Bloc de données
** Retourne la taille de data reçu
******************************************************************************/
int RecvData ( SOCKET sock, char *data )
{
size_t len_data;
size_t data_receved = 0;
char textmode_lendata[25], *pend;
int n;
/* Recevoir la taille de data */
n = recv(sock, textmode_lendata, sizeof textmode_lendata - 1, 0);
if (n <= 0)
{
if( n == 0 )
printf("Le Client 'a Quitter...\n");
return SOCKET_ERROR;
}
else
{
textmode_lendata[n] = '\0';
len_data = strtol (textmode_lendata, &pend, 10);
if (*pend != '\n')
{
printf("Invalid data size receved !\n");
return -1;
}
}
/* TantQu'on as pas recu tout, on continue ... */
while (data_receved < len_data)
{
n = recv (sock, data + data_receved, sizeof data - 1, 0);
if (n > 0)
data_receved += n;
else
return SOCKET_ERROR;
}
return ((int)data_receved);
}
Fichier Zip
Historique
- 11 septembre 2007 11:29:16 :
- Ajout d'exécutables dans le ZIP.
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
sockets!!!!!!!!!!!!!!! [ par dletozeun ]
bonjour,J'ai encore un probleme avec les sockets:voila, g reussi a faire communiquer un serveur et un client su r 2 ordinateur distant mais cette conn
Sockets asychrones et client serveur TCP [ par argali ]
Bonjour,Est-ce qqun pourrait m'indiquer ou je pourrai trouver un cours complet sur les sockets asynchrones et leurs options (WSAAsyncSelect, FD_ACCEPT
problemme avec les sockets [ par sirion ]
Bonjour, j'ai un petit problemme avec les sockets. donc voila, j'ai essayer de faire un serveur/client qui recois 2msg chacun, mais ca merdouil
Récupérer l'IP du client (SOCKETS) dans une app client/serveur [ par supergrey ]
Bonjour, je voudrais savoir comment récupérer l'IP du client a partir du programme serveur après le sock=accept(...);Merci d'avance !
Problème de sockets... [ par nightlord666 ]
Bonjour ! J'essaie en ce moment de programmer un serveur multithread qui servira(peut-être) à un projet de MMORPG. Le serveur fonctionne nor
Sockets sous MFC [ par DJONJ ]
Bonjour, Peut être avez vous comme moi décortiqué un jour les codes sources exemples de Microsoft sous MFC CHATTER et CHATSRVR qui est un modèle cli
sockets [ par coucoumiya ]
bonjour, moi je travaille avec les sockets sous le procole de transmission TCP/IP. mon client est programmé avec Builderc++ sous windows. mon serveur
Commen créer un mmorpg (creer son serveur et client). [ par csphpchtml ]
Je voudrait savoir commen creer son MMORPG en faisant son serveur et client comme DBZ Online mais de dragonball Z.Je voudrait svoir ou je pourrait avo
|
Derniers Blogs
TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3TECHDAYS PARIS 2012 : SESSION PLEINIèRE JOUR 3 par ROMELARD Fabrice
Speaker: Bernard Ourghanlian Cette session est comme chaque jour transmise en live par BrainSonic, et j'ai donc suivi cette troisième pleinière par ce moyen sur mon iPad . Elle est dédiée comme chaque année à la mise en perspective de l'é...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE !MISHRA READER : UN LECTEUR RSS TRèS ZUNE STYLE EN OPEN SOURCE ! par Vko
Hier durant une session dédiée aux Techdays 2012, j'ai eu le plaisir d'annoncer la sortie de la Béta 2 de Mishra Reader. C'est quoi ? Pour les utilisateurs, c'est une vraie expérience de lecture de flux RSS sur Windows. Rien à voir avec les produit...
Cliquez pour lire la suite de l'article par Vko [FRAMEWORK 4] LES TASKS ET LE THREAD UI[FRAMEWORK 4] LES TASKS ET LE THREAD UI par fathi
Je viens de passer quelques temps au TechDay's et j'ai pu voir pas mal de session intéressante. Par contre une chose m'a un peu étonné lors de certaines de ces sessions qui abordaient les améliorations du framework .NET (donc le 4.5) : en gros, bea...
Cliquez pour lire la suite de l'article par fathi WORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBEWORKFLOW FOUNDATION 3 A UN PIED DANS LA TOMBE par JeremyJeanson
Depuis déjà un an, je conseille vivement les utilisateurs de Workflow Foundation 3 à migrer vers la version 4. L'information qui va suivre ne devrait donc pas trop prendre au dépourvu les personnes qui m'ont suivi. Je profite de ce poste, pour faire le re...
Cliquez pour lire la suite de l'article par JeremyJeanson TECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PCTECHDAYS PARIS 2012 : NOUVELLES TENDANCES DU POSTE DE TRAVAIL - BRING YOUR OWN PC par ROMELARD Fabrice
Speakers: Thierry Rapatout, Antoine Petit et Xavier Trebbia Cette session entre dans le cadre des RDV Décideurs des TechDays 2012, elle est liée à la consumérisation de l'IT et la mise en place du "DeskTop as a Service" dans de plus en ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
RE : CXIMAGERE : CXIMAGE par rt15
Cliquez pour lire la suite par rt15
Logiciels
Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|