Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Experiment 6: A Telnet Client

Lesson 6 of 11 in the free Network Programming Lab notes on Siksha Sarovar, written by Rohit Jangra.

Program Statement

A minimal line-mode telnet: connect to any host and port, then relay stdin↔socket with select — this is the str_cli function from Unit 2:

/* mini_telnet.c — usage: ./mini_telnet host port */
int main(int argc, char **argv) {
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    struct sockaddr_in srv = {0};
    srv.sin_family = AF_INET; srv.sin_port = htons(atoi(argv[2]));
    inet_pton(AF_INET, argv[1], &srv.sin_addr);
    if (connect(fd, (struct sockaddr *)&srv, sizeof(srv)) < 0) {
        perror("connect"); exit(1);
    }
    fd_set rset; char buf[4096]; int n;
    for (;;) {
        FD_ZERO(&rset); FD_SET(0, &rset); FD_SET(fd, &rset);
        select(fd + 1, &rset, NULL, NULL, NULL);
        if (FD_ISSET(fd, &rset)) {                   /* network → screen */
            if ((n = read(fd, buf, sizeof(buf))) <= 0) break;
            write(1, buf, n);
        }
        if (FD_ISSET(0, &rset)) {                    /* keyboard → network */
            if ((n = read(0, buf, sizeof(buf))) <= 0) { shutdown(fd, SHUT_WR); FD_CLR(0,&rset); continue; }
            write(fd, buf, n);
        }
    }
    close(fd); return 0;
}

Testing

Test it against your echo server, or speak HTTP by hand: type GET / HTTP/1.0 followed by a blank line against port 80.

Record Task

Real telnet adds IAC option negotiation (WILL/WONT/DO/DONT) on top of this raw byte relay — describe it in your record. Theory: Unit 4 "Remote Login".