Need assistance from a programmer please!

CodeBloxCodeBlox Member Posts: 1,363 ■■■■□□□□□□
So, I was messing around with the sockets API and thought "Why not try and write a telnet client?". What was trying to do was connect from a linux system to a telnet server on a windows system. I am able to do so with the "built-in" telnet client from linux to the windows computer. Problem I'm having is that when I run my code even just to read the login prompt, I get gibberish back. Can someone tell me if telnet requires some form of negotiation before one is to read from the socket?

The socket code is the usual seen in a lot of tutorials. But the part I'm interested in is the read portion.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <errno.h>

#define MAX_BUFFER 2048
#define PORT 23

void sys_err( ){
    printf( "%s\n", strerror( errno ) );
    exit( 0 );

int main( int argc, char ** argv ){

    int socket_fd, index = 0, nread = 0;
    char buffer[ MAX_BUFFER + 1 ]; 
    struct sockaddr_in servaddr;

    if( argc != 2 )
        sys_err( );
    if( ( socket_fd = socket( AF_INET, SOCK_STREAM, 0 ) ) < 0 )
        sys_err( );    

    bzero( &servaddr, sizeof( servaddr ) );
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons( PORT );

    if( inet_pton( AF_INET, argv[1], &servaddr.sin_addr ) <= 0)
        sys_err( );

    if( connect( socket_fd, ( struct sockaddr * ) &servaddr, sizeof( servaddr ) ) < 0 )
        sys_err( );

//  for( ; ; ){ 

        nread = read( socket_fd, buffer, MAX_BUFFER );
        buffer[ nread + 1 ] = '\0';
        printf("%d bytes read \n", nread );
        fputs( buffer, stdout );

//  }

    return 0;

The read reports 22 bytes read when I know that the whole windows telnet prompt at the beginning is much larger than 22 bytes. I have a buffer with plenty of space too. Why am I getting gibberish back?

This is what I expect to get:
Connected to
Escape character is '^]'.
Welcome to Microsoft Telnet Service 

login: codeblox
Logon failure: unknown user name or bad password.

Login Failed

login: admin

Welcome to Microsoft Telnet Server.
C:\Documents and Settings\Admin>

Instead I get:
root@UbuntuVM:/home/codeblox# ./Desktop/Code/telnet_client
21 bytes read 
Suggestions? And sorry if my coding sucks!!
Currently reading: Network Warrior, Unix Network Programming by Richard Stevens


  • Options
    CoolhandlukeCoolhandluke Member Posts: 118
    Hi CodeBlox,

    I would memset buffer before each read rather than terminate it with a NULL.

    i.e memset(buffer,NULL,sizeof(MAX_BUFFER+1));

    It looks as though telnet does indeed have a header (I assume its to provide terminal emulation information). I have worked a lot with other connections (HTTP, FTP etc) and non provide any header information simply for client connections.

    If you take out the comments on the for loop you will see it working.

Sign In or Register to comment.