Article View: es.comp.os.linux.programacion
Article #11683Re: Sockets en c++
From: JamesIncope
Date: Wed, 17 Oct 2001 14:32
Date: Wed, 17 Oct 2001 14:32
99 lines
4506 bytes
4506 bytes
Hola, yo tambine estoy programando con sockets y tambien tengo algunos problemas.
Abre el socket con la opcion SOCK_DATAGRAM (no se si es el nombre correcto, miralo en
el 'man') cuando quieras utilizar 'recv' y 'send'.
Si lo abres con SOCK_STREAM entonces debes utilizar 'read' y 'write'.
Si te ha servido de algo respondeme...Gracias
----------------------------------------------------------------------------
skalvin wrote:
> Hola otra vez!
> Tengo un problema con un programa que estoy haciendo en C++ para
> conectarme con un servidor FTP (exactamente el proFTP de mi m�quina).
> Bueno el caso es que conectar se conecta bien, pero al enviar el usuario
> y dem�s, ya no avanza. Esto es lo que he hecho:
>
> ...
> #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[X]="usuario"; //X= n� de d�gitos que tiene el usuario
> char pass[X]="contrase�a"; //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("192.168.1.1");
>
> /* 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("recv");
> exit(1);
> }
> /* Visualizamos lo recibido */
> buf[numbytes] = '\0';
> printf("Recivido: %s\n",buf);
>
> /* Mandamos por el socket el USER */
> if (write(sockfd,user,sizeof(user),0) > 0)
> printf("\n USER Enviado OK \n");
> /* Recibimos los datos del servidor */
> if ((numbytes=read(sockfd, buf, MAXDATASIZE, 0)) == -1)
> {
> perror("recv");
> exit(1);
> }
> /* Visualizamos lo recibido */
> buf[numbytes] = '\0';
> printf("Recivido: %s\n",buf);
>
> /* Mandamos por el socket el PASS */
> if (send(sockfd,pass,sizeof(pass),0) > 0)
> printf("\n PASS Enviado OK \n");
> /* Recibimos los datos del servidor */
> if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
> {
> perror("recv");
> exit(1);
> }
> /* Visualizamos lo recibido */
> buf[numbytes] = '\0';
> printf("Recivido: %s\n",buf);
>
> close(sockfd);
> return 0;
> }
>
> Compila perfectamente. Al ejecutarlo esto es lo que pasa:
> skalvin@BASAJAUN:~$ ./cliente
> Recivido: 220 ProFTP 1.2.0pre10 Server (Debian) [BASAJAUN]
>
> USER Enviado OK
>
> Y aqu� se queda.
>
> Alguna idea?
>
> PD: acabo de empezar a programar con sockets en linux
Technical Details
Message-ID:
<3BCD9688.55FE1DEF@est.fib.upc.es>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsswitch.lcs.mit.edu!bloom-beacon.mit.edu!news.rediris.es!news.cesca.es!news.upc.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 06:32:53 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsswitch.lcs.mit.edu!bloom-beacon.mit.edu!news.rediris.es!news.cesca.es!news.upc.es!not-for-mail From: JamesIncope <d6522117@est.fib.upc.es> Newsgroups: es.comp.os.linux.programacion Subject: Re: Sockets en c++ Date: Wed, 17 Oct 2001 15:32:40 +0100 Organization: Facultat d'Informàtica de Barcelona Lines: 100 Distribution: es Message-ID: <3BCD9688.55FE1DEF@est.fib.upc.es> References: <9qi1if$o126k$1@ID-83805.news.dfncis.de> NNTP-Posting-Host: c6s301pc44.fib.upc.es Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: defalla.upc.es 1003325578 27883 147.83.148.48 (17 Oct 2001 13:32:58 GMT) X-Complaints-To: newsmngr@upc.es NNTP-Posting-Date: 17 Oct 2001 13:32:58 GMT To: skalvin <skalvinANTISPAM@antisocial.com> X-Mailer: Mozilla 4.76 [en] (Win98; U) X-Accept-Language: en,es,ca