Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********************************TESTEUR**********************************************/
- /*************************************************************************************/
- int main(int argc, char * argv[]) {
- // port client
- unsigned short portMiroirTCP;
- unsigned short portTestUDP;
- // socket client
- int sockTCP;
- struct sockaddr_in addr_miroirTCP;
- struct hostent * hostMiroir,* hostTest;
- int buf[1];
- buf[0] = 10;
- // socket local
- if ((sockTCP = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
- perror("socket");
- exit(EXIT_FAILURE);
- }
- // adresse IP miroir
- if ((hostMiroir = gethostbyname(argv[1])) == NULL) {
- perror("gethostbyname");
- exit(EXIT_FAILURE);
- }
- // adresse IP miroir
- if ((hostTest = gethostbyname("localhost")) == NULL) {
- perror("gethostbyname");
- exit(EXIT_FAILURE);
- }
- // préparation adresse serveur
- portMiroirTCP = (unsigned short) 5000;
- addr_miroirTCP.sin_family = AF_INET;
- addr_miroirTCP.sin_port = htons(portMiroirTCP);
- bcopy(hostMiroir->h_addr, &addr_miroirTCP.sin_addr, hostMiroir->h_length);
- // demande de connexion serveur
- if (connect(sockTCP, (struct sockaddr *) &addr_miroirTCP, sizeof(addr_miroirTCP)) == -1) {
- perror("connect");
- exit(EXIT_FAILURE);
- }
- // connexion OK
- printf("Connexion acceptée.\n");
- // envoi de la commande
- ssize_t wr;
- if ((wr = write(sockTCP, buf, sizeof(int))) == -1) {
- perror("write");
- exit(EXIT_FAILURE);
- }
- //creation socket UDP
- int sockUDP;
- //declaration des compteurs
- int nbMsg = 0;
- int nbInvers = 0;
- struct sockaddr_in addr_miroirUDP,addr_testUDP;
- // Creating socket file descriptor
- if ( (sockUDP = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
- perror("socket creation failed");
- exit(EXIT_FAILURE);
- }
- memset(&addr_miroirUDP, 0, sizeof(addr_miroirUDP));
- memset(&addr_testUDP, 0, sizeof(addr_testUDP));
- // Filling server information
- portTestUDP = (unsigned short) 4000 ;
- addr_testUDP.sin_family = AF_INET; // IPv4
- addr_testUDP.sin_addr.s_addr = htonl(INADDR_ANY);
- addr_testUDP.sin_port = htons(portTestUDP);
- bcopy(hostTest->h_addr, &addr_testUDP.sin_addr, hostTest->h_length);
- // Bind the socket with the server address
- if ( bind(sockUDP, (const struct sockaddr *)&addr_testUDP,
- sizeof(addr_testUDP)) < 0 )
- {
- perror("bind failed");
- exit(EXIT_FAILURE);
- }
- int n,msg,msg_av = -1;
- socklen_t len_addr_miroirUDP;
- //SELECT
- fd_set fd_set_read;
- /* Initialize the set of active sockets. */
- FD_ZERO (&fd_set_read);
- FD_SET (sockTCP, &fd_set_read);
- FD_SET (sockUDP, &fd_set_read);
- if (select (sockUDP+1, &fd_set_read, NULL, NULL, NULL) < 0)
- {
- perror ("select");
- exit (EXIT_FAILURE);
- }
- if (FD_ISSET (sockUDP, &fd_set_read)){
- while(nbMsg<buf[0]){
- if((n = recvfrom(sockUDP, (int *)&msg, sizeof(int),0,(struct sockaddr *) &addr_miroirUDP,(socklen_t *)&len_addr_miroirUDP)) ==-1){
- perror("recvfrom failed");
- exit(EXIT_FAILURE);
- }
- printf("message recu = %d\n",msg);
- nbMsg++;
- if(msg_av > msg){
- nbInvers++;
- }
- msg_av = msg;
- }
- }
- if (FD_ISSET (sockTCP, &fd_set_read)){
- // réception de la réponse de CONCERT
- char fin[4];
- ssize_t rd;
- if ((rd = read(sockTCP, fin, 4)) != 4) {
- perror("read");
- exit(EXIT_FAILURE);
- }
- printf("message FIN recu\n");
- FD_CLR(sockUDP,&fd_set_read);
- FD_CLR(sockTCP,&fd_set_read);
- }
- //traitement des resultats
- printf("recu %d %% de messages\n",(nbMsg/buf[0]*100));
- printf("%d %% de messages invercee\n",(nbInvers/buf[0]*100));
- close(sockUDP);
- close(sockTCP);
- printf("les sockets ferme\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement