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

  FORUM HardWare.fr
  Programmation
  C

  Problème de connexion de client (Socket)

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Problème de connexion de client (Socket)

n°2353660
erdax
Posté le 12-05-2020 à 10:06:23  profilanswer
 

Salut à tous, j'ai fais un programme qui fonctionnait parfaitement pour établir une connexion client. J'ai changé de domicile donc de box et depuis le serveur créé accepte automatiquement et renvoie un socket = -1. Est ce que vous sauriez m'éclairer?
 
Merci!
 
Un bout de mon code serveur:

Code :
  1. /*.............Initialisation de la librairie winsock...........*/
  2.     WSADATA WSAData;
  3.     if(WSAStartup(MAKEWORD(1, 1), &WSAData))
  4.     {
  5.        printf("WSA n'a pas pu être initialisé.\n" );
  6.     }
  7. /*................................................................*/
  8.     setbuf(stdout, NULL);
  9.     setbuf(stderr, NULL);
  10.     User User[nombreDejoueurs-1];       //Structure du nombre de joueurs    //Celui qui héberge est également un User
  11.     printf("Rentrer votre Pseudo:" );
  12.     fflush(stdin);
  13.     fgets(User[nombreDejoueurs-1].pseudo,30,stdin);
  14.     printf("\nPseudo:" );
  15.     puts(User[nombreDejoueurs-1].pseudo);
  16. /*..................CREATION DES SOCKETS..............................*/
  17.     int socketServer = socket(AF_INET, SOCK_STREAM, 0);
  18.     struct sockaddr_in addrServer;
  19.     memset(&addrServer, 0, sizeof (addrServer));
  20.     addrServer.sin_addr.s_addr = inet_addr("localhost" );             //Insérer ici l'adresse privée du pc qui héberge le seveur
  21.     addrServer.sin_family = AF_INET;
  22.     addrServer.sin_port = htons(port);
  23.     bind(socketServer, (const struct sockaddr *)&addrServer, sizeof(addrServer));
  24.     printf("bind: %d\n",socketServer);
  25.     listen(socketServer,5);
  26.     printf("listen\n" );
  27.     SDL_Thread* ClientThread[nombreDejoueurs-2];            //Thread permettant de demander des infos à chaque client en même temps
  28.     int i=0;
  29.     ThreadUser *par;
  30.     printf("JOUEURS CONNECTES:\n" );
  31.     for(i=0;i<=nombreDejoueurs-2;i++)       //Connexions aux clients
  32.     {
  33.         struct sockaddr_in addrClient;
  34.         socklen_t csize = sizeof(addrClient);
  35.         int socketClient = accept(socketServer, (struct sockaddr *)&addrClient, &csize);
  36.         printf("\nSOCKET CLIENT:%d",socketClient);
  37.         par = malloc(sizeof(ThreadUser));     
  38.         par->User = &User[i];
  39.         par->socket = socketClient;
  40.         ClientThread[i] = SDL_CreateThread(ConnexionClient,"ThreadConnexionClient", par);
  41.         printf("\nSOCKET USER:%d", User[i].socket);
  42.     }

mood
Publicité
Posté le 12-05-2020 à 10:06:23  profilanswer
 

n°2353663
erdax
Posté le 12-05-2020 à 11:21:12  profilanswer
 

Le problème vient de adresse ip privé qui change avec la box. En la modifiant ça fonctionne! En revanche si je met "localhost" cela ne fonctionne pas

n°2353665
rufo
Pas me confondre avec Lycos!
Posté le 12-05-2020 à 12:00:13  profilanswer
 

C'est ce que j'aurais proposé en 1er, l'@IP. Pour le localhost, c'est normal, ça désigne la machine d'où est exécuté le programme. Donc, si tu l'exécutes depuis ton PC, localhost est ton PC (127.0.0.1), pas ta box. Or, l'IP 127.0.0.1 est pas routable sur un LAN privé qui est en général en 192.168.x.y.


Message édité par rufo le 12-05-2020 à 12:01:39

---------------
Astres, outil de help-desk GPL : http://sourceforge.net/projects/astres, ICARE, gestion de conf : http://sourceforge.net/projects/icare, Outil Planeta Calandreta : https://framalibre.org/content/planeta-calandreta
n°2354304
xilebo
noone
Posté le 23-05-2020 à 21:33:29  profilanswer
 

