Communication client serveur , transmition de données par trames , sockets en langage c

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 / Exemple :


/* 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); }

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.