Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Telnet client in C

Reply
Thread Tools

Telnet client in C

 
 
Enos Meroka
Guest
Posts: n/a
 
      05-20-2005
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,2 55,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));
}
================================================== =================

 
Reply With Quote
 
 
 
 
Grumble
Guest
Posts: n/a
 
      05-20-2005
Enos Meroka 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.


Wrong newsgroup. Try comp.unix.programmer

I see you've already posted to comp.unix.programmer

Newsgroups: comp.unix.programmer
Subject: Telnet client in C
Date: 20 May 2005 02:19:04 -0700

Multi-posting (copying the same message to different groups) is
frowned upon.
 
Reply With Quote
 
 
 
 
Enos Meroka
Guest
Posts: n/a
 
      05-20-2005
Thanks for your reply,
I realized that I had posted my message in the wrong newsgroup. That's
when i posted it on the other newsgroup.
Apologies are in order. Am sorry for the inconvenience caused.

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      05-20-2005
In article <d6kds0$nit$(E-Mail Removed)>,
Grumble <(E-Mail Removed)> wrote:
>Enos Meroka wrote some off-topic clutter:

....
To which you responded:
>Wrong newsgroup. Try comp.unix.programmer
>
>I see you've already posted to comp.unix.programmer
>
>Newsgroups: comp.unix.programmer
>Subject: Telnet client in C
>Date: 20 May 2005 02:19:04 -0700
>
>Multi-posting (copying the same message to different groups) is
>frowned upon.


Right. But then again, if you are going to post to an inappropriate
newsgroup, it is better to multi-post there, than to cross-post, since then
the followups (generated from the appropriate newsgroups) don't also end up
there. I.e., most users (of all email and news oriented type software)
are too stupid to trim followups - they just go ahead and "reply all".

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      05-20-2005
Enos Meroka wrote:
>
> 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>


None of these (save stdio.h) are valid in a conforming C program.
Thus this is off topic in c.l.c, where we deal with standard
portable C only. Find a group that deals with your system.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Telnet - attempting to initiate a telnet session within an established telnet session Carcarius Ruby 0 12-06-2007 03:26 AM
Need to talk to a telnet server and send a telnet break Jim Isaacson C Programming 5 11-05-2004 09:17 PM
Java SSH / Telnet client Source Ted Java 4 09-19-2004 03:11 PM
Is there java telnet-client API to connect telelnet server? rambo88 Java 17 07-01-2004 04:01 AM
How can I telnet when I dont have specific access to a telnet client Jack B. Pollack Computer Support 4 07-24-2003 08:58 PM



Advertisments