tu mets INADDR_ANY pour écouter sur toutes les interfaces. Si tu veux écouter sur une interface spécifique, tu mets l'adresse IP de l'interface. Si tu ne la connais pas parce qu'elle change tu peux la détecter en listant les interfaces. Voici un bout de code permettant de le faire  :
 
sous linux :
 

Code :
  1. /******************************************************************************
  2. Welcome to GDB Online.
  3. GDB online is an online compiler and debugger tool for C, C++, Python, PHP, Ruby,  
  4. C#, VB, Perl, Swift, Prolog, Javascript, Pascal, HTML, CSS, JS
  5. Code, Compile, Run and Debug online from anywhere in world.
  6. *******************************************************************************/
  7. #include <stdio.h>
  8. #include <net/if.h>
  9. #include <sys/ioctl.h>
  10. #include <sys/socket.h>
  11. #include <netinet/ether.h>
  12. #include <netinet/in.h>
  13. #include <arpa/inet.h>
  14. int main()
  15. {
  16.     struct ifconf ifc;
  17.     struct ifreq ifr[ 32 ];
  18.     int sock = socket(AF_INET, SOCK_DGRAM, 0);
  19.    
  20.     if (sock >= 0)
  21.     {
  22.      ifc.ifc_buf = (char *)ifr;
  23.      ifc.ifc_len = sizeof(ifr);
  24.      if (ioctl(sock, SIOCGIFCONF, &ifc) >= 0) {
  25.                 int size = ifc.ifc_len / sizeof(struct ifreq);
  26.                 for ( int i = 0 ; i < size ; i++ ) {
  27.                       printf("name : %s\n" , ifr[ i].ifr_name);
  28.                       printf("ip : %s\n" , inet_ntoa(((struct sockaddr_in *)&ifr[ i ].ifr_addr)->sin_addr));
  29.                       if ( ioctl( sock , SIOCGIFNETMASK , &ifr[ i ] ) >= 0 ){
  30.                         printf("mask : %s\n" , inet_ntoa(((struct sockaddr_in *)&ifr[ i ].ifr_addr)->sin_addr));
  31.                       }
  32.                       if ( ioctl( sock , SIOCGIFBRDADDR , &ifr[ i ] ) >= 0 ){
  33.                         printf("broadcast : %s\n",inet_ntoa(((struct sockaddr_in *)&ifr[ i ].ifr_broadaddr)->sin_addr));
  34.                       }
  35.                       if ( ioctl( sock , SIOCGIFHWADDR , &ifr[ i ] ) >= 0 )
  36.                       {
  37.                         printf("ether : %s\n ", ether_ntoa((struct ether_addr *)&ifr[ i ].ifr_hwaddr.sa_data ));
  38.                       }
  39.                 }
  40.            }
  41.     }
  42.     return 0;
  43. }


 
result sous gdb online ( https://www.onlinegdb.com/ ) :  
 


 
About • FAQ • Blog • Terms of Use • Contact Us • GDB Tutorial • Credits • Privacy
© 2016 - 2020 GDB Online
       Language  
C
   
main.c
   
   
input
name : lo                                                                                                                                                                                                                                        
ip : 127.0.0.1                                                                                                                                                                                                                                  
mask : 255.0.0.0                                                                                                                                                                                                                                
broadcast : 0.0.0.0                                                                                                                                                                                                                              
ether : 0:0:0:0:0:0                                                                                                                                                                                                                              
 name : eth0                                                                                                                                                                                                                                    
ip : 172.17.0.99                                                                                                                                                                                                                                
mask : 255.255.0.0                                                                                                                                                                                                                              
broadcast : 172.17.255.255                                                                                                                                                                                                                      
ether : 2:42:ac:11:0:63    


 
 
Sous windows, c'est un peu différent puisque ce n'est pas un système POSIX ( voir si ça marche sous win10 quand même ). Il faut utiliser la fonction GetAdaptersInfo : https://docs.microsoft.com/en-us/wi [...] aptersinfo
 
 
 

n°2354517
erdax
Posté le 27-05-2020 à 16:29:57  profilanswer
 

Merci pour vos réponses!


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

  Problème de connexion de client (Socket)

 

Sujets relatifs
problème de charactèresEnvoie et réception d'un tableau d'entier(socket
Création Serveur/Client[C] Problème de pointeur
[résolu] Problème jQueryProblème de saisie clavier sur Windows 10
Probleme avec socket et SDLProbleme avec SDL_ttf
probleme execution docker-compose en script bash 
Plus de sujets relatifs à : Problème de connexion de client (Socket)


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