468,554 Members | 1,369 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,554 developers. It's quick & easy.

telnet

Hi,
I want to make a program what connect through telnet protocol to a router,
and run commands for configure the router. I read over sockets and I made
the program above, but it donīt work fine.

After this, I read document in Internet about this, and I believe that I
have to implement the protocol. But I donīt know.

Someone can help me.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 23 /* El puerto donde se conectara
*/
#define MAXDATASIZE 100 /* maxima cant. De bytes que se pueden recibir
en una llamada a recv */
int main(int argc, char *argv[])
{
int sockfd, numbytes; /* Contendra el numero de bytes
recibidos despues de llamar a recv() */
int newfd; /* las transferencias de datos se realizar
mediante newfd */
char buf[MAXDATASIZE]; /* Buffer donde se reciben los datos */
struct hostent *he; /* Se utiliza para
convertir el nombre del host a su direccion IP */
struct sockaddr_in their_addr; /* direccion del server donde se
conectara */
/* Tratamiento de la linea de comandos. */
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
/* Convertimos el nombre del host a su direccion IP */
if ((he=gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
/* Creamos el socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
/* Establecemos their_addr con la direccion del server */
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);
/* 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);
}
/*prueba de envio de datos*/
if (send(sockfd, "abcd\n", 18, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/*prueba de envio de datos*/
if (send(sockfd, "exit\n", 6, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);
/* Devolvemos recursos al sistema */
close(sockfd);
return 0;
}

thanks,

Fernando

Nov 14 '05 #1
2 2787
On 2004-11-15, newsf <ne***@grupalia.com> wrote:
Hi,
I want to make a program what connect through telnet protocol to a router,
and run commands for configure the router. I read over sockets and I made
the program above, but it donīt work fine.

After this, I read document in Internet about this, and I believe that I
have to implement the protocol. But I donīt know.

Someone can help me.

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>
#define PORT 23 /* El puerto donde se conectara
*/
#define MAXDATASIZE 100 /* maxima cant. De bytes que se pueden recibir
en una llamada a recv */
int main(int argc, char *argv[])
{
int sockfd, numbytes; /* Contendra el numero de bytes
recibidos despues de llamar a recv() */
int newfd; /* las transferencias de datos se realizar
mediante newfd */
char buf[MAXDATASIZE]; /* Buffer donde se reciben los datos */
struct hostent *he; /* Se utiliza para
convertir el nombre del host a su direccion IP */
struct sockaddr_in their_addr; /* direccion del server donde se
conectara */
/* Tratamiento de la linea de comandos. */
if (argc != 2)
{
fprintf(stderr,"usage: client hostname\n");
exit(1);
}
/* Convertimos el nombre del host a su direccion IP */
if ((he=gethostbyname(argv[1])) == NULL)
{
herror("gethostbyname");
exit(1);
}
/* Creamos el socket */
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
{
perror("socket");
exit(1);
}
/* Establecemos their_addr con la direccion del server */
their_addr.sin_family = AF_INET;
their_addr.sin_port = htons(PORT);
their_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(their_addr.sin_zero), 8);
/* 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);
}
/*prueba de envio de datos*/
if (send(sockfd, "abcd\n", 18, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/* Recibimos los datos del servidor */
if ((numbytes=recv(sockfd, buf, MAXDATASIZE, 0)) == -1)
{
perror("recv");
exit(1);
}
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);

/*prueba de envio de datos*/
if (send(sockfd, "exit\n", 6, 0) == -1)
perror("send");
/* Visualizamos lo recibido */
buf[numbytes] = '\0';
printf("%s\n",buf);
/* Devolvemos recursos al sistema */
close(sockfd);
return 0;
}

thanks,

Fernando


Have a close look at this:
http://www.ecst.csuchico.edu/~beej/guide/net/html/

Greetings, Chris

--
Christian Staudenmayer
University of Ulm, Germany
cs****@gmail.com
Nov 14 '05 #2
newsf <ne***@grupalia.com> spoke thus:
Ok Chris, I had a client an a server socket. I had intall the server socket
in a PC and run. After I had intall the client socket in another PC. The
result of this is OK. The server send the messages at the client and
viceversa. ?Where I can find information about this?......


1) Please don't top-post.
2) You'll do a lot better on a different newsgroup, possibly
comp.unix.programmer.
3) Please read the FAQ and welcome messages:

http://www.ungerhu.com/jxh/clc.welcome.txt
http://www.eskimo.com/~scs/C-faq/top.html
http://benpfaff.org/writings/clc/off-topic.html

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Yannick Turgeon | last post: by
4 posts views Thread by Donnal Walter | last post: by
2 posts views Thread by john brown | last post: by
reply views Thread by CJ | last post: by
2 posts views Thread by eight02645999 | last post: by
17 posts views Thread by ravimath | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.