1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#if defined (WIN32)
#include <winsock2.h>
typedef int socklen_t;
#else // SI OS X OU LINUX
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <netinet/in.h>
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;
#define closesocket(s) close(s)
#define INVALID_SOCKET -1
#define SOCKET_ERROR -1
#endif
#if defined (linux)
#include <arpa/inet.h>
#else// OS X
#include <pthread.h>
#include <string.h>
#include <fcntl.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#define PORT 1234
void host(SOCKET *sock,SOCKET *csock)
{
#if defined (WIN32)
WSADATA WSAData;
int erreur = WSAStartup(MAKEWORD(2,2), &WSAData);// initialise bibliotheque WinSock.
Puis à la fin, placez celui-ci :
#else
int erreur = 0;
#endif
/* Socket et contexte d'adressage du serveur */
SOCKADDR_IN sin;//contexte d'adressage
socklen_t recsize = sizeof(sin);
/********************************************/
/* Socket et contexte d'adressage du client */
SOCKADDR_IN csin;
socklen_t crecsize = sizeof(csin);
/********************************************/
if(!erreur)//Pour windows
{
/* Création d'une socket */
*sock=socket(AF_INET, SOCK_STREAM, 0);
if(*sock== INVALID_SOCKET)
perror("socket");
printf("La socket %d est maintenant ouverte en mode TCP/IP\n", *sock);
/* Configuration */
sin.sin_addr.s_addr = htonl(INADDR_ANY); /* Adresse IP automatique */
sin.sin_family = AF_INET; /* Protocole familial (IP) */
sin.sin_port = htons(PORT); /* Listage du port */
if(bind(*sock, (SOCKADDR*)&sin, recsize)== SOCKET_ERROR)
perror("bind");
/*********************************************************/
/* Démarrage du listage (mode server) */
if(listen(*sock, 5)!=SOCKET_ERROR); //Si l ecoute est valide
printf("Listage du port %d...\n", PORT);
printf("Un client se connecte avec la socket %d de :\n", htonl(INADDR_ANY));
/* Attente pendant laquelle le client se connecte */
printf("Patientez pendant que le client se connecte sur le port %d...\n", PORT);
*csock = accept(*sock, (SOCKADDR*)&csin, &crecsize);
printf("Un client se connecte avec la socket %d de :%d\n", *csock, htons(csin.sin_port));
}
}
void closeSock(SOCKET *sock,SOCKET *csock)
{
/* Fermeture de la socket client et de la socket serveur */
printf("Fermeture de la socket client\n");
closesocket(*csock);
printf("Fermeture de la socket serveur\n");
closesocket(*sock);
printf("Fermeture du serveur terminée\n");
#if defined (WIN32)
WSACleanup();
#endif
}
int main()
{
int i=0;
SOCKET sock = 0,csock = 0;
host(&sock,&csock);
struct in_addr addr;
struct hostent *localhost;
int ret = -1;
char localname[1000] = {0};
// Recuperer le nom du PC local
ret = gethostname( &(localname[0]), 999);
// Recuperer une structure decrivant un hote a partir de son nom
localhost = gethostbyname( &(localname[0]) );
// Extraire l'adresse (on suppose qu'elle est de type IPv4)
addr = * ( (struct in_addr*)localhost->h_addr_list[0] );
char msg[50]="" ;
printf ("\n le serveur va lire \n");
for (i=0; i<1000; i++) {
send(csock, "123456789", 50, 0);
}
for (i=0; i<1000; i++) {
recv (csock, msg, 50,0);
printf ("\n le message envoye par le client est : %s ", msg);
}
closeSock(&sock, &csock);
return 0;
} |
Partager