简单的telnet客户端1

来源:百度文库 编辑:神马文学网 时间:2024/04/27 21:35:20
发信人: hightman (鳗), 信区: Socket
标  题: Re: 谁写过简单的telnet客户端啊
发信站: 飘渺水云间 (Fri Aug  8 20:14:10 2003), 转信
 
拷下来存成 .c 文件,
 
/* hightman.bbs@bbs.hightman.net     */
/* Unix: gcc -o d d.c                */
/* Cygwin: gcc -o d d.c -DWIN32      */
/* 2002/12/16                        */
 
#include
#include
#ifdef WIN32
#include
#endif
#include
#include
 
 
unsigned char buf[2048];
int result, readnum;
int fd;
 
fd_set readfds;
 
struct termios t1;
struct timeval tv;
 
void break_check() {
    int i;
 
    tv.tv_sec = 0;
    tv.tv_usec = 0;
    FD_ZERO(&readfds) ;
    FD_SET(0, &readfds);
    result = select(1, &readfds, NULL, NULL, &tv);
    if (FD_ISSET(0, &readfds)) {
        readnum = read(0, buf, 80);
        for (i=0; i            if (buf[i] == 3 || buf[i] == 4 || buf[i] == 29) {
                printf("\r\nX X X 用户中断操作!\r\n");
                close(fd);
                exit(0);
            }
        }
    }
    alarm(1);
}
 
 
int telnet(char *host, int port)
{
    struct sockaddr_in blah;
    struct hostent *he;
 
    signal(SIGALRM, break_check);
    alarm(1);
 
    bzero((char*) &blah, sizeof(blah));
    blah.sin_family = AF_INET;
    blah.sin_port = htons(port);
 
    printf("\r\n连线中.... \r\n");
    fflush(stdout);
 
    fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if ((he = gethostbyname(host)) != NULL)
        bcopy(he->h_addr, (char*) &blah.sin_addr, he->h_length);
    else
        if ((blah.sin_addr.s_addr=inet_addr(host)) < 0) return -1;
 
    if (connect(fd, (struct sockaddr*) &blah, 16) < 0) return -1;
 
    /* 连上后就不允许定时器检查中断操作了 */
    signal(SIGALRM, SIG_IGN);
 
    printf("\033[H\033[J\033[m已连接到 %s.\r\n", host);
    printf("返回请用 \033[1;32m‘Ctrl+]‘\033[m 键.\r\n");
    fflush(stdout);
 
    while(1) {
        tv.tv_sec = 2400;
        tv.tv_usec = 0;
 
        FD_ZERO(&readfds) ;
        FD_SET(fd, &readfds);
        FD_SET(0, &readfds);
 
        result = select(fd+1, &readfds, NULL, NULL, &tv);
        if (result <= 0) break;
 
        if (FD_ISSET(0, &readfds)) {
            readnum = read(0, buf, 2048);
            if (readnum <= 0) break;
            if (buf[0] == 29) return 0;
 
            /* 回车回显问题
             *if (buf[0] == 13 && readnum == 1) {
             *  buf[1] = 10;
             *  readnum++;
             *}
             */
 
             write(fd, buf, readnum);
        } else {
            readnum = read(fd, buf, 2048);
            if (readnum <= 0) break;
            if (strchr(buf, 255))
                telnetopt(fd, buf, readnum);
            else
                write(0, buf, readnum);
        }
    }
}
 
int telnetopt(int fd, unsigned char *buf, int max) {
    unsigned char c2, c3;
    int i;
    unsigned char tmp[30];
 
    for (i = 0; i < max; i++) {
        if (buf[i] != 255) {
            write(0, &buf[i], 1);
            continue;
        }
 
        c2 = buf[++i];
        c3 = buf[++i];
 
        if (c2 == 253 && (c3 == 3 || c3 == 24)) {
            sprintf(tmp, "%c%c%c", 255, 251, c3);
            write(fd, tmp, 3);
            continue;
        }
        if ((c2 == 251 || c2 == 252) && (c3 == 1 || c3 == 3 || c3 == 24)) {
            sprintf(tmp, "%c%c%c", 255, 253, c3);
            write(fd, tmp, 3);
            continue;
        }
        if (c2 == 251 || c2 == 252) {
            sprintf(tmp, "%c%c%c", 255, 254, c3);
            write(fd, tmp, 3);
            continue;
        }
        if (c2 == 253 || c2 == 254) {
            sprintf(tmp, "%c%c%c", 255, 252, c3);
            write(fd, tmp, 3);
            continue;
        }
        if (c2 == 250) {
            while (c3 != 240 && i < max)
                c3 = buf[++i];
            sprintf(tmp, "%c%c%c%c%c%c%c%c%c%c", 255, 250, 24, 0, 65, 78, 83,
73, 255, 240);
            write(fd, tmp, 10);
        }
    }
    fflush(stdout);
}
 
int telnet_init() {
  char buf[20], c;
  c = 0;
  if (tcgetattr(0, &t1)< 0) {
      sprintf(buf, "%c%c%c%c%c%c", 255, 251, 3, 255, 251, 1);
      write(0, buf, 6);
      while (c != 1)
          if (read(0, &c, 1) <= 0) exit(0);
  } else {
      cfmakeraw(&t1);
      t1.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
      t1.c_oflag &= ~OPOST;
      t1.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
      t1.c_cflag &= ~(CSIZE|PARENB);
      t1.c_cflag |= CS8;
      tcsetattr(0, TCSANOW, &t1);
  }
}
 
main(int argc, char *argv[])
{
    int port = 23;
 
    if (argc < 2)
    {
        printf(" %s [port]\r\n", argv[0]);
        return -1;
    }
    if (argc > 2)
        port = atoi(argv[2]);
 
    telnet_init();
    telnet(argv[1], port);
    return;
}
 
【 在 liuyidaojian (山水涅磐) 的大作中提到: 】
: 最好是用底层的socket写的,
: 能否给我传一个,10.111.154.107