Article View: es.comp.os.linux.programacion
Article #11689Re: Sockets en c++
From: Enrique
Date: Wed, 17 Oct 2001 19:31
Date: Wed, 17 Oct 2001 19:31
107 lines
3828 bytes
3828 bytes
>
> USER Enviado OK
>
> Y aquí se queda.
>
> Alguna idea?
>
> PD: acabo de empezar a programar con sockets en linux
Hola, ante todo decirte que yo tampoco tengo mucha idea.
Después de darle unas vueltas, me ha empezado a funcionar
cuando en write y send he cambiado sizeof(...) por strlen(...).
Este es el código que a mi me funciona:
#include <stdio.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 21
#define MAXDATASIZE 300
int main(int argc, char *argv[])
{
int sockfd, numbytes;
/* Buffer donde se reciben los datos */
char buf[MAXDATASIZE];
char user[256]="USER anonymous\n"; //X= nº de dígitos que tiene
el usuario
char pass[256]="PASS mimail@wanadoo.es\n"; //X=nºde dig. de la
contraseña
struct sockaddr_in their_addr;
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr.s_addr = inet_addr("208.196.145.20");
/* Intentamos conectarnos con el servidor */
if (connect(sockfd, (struct sockaddr *)&their_addr,sizeof(struct
sockaddr)) == -1)
{
perror("connect");
exit(1);
}
/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv1");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("Recibido: %s\n",buf);
/* Mandamos por el socket el USER */
if (write(sockfd,user,strlen(user)) > 0)
printf("\n USER Enviado OK \n");
/* Recibimos los datos del servidor */
if ((numbytes=read(sockfd, buf, MAXDATASIZE)) == -1)
{
perror("recv2");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("Recibido: %s\n",buf);
/* Mandamos por el socket el PASS */
if (send(sockfd,pass,strlen(pass),0) > 0)
printf("\n PASS Enviado OK \n");
/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv3");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("Recivido: %s\n",buf);
close(sockfd);
return 0;
}
Un saludo.
Technical Details
Message-ID:
<3BCDDC89.55F1EB70@wanadoo.es>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!opentransit.net!newsfeed.comtenidos.com!newsfeeder.wanadoo.es!not-for-mailReferences:
<9qi1if$o126k$1@ID-83805.news.dfncis.de>Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII X-Google-Thread: f86c8,c6402803ae6cf871 X-Google-Attributes: gidf86c8,public X-Google-ArrivalTime: 2001-10-17 12:36:08 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!opentransit.net!newsfeed.comtenidos.com!newsfeeder.wanadoo.es!not-for-mail From: Enrique <nesii@wanadoo.es> Newsgroups: es.comp.os.linux.programacion Subject: Re: Sockets en c++ Date: Wed, 17 Oct 2001 21:31:21 +0200 Organization: Wanadoo Lines: 107 Message-ID: <3BCDDC89.55F1EB70@wanadoo.es> References: <9qi1if$o126k$1@ID-83805.news.dfncis.de> NNTP-Posting-Host: 62-36-132-87.dialup.uni2.es Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: news.wanadoo.es 1003346799 26745 62.36.132.87 (17 Oct 2001 19:26:39 GMT) X-Complaints-To: usenet@wanadoo.es NNTP-Posting-Date: Wed, 17 Oct 2001 19:26:39 +0000 (UTC) X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.4.4-4GB i686) X-Accept-Language: es-ES, en