Linux上C语言写的简易telnet客户端

telnet.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>  
#include <netinet/in.h> 
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <time.h>
#include <stdarg.h>

#define ERR_EXIT(m, ...) \  
    do { \  
        fprintf(stderr, m"\n", ##__VA_ARGS__); \  
        log(m"\n", ##__VA_ARGS__); \
        exit(EXIT_FAILURE); \  
    } while (0) 
#define MAX_BUFF_LEN 1024

void log(const char *fmt, ...)
{
    char buffer[MAX_BUFF_LEN] = {0};
    va_list args;
    time_t timep;
    time(&timep);
    struct tm *p = gmtime(&timep);
    FILE* fp = fopen("telnet.log", "a+");
    if(fp){
        snprintf(buffer, MAX_BUFF_LEN, "[%.4d-%.2d-%.2d %.2d:%.2d:%.2d][%d]", (1900+p->tm_year),(1+p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, getpid());
        va_start(args, fmt);
        vsnprintf(buffer + strlen(buffer), MAX_BUFF_LEN - strlen(buffer) - 1, fmt, args);
        va_end(args);
        fwrite(buffer, strlen(buffer), 1, fp);
        fclose(fp);
    }
}

ssize_t      /* Write "n" bytes to a descriptor. */
writen(int fd, const void *vptr, size_t n){
    size_t  nleft;
    ssize_t  nwritten;
    const char *ptr;
    ptr = vptr;
    nleft = n;
    while (nleft > 0) {
        if ( (nwritten = write(fd, ptr, nleft)) <= 0) {
            if (nwritten < 0 && errno == EINTR)
                nwritten = 0;  /* and call write() again */
            else
                return(-1);   /* error */
        }
        nleft -= nwritten;
        ptr   += nwritten;
    }
    return(n);
}

int main(int argc,char **argv){
    if(argc < 3){
        fprintf(stderr, "Usage: %s ServerIPAddress ServerPort\n",argv[0]);
        return 0;
    }

    fd_set rset;
    FD_ZERO(&rset);
    int sock = socket(AF_INET,SOCK_STREAM,0);
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;    //internet协议族
    struct hostent *h = gethostbyname(argv[1]);
    server_addr.sin_addr = *((struct in_addr *)h->h_addr);
    server_addr.sin_port = htons(atoi(argv[2]));
    if(connect(sock,(struct sockaddr*)&server_addr, sizeof(struct sockaddr_in)) < 0){
        ERR_EXIT("connect to %s:%s failed", argv[1], argv[2]);
    }

    fprintf(stdout, "% 9d\n", getpid());
    fflush(stdout);

    int nready;  
    int maxfd;  
    int fd_stdin = fileno(stdin);
    int fd_stdout = fileno(stdout);
    if (fd_stdin > sock)  
        maxfd = fd_stdin;  
    else  
        maxfd = sock;  
    char sendbuf[1024*4] = {0};  
    char recvbuf[1024*4] = {0};  

    while (1){  
        FD_SET(fd_stdin, &rset);  
        FD_SET(sock, &rset);  
        nready = select(maxfd + 1, &rset, NULL, NULL, NULL); //select返回表示检测到可读事件  
        if (nready == -1) ERR_EXIT("select error");  
        if (nready == 0) continue;  
        if (FD_ISSET(sock, &rset)){  
            int ret = read(sock, recvbuf, sizeof(recvbuf)); //按行读取  
            if (ret == -1) ERR_EXIT("read from socket error");  
            else if (ret  == 0) ERR_EXIT("server close");  //服务器关闭  

            writen(fd_stdout, recvbuf, ret);
            memset(recvbuf, 0, sizeof(recvbuf));  
        }  
        if (FD_ISSET(fd_stdin, &rset)){  
            int ret = read(fd_stdin, sendbuf, sizeof(sendbuf));
            if(ret > 0) {
                writen(sock, sendbuf, ret);  
                memset(sendbuf, 0, sizeof(sendbuf));
            }else{
                ERR_EXIT("read from stdin error");                
            }
        }  
    }  
    close(sock);  
    exit(0);
}

编译方法:

gcc -o telnet telnet.c

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏技术点滴

Windows注册表操作基础代码

Windows注册表操作基础代码     Windows下对注册表进行操作使用的一段基础代码Reg.h: #pragma once #include<asser...

22490
来自专栏Pulsar-V

OpenCV编译参数一览

全部参数一览 //Path to a program. ANT_EXECUTABLE:FILEPATH=D:/apache-ant-1.10.1/bin/ant...

86660
来自专栏智能计算时代

Designing your SaaS Database for Scale with Postgres

? If you’re building a SaaS application, you probably already have the notion o...

22540
来自专栏数据库新发现

How to Install Orabm

http://www.eygle.com/special/How.to.Install.Orabm.TPC.htm

17530
来自专栏算法修养

FZU 2105 Digits Count

 Problem 2105 Digits Count Accept: 444    Submit: 2139 Time Limit: 10000 mSec ...

30650
来自专栏H2Cloud

Tips for thrift

Introduction I have designed and developed game servers successfully with thrift...

36850
来自专栏我的博客

Zend Framework自动加载、简单路由配置(Bootstrap.php)

<?php class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { /* * 自动加...

36280
来自专栏一个会写诗的程序员的博客

Spring Data之@Query中的org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML

Spring Data之@Query中的org.hibernate.hql.internal.QueryExecutionRequestException: N...

39420
来自专栏运维

linux文件树

以前有意找这方面的资料,今天突然发现在系统中就有 linux系统用man hier solaris用man  filesystem 其结果如下     ...

11720
来自专栏数据和云

DBA生存警示:保护现场不要让事情更糟

张乐奕 云和恩墨副总经理 Oracle ACE 总监 ITPUB Oracle数据库管理版版主、Oracle高可用版版主、ACOUG联合创始人 今天收到一个发过...

317120

扫码关注云+社区

领取腾讯云代金券