Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3109 connectés 

  FORUM HardWare.fr
  Programmation
  C

  problème socket unix - lecture

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

problème socket unix - lecture

n°692592
mildred
Posté le 05-04-2004 à 13:37:10  profilanswer
 

Voila, j'essaie de faire un petit serveur mais je ny arrive pas. J'utilise les sockets unix.
 
Jai des problèmes pour lire les données du socket. Voici mon code:

Code :
  1. send(t, "Bienvenue\n", sizeof("Bienvenue\n" ), 0);
  2. quit = 0;
  3. fprintf(stderr, "Listening ...\n" );
  4. while(!quit)
  5. {
  6. char buffer[BUFSIZE];
  7. if(recv(t, buffer, BUFSIZE, 0) > 0)
  8.  {
  9.  if(buffer[0] == "q" ) quit = 1;
  10.  else printf("unknown request: %s", buffer);
  11.  }
  12. }
  13. close(t);
  14. fprintf(stderr, "Connexion closed ...\n" );


 
Lorsque je me connexte avec telnet, je n'ai pas le message de bienvenue alors que sur la console du serveur, ile me marque bien "Listening ..."
Après, quoi que je fasse sur mon telnet, rien ne se passe sur mon serveur ...
Pouvez vous maider ...
Merci
 
Voici mon fichier complet pour ceux que ca intéresse:
config.c

Code :
  1. /***************************************************************************
  2. *   Copyright (C) 2004 by mildred                                         *
  3. *   mildred@louve                                                         *
  4. *                                                                         *
  5. *   This program is free software; you can redistribute it and/or modify  *
  6. *   it under the terms of the GNU General Public License as published by  *
  7. *   the Free Software Foundation; either version 2 of the License, or     *
  8. *   (at your option) any later version.                                   *
  9. *                                                                         *
  10. *   This program is distributed in the hope that it will be useful,       *
  11. *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12. *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13. *   GNU General Public License for more details.                          *
  14. *                                                                         *
  15. *   You should have received a copy of the GNU General Public License     *
  16. *   along with this program; if not, write to the                         *
  17. *   Free Software Foundation, Inc.,                                       *
  18. *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19. ***************************************************************************/
  20. #define true 1
  21. #define false ""
  22. #define YES 1
  23. #define NO 0
  24. #define PORT 59300
  25. #define BACKLOG 100
  26. #define BUFSIZE 250
  27. #define SOCKET_ERROR -1


serv.c

