🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: chile.comp.programacion
Article #3153

problema programando socket

#3153
From: Rickoj
Date: Fri, 20 May 2005 22:35
118 lines
4861 bytes
Estoy programando un socket, un amigo me paso un socket de conexion ya
hecho, al cual le elimine lo que necesitaba y resulto en esto (codigo
completo)

********************* incio codigo********************************
    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <errno.h>
    #include <string.h>
    #include <netdb.h>
    #include <sys/types.h>
    #include <netinet/in.h>
    #include <sys/socket.h>
    #define PORT2 110 // puerto al que vamos a conectar
    #define MAXDATASIZE 3 * 1024 // m�ximo n�mero de bytes que se pueden
leer de una vez
    //Separa encabezado del mensaje
    void listarcorreo2(char host[200],char usuario[100],char pass[120])
    {
        int sockfd, numbytes,opc;
        char buf[MAXDATASIZE];
        struct hostent *he;
        struct sockaddr_in their_addr; // informaci�n de la direcci�n de
destino
        if ((he=gethostbyname(host)) == NULL) {  // obtener informaci�n de
m�quina
            perror("gethostbyname");
            exit(1);
        }
        if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
            perror("socket");
            exit(1);
        }
        their_addr.sin_family = AF_INET;    // Ordenaci�n de bytes de la
m�quina
        their_addr.sin_port = htons(PORT2);  // short, Ordenaci�n de bytes
de la red
        their_addr.sin_addr = *((struct in_addr *)he->h_addr);
        memset(&(their_addr.sin_zero),0, 8);  // poner a cero el resto de la
estructura
        if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct
sockaddr)) == -1) {
            perror("connect");
            exit(1);
        }
        if ((numbytes=recv(sockfd, buf, MAXDATASIZE-1, 0)) == -1) {
            perror("recv");
            exit(1);
        }
        buf[numbytes] = '\0';
      printf("\t%s\n",buf);
      if(strncmp(buf,"+OK ",3)!=0){
      printf("\tProblemas con el servidor POP\n");
      exit(-1);
      };
     sprintf(buf,"User %s\n", usuario);
     if((numbytes=send(sockfd,buf,strlen(buf),0)) == -1){
     perror("recv");
     exit(1);
     };
    memset(buf,0x0,MAXDATASIZE);
   if((numbytes=recv(sockfd,buf,MAXDATASIZE-1,0)) == -1){
   perror("recv"); exit(1);
   };
  printf("\t%s\n",buf);
  if(strncmp(buf,"+OK ",3)!=0){
  printf("\tProblemas con el usuario\n");
  exit(-1);
 };
  sprintf(buf,"pass %s\n",pass);
 if((numbytes=send(sockfd,buf,strlen(buf),0)) == -1){
   perror("recv");
   exit(1);
 };
 memset(buf,0x0,MAXDATASIZE);
 if((numbytes=recv(sockfd,buf,MAXDATASIZE-1,0)) == -1){
   perror("recv"); exit(1);
 };
 printf("\t%s\n",buf);
 if(strncmp(buf,"+OK ",3)!=0){
   printf("\tProblemas con el password\n");
   exit(-1);
 };
   printf("\tIngrese el correo que desea leer: ");
   scanf("%d",&opc);
   sprintf(buf,"RETR %d\n",opc);//con esto leo cada correo
   if((numbytes=send(sockfd,buf,strlen(buf),0)) == -1)
     {
     perror("recv");
     exit(1);
     };
   memset(buf,0x0,MAXDATASIZE);
   if((numbytes=recv(sockfd,buf,MAXDATASIZE-1,0)) == -1)
    {
    perror("recv");
    exit(1);
    };
    printf("\n\t%s",buf);
    sleep(5);
     sprintf(buf,"QUIT\n");
     if((numbytes=send(sockfd,buf,strlen(buf),0)) == -1){
          perror("recv");
          exit(1);
        };
        memset(buf,0x0,MAXDATASIZE);
        if((numbytes=recv(sockfd,buf,MAXDATASIZE-1,0)) == -1){
          perror("recv"); exit(1);
        };
        close(sockfd);
    }
****************** fin de codigo***************

el problema que tengo es que al ingresar el numero del correo que deso leer,
solo me entrega la primera linea es decir
"+OK xxxx bytes" (siendo xxx los bytes del correo), pareciera ser que solo
la primera linea se almacena en buf, agradezco ayuda ya que de sockets no se
mucho
Technical Details
Message-ID: <d6loku$hgf$1@news1.nivel5.cl>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!easynet-monga!easynet.net!news-lond.gip.net!news.gsl.net!gip.net!newsfeed.yul.equant.net!news-santiago!news1.nivel5.cl!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 1593cb,8a1971cc8ba8724e,start
X-Google-Thread: 1054ea,8a1971cc8ba8724e,start
X-Google-Attributes: gid1593cb,gid1054ea,public
X-Google-Language: SPANISH,ASCII
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!news.glorb.com!easynet-monga!easynet.net!news-lond.gip.net!news.gsl.net!gip.net!newsfeed.yul.equant.net!news-santiago!news1.nivel5.cl!not-for-mail
From: "Rick_oj" <rick_oj@[sin-esto]tutopia.com>
Newsgroups: chile.comp.programacion,chile.comp.unix
Subject: problema programando socket
Date: Fri, 20 May 2005 18:35:30 -0400
Organization: Varios
Lines: 121
Message-ID: <d6loku$hgf$1@news1.nivel5.cl>
NNTP-Posting-Host: pc-97-201-120-200.cm.vtr.net
X-Trace: news1.nivel5.cl 1116628447 17935 200.120.201.97 (20 May 2005 22:34:07 GMT)
X-Complaints-To: news@news1.nivel5.cl
NNTP-Posting-Date: Fri, 20 May 2005 22:34:07 +0000 (UTC)
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478