Unix Programming - Telnet client in C

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2005 > Telnet client in C





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Telnet client in C
Enos Meroka

2005-05-20, 7:55 am

Hallo,
Am trying to establish a telnet session with my C program. However, I
seem not to able to get the prompt, after supplying the username and
passowrd. could someone assist me in troubleshoot my code and inform me
where i might be getting it wrong.

I would appreciate if I got ideas of how to achieve this in a simpler
way. Below is my code
========================================
===========================

#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>

int printFromSocket(int sd);

void error(char *msg)
{
perror(msg);
exit(0);
}

int main(int argc, char *argv[])
{
int sockfd, portno, n, rs;
struct sockaddr_in serv_addr;
struct hostent *server;

char buffer[2056];
if (argc < 3) {
fprintf(stderr,"usage %s hostname port\n", argv[0]);
exit(0);
}
portno = atoi(argv[2]);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
server = gethostbyname(argv[1]);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serv_addr.sin_addr.s_addr,
server->h_length);
serv_addr.sin_port = htons(portno);
if (connect(sockfd,&serv_addr,sizeof(serv_addr)) < 0)
error("ERROR connecting");
//printf("Please enter the message: ");
bzero(buffer,2056);

strcpy(buffer,"");
//fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,2056);

// Sending first sequence of string characters
bzero(buffer,2056);
sprintf (buffer, "%c%c%c",255,252,36);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket2");
bzero(buffer,2056);
// Sending second sequence of string characters
bzero(buffer,2056);
sprintf (buffer, "%c%c%c",255,252,24);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);

sprintf (buffer, "%c%c%c%c%c%c",255,252,24,255,252,32);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,2056);

sprintf (buffer,
"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c" ,255,252,32,255,253,1,255,253,3,255,252,
31,255,252,33,255,251,1);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,2056);

sprintf (buffer,
"%c%c%c%c%c%c%c%c%c%c%c%c" ,255,252,31,255,252,33,255,253,1,255,252
,1);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
bzero(buffer,2056);

sleep(1);
n = read(sockfd,buffer,2055);
if (n < 0)
error("ERROR reading from socket");
printf("%s\t",buffer);



//---------------------------------------------------------------------------------------
bzero(buffer,2056);
//sending the username
sprintf (buffer, "meroka%\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
sleep(1);
bzero(buffer,2056);
n = read(sockfd,buffer,2055);
if (n < 0)
error("ERROR reading from socket7");
printf("%s",buffer);

//---------------------------------------------------------------------------------------

bzero(buffer,2056);
//sending the password
sprintf (buffer, "meroka123\n");
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
sleep(1);
bzero(buffer,2056);
n = read(sockfd,buffer,2055);
if (n < 0)
error("ERROR reading from socket7");
if ((rs = printFromSocket(sockfd))!= -1)
printf("\n Am Here!!");

//---------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------

bzero(buffer,2056);
strcpy(buffer,"");
//fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket");
printf("%s",buffer);

bzero(buffer,2056);
// Sending first sequence of string characters
bzero(buffer,2056);
sprintf (buffer, "%c%c%c",255,252,36);
n = write(sockfd,buffer,strlen(buffer));
if (n < 0)
error("ERROR writing to socket2");
bzero(buffer,2056);
printf("%s",buffer);

//---------------------------------------------------------------------------------------
return 0;
}

int printFromSocket(int sd)
{
int len = 0;
int result;
char buf[2056];
do
{
len = read(sd, buf, 2055);
printf("\n%s",buf);
}while(len > 0);
result =input_timeout (sd, 5);
if (result == 0)
{
printf("\n No output");
return 0;
}
else
return -1;

}

int input_timeout (int filedes, unsigned int seconds)
{
fd_set set, wrset;
struct timeval timeout;

/* Initialize the file descriptor set. */
FD_ZERO (&set);
FD_ZERO (&wrset);
FD_SET (filedes, &set);
FD_SET (1, &wrset);

/* Initialize the timeout data structure. */
timeout.tv_sec = seconds;
timeout.tv_usec = 0;

/* select returns 0 if timeout, 1 if input available, -1 if error. */
return (select (FD_SETSIZE,&set, NULL, NULL,&timeout));
}
========================================
===========================

Barry Margolin

2005-05-21, 2:52 am

In article <1116580744.701131.87930@g47g2000cwa.googlegroups.com>,
"Enos Meroka" <en_meroka@yahoo.com> wrote:

> Hallo,
> Am trying to establish a telnet session with my C program. However, I
> seem not to able to get the prompt, after supplying the username and
> passowrd. could someone assist me in troubleshoot my code and inform me
> where i might be getting it wrong.


I haven't really looked at your code, but are you handling option
negotiation? I didn't find the word "option" anywhere in your program.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Enos Meroka

2005-05-23, 2:48 am

Barry Margolin wrote:
>
> I haven't really looked at your code, but are you handling option
> negotiation? I didn't find the word "option" anywhere in your

program.
------------------

Thanks for your reply?
What do you mean by option negotiation?

Måns Rullgård

2005-05-23, 5:59 pm

"Enos Meroka" <en_meroka@yahoo.com> writes:

> Barry Margolin wrote:
>
> Thanks for your reply?
> What do you mean by option negotiation?


Read RFC854 and related documents.

--
Måns Rullgård
mru@inprovide.com
Doug Mitton

2005-05-23, 8:49 pm

"Enos Meroka" <en_meroka@yahoo.com> wrote:

>Barry Margolin wrote:
>program.
>------------------
>
>Thanks for your reply?
>What do you mean by option negotiation?


I can't find the links I used recently in my program but do a few
DejaNews.com searches, thats how I found them. Basically there is an
exchange/negotiation of capabilities between the client and the server
immediately after connection. It has to complete successfully before
the session is initiated.
--
------------------------------------------------
http://www3.sympatico.ca/dmitton
SPAM Reduction: Remove "x." from my domain.
------------------------------------------------
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com