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:
Trying 192.168.0.2...
Connected to 192.168.0.2.
Escape character is '^]'.
Welcome to Microsoft Telnet Service
login: codeblox
password:
Logon failure: unknown user name or bad password.
Login Failed
login: admin
password:
*===============================================================
Welcome to Microsoft Telnet Server.
*===============================================================
C:\Documents and Settings\Admin>
Instead I get:
root@UbuntuVM:/home/codeblox# ./Desktop/Code/telnet_client 192.168.0.2
21 bytes read
��%������'���
Suggestions? And sorry if my coding sucks!!