Code :
  1. /***************************************************************************
  2. *   Copyright (C) 2004 by mildred                                         *
  3. *   mildred@louve                                                         *
  4. *                                                                         *
  5. *   This program is free software; you can redistribute it and/or modify  *
  6. *   it under the terms of the GNU General Public License as published by  *
  7. *   the Free Software Foundation; either version 2 of the License, or     *
  8. *   (at your option) any later version.                                   *
  9. *                                                                         *
  10. *   This program is distributed in the hope that it will be useful,       *
  11. *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
  12. *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
  13. *   GNU General Public License for more details.                          *
  14. *                                                                         *
  15. *   You should have received a copy of the GNU General Public License     *
  16. *   along with this program; if not, write to the                         *
  17. *   Free Software Foundation, Inc.,                                       *
  18. *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include <config.h>
  22. #endif
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <netinet/in.h>
  26. #include <sys/socket.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>*
  29. #include "config.c"
  30. int main(int argc, char *argv[])
  31. {
  32. int sockfd, newfd, size, count_req, t, quit;
  33. pid_t pid;
  34. struct sockaddr_in local;
  35. struct sockaddr_in remote;
  36. bzero(&local, sizeof(local));
  37. local.sin_family = AF_INET;
  38. local.sin_port = htons( PORT );
  39. local.sin_addr.s_addr = INADDR_ANY;
  40. bzero(&(local.sin_zero), 8);
  41. if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == SOCKET_ERROR)
  42.  {
  43.  fprintf(stderr, "Socket creation error\n" );
  44.  return EXIT_FAILURE;
  45.  }
  46. if(bind(sockfd, (struct sockaddr *)&local, sizeof(struct sockaddr)) == SOCKET_ERROR)
  47.  {
  48.  fprintf(stderr, "Can't link socket\n" );
  49.  return EXIT_FAILURE;
  50.  }
  51. if(listen(sockfd, BACKLOG) == SOCKET_ERROR)
  52.  {
  53.  fprintf(stderr, "Can't listen\n" );
  54.  return EXIT_FAILURE;
  55.  }
  56. size = sizeof(struct sockaddr_in);
  57. while(t = accept(sockfd, (struct sockaddr *)&remote, &size) || true)
  58.  {
  59.  if(t == SOCKET_ERROR)
  60.   {
  61.   fprintf(stderr, "Can't accept connexion\n" );
  62.   return EXIT_FAILURE;
  63.   continue;
  64.   }
  65.  else pid = fork ();
  66.  if (pid > 0)
  67.   {
  68.   printf("Forking. PID: %d\n", pid);
  69.   }
  70.  else if (pid == 0)
  71.   {
  72.   send(t, "Bienvenue\n", sizeof("Bienvenue\n" ), 0);
  73.   quit = 0;
  74.   fprintf(stderr, "Listening ...\n" );
  75.   while(!quit)
  76.    {
  77.    char buffer[BUFSIZE];
  78.    if(recv(t, buffer, BUFSIZE, 0) > 0)
  79.     {
  80.     if(buffer[0] == "q" ) quit = 1;
  81.     else printf("unknown request: %s", buffer);
  82.     }
  83.    }
  84.   close(t);
  85.   fprintf(stderr, "Connexion closed ...\n" );
  86.   }
  87.  else
  88.   {
  89.   fprintf(stderr, "Can't fork(): Unknown error: %d\n", pid);
  90.   fprintf(stderr, "Type `man 2 pid` for more details\n" );
  91.   return EXIT_FAILURE;
  92.   }
  93.  }
  94. close(newfd);
  95. return EXIT_SUCCESS;
  96. }

mood
Publicité
Posté le 05-04-2004 à 13:37:10  profilanswer
 

n°692658
cricri_
Posté le 05-04-2004 à 14:51:16  profilanswer
 

je sais que ce n'est pas le pb mais ça c'est pas top :
 #define false ""

n°692716
mildred
Posté le 05-04-2004 à 15:09:28  profilanswer
 

A vrai dire, je n'utilise pas la constante false
Et il n'y a pas d'erreurs de compilation
Donc je ne crois pas que c'est ca ...

n°692945
mildred
Posté le 05-04-2004 à 17:28:37  profilanswer
 

J'ai quelque peu modifié mon programme mais ca ne marche toujours pas.
Voila la partie qui se charge d'envoyer un message au client:

Code :
  1. else if(pid == 0)
  2. {
  3. fprintf(stderr, "Listening ...\n" );
  4. char *msg = "Bienvenue\n";
  5. int len, bytes_sent;
  6. len = strlen(msg);
  7. bytes_sent = send(t, msg, len, 0);
  8. if(bytes_sent==-1) printf(stderr, "Error when sending\n" );
  9. else fprintf(stderr, "Sent %d bytes\n", bytes_sent);
  10. close(t);
  11. fprintf(stderr, "Connexion closed ...\n" );
  12. }


Et lorsque je me connecte avec telnet, toujours rien et sur la console de mon serveru j'ai:

Forking. PID: 25811
Listening ...
Connexion closed ...


Avez vous des idées ?
Merci
 
PS: si vous avez un exemple d'un serveur simple, ca m'intéresserait ...

n°693137
Deaddy
Posté le 06-04-2004 à 08:19:06  profilanswer
 

if(buffer[0] == "q" ) quit = 1;  
-> if(buffer[0] == 'q') quit = 1;
 
et pourquoi tu fais un fork() au juste ?


Message édité par Deaddy le 06-04-2004 à 08:22:55
n°693547
mildred
Posté le 06-04-2004 à 13:25:00  profilanswer
 

le fork() sert a pouvoir accepter plusieurs clients ...
Ensuite, avant de détecter la touche 'q', j'aimmerais bien poivoir envoyer un message vers le client (ca ne marche pas ...)
Ensuite, dans mon code précedant, le compilateur me disait que je faisais une comapraison avec un pointeur et une chaine (buffer[0] == "q" ). C'est pour ca que ca ne marchait pas ...
 
Merci de la réponse ...


Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  problème socket unix - lecture

 

Sujets relatifs
[C++] Socket - envoyer URLpetit probleme debutant
PostGres - Gros problème d'user[C] probleme de lseek & write
problème Php / popupProblème de joker * et selection for-each
[C/C++] Probleme de link sous VC6probleme pointeur void alors qu'un cast de char* marche :\
problème de téléchargment de fichierProbléme fonction mail
Plus de sujets relatifs à : problème socket unix - lecture


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR