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

  FORUM HardWare.fr
  Programmation
  C

  Socket et fdopen

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Socket et fdopen

n°1004339
xiluoc
un pc pour les unirs ....
Posté le 08-03-2005 à 03:14:12  profilanswer
 

:hello: ,
On dois faire un petit clien ftp pour jouer avec les sockets en C.
j arrive a me connecter avec le programme suivant. mais LOGIN ne me renvoie aucune reponse j en deduit que rien nas etait envoye.
 
via telnet

Code :
  1. pompeii:a1:64 % telnet 127.0.0.1 1034
  2. Trying 127.0.0.1...
  3. Connected to 127.0.0.1.
  4. Escape character is '^]'.
  5. ACK comp341 server ready
  6. LOGIN test
  7. ACK  Log in ID OK
  8. LOGOUT
  9. ACK SFTP server exit
  10. Connection to 127.0.0.1 closed by foreign host.


 
mon prog, les message envoye au server doivent se terminet par char(255).
 

Code :
  1. # define HOST "pompeii.ics.mq.edu.au"
  2. # include <sys/types.h>
  3. # include <sys/socket.h>
  4. # include <netinet/in.h>
  5. # include <arpa/inet.h>
  6. # include <netdb.h>
  7. # include <stdio.h>
  8. #include <string.h>
  9. int main (int argc, char* argv[])
  10. {
  11.   char c;
  12.   register struct hostent *hp = 0;
  13.   struct sockaddr_in remoteaddr;
  14.   struct servent *sp;
  15.   FILE *send, *receive;
  16.   int sock, port;
  17.   hp = gethostbyname (argv[1]); /* Take it from command line */
  18.   /* Extract the remote address and put it into the structure for socket */
  19.   remoteaddr.sin_family = hp->h_addrtype;
  20.   bcopy (hp->h_addr_list[0], (caddr_t) &remoteaddr.sin_addr, hp->h_length);
  21.   sock = socket (remoteaddr.sin_family, SOCK_STREAM, 0);
  22.   if (sock < 0)
  23.   {
  24.     printf("error connecting to socket of type %s \n", remoteaddr.sin_family);
  25.     exit (1);
  26.   }
  27.   sp = getservbyname ( "comp341","tcp" );
  28.   if (sp == 0)
  29.   {
  30.     exit (1);
  31.   }
  32.   else
  33.     port = sp->s_port;
  34.     remoteaddr.sin_port = port;
  35.   if (comp341_connect (sock, &remoteaddr, sizeof (remoteaddr)) < 0)
  36.   {
  37.     printf("error while calling connect() \n" );
  38.     exit (1);
  39.   }
  40.  
  41.   receive = fdopen (sock, "r" );
  42.   send = fdopen (sock, "w" );
  43. while ((c = fgetc(receive)) != EOF) {
  44.             putchar(c); }
  45. printf("\n" );
  46. int length;
  47. char *logi="LOGIN s4046342";
  48. length = strlen(logi);
  49. logi[length]=(char)0xFF;
  50. fprintf(send, "%s", logi);
  51. while ((c = fgetc(receive)) != EOF) {
  52.             putchar(c); }
  53. printf("\n" );
  54. }


 
Ou est le problem il y a til de grosses erreurs ?  
merci pour votre aide


Message édité par xiluoc le 08-03-2005 à 11:08:26
mood
Publicité
Posté le 08-03-2005 à 03:14:12  profilanswer
 

n°1004362
jan0
Posté le 08-03-2005 à 08:16:28  profilanswer
 

il me semble que les commandes doivent se terminer par \r\n

n°1004598
xiluoc
un pc pour les unirs ....
Posté le 08-03-2005 à 11:11:31  profilanswer
 

j ai updater le code du poste 1, ca coince au niveua de lenvoie de donnee, je dois envoyer cette commande :  
The characters 'L', 'O', 'G', 'I', 'N' followed by white space then the user's Unix login name and terminated by a byte value 255.
 
donc LOGIN s4046342 +le byte 255
est ce que (char)0xFF est la bonne solution ?
pour tester je necrit pas vers le socket mais vers un fichier et sa me sors  :  LOGIN s4046342\377
 
 

n°1004695
xiluoc
un pc pour les unirs ....
Posté le 08-03-2005 à 12:10:43  profilanswer
 

fflush(send) a resolu le problem :)


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

  Socket et fdopen

 

Sujets relatifs
[Java] ObjectInputStream (socket.getInputStream()) bloquant !connexion par socket via XML
Problème de reception socketProblème socket UDP
[Perl] Probleme de fork & serveur sockettransfert de fichier socket client-serveur
Socket Client+Serveur MultiThread[c#] lire plusieurs lignes sur une socket ?
[C / Linux] Jeu bataille navale avec Socketselect et socket enrobé de sucre (fdopen mode lecture) -> probleme ???
Plus de sujets relatifs à : Socket et fdopen


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