#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <termios.h>
#include <string.h> #define DO 0xfd
#define WONT 0xfc
#define WILL 0xfb
#define DONT 0xfe
#define CMD 0xff
#define CMD_ECHO 1
#define CMD_WINDOW_SIZE 31
#define IAC 255
#define SB 250
#define SE 240
#define BUFLEN 200
#define ESCAPE 27 #define SA struct sockaddr static struct termios tin;
static void terminal_set(void);
static void terminal_reset(void);
void negotiate(int sock, unsigned char *buf, int len);
void connect_to_server(int sock, int port, char *address); int main(int argc, char *argv[]) {
int sock;
unsigned char buf[BUFLEN + ];
int len;
int i;
int port = ; if (argc < || argc > ) {
printf("Usage: %s address [port]\n", argv[]);
return ;
} if (argc == )
port = atoi(argv[]); // Create socket
sock = socket(AF_INET, SOCK_STREAM, );
if (sock == -) {
perror("Could not create socket. Error");
return ;
}
printf("Trying %s...\n", argv[]); connect_to_server(sock, port, argv[]); printf("Connected to %s\n", argv[]);
puts("Escape character is '^]'."); // set terminal
terminal_set();
atexit(terminal_reset); // 1 second
struct timeval ts;
ts.tv_sec = ;
ts.tv_usec = ; while () {
// select setup
fd_set fds;
FD_ZERO(&fds);
if (sock != )
FD_SET(sock, &fds);
FD_SET(, &fds); // wait for data
int nready = select(sock + , &fds, (fd_set *) , (fd_set *) , &ts);
if (nready < ) {
perror("select. Error");
return ;
} else if (nready == ) {
ts.tv_sec = ;
ts.tv_usec = ;
} else if (sock != && FD_ISSET(sock, &fds)) {
// start by reading a single byte
int rv;
if ((rv = recv(sock, buf, , )) < )
return ;
else if (rv == ) {
printf("Connection closed by the remote end\n\r");
return ;
} if (buf[] == CMD) {
// read 2 more bytes
len = recv(sock, buf + , , );
if (len < )
return ;
else if (len == ) {
printf("Connection closed by the remote end\n\r");
return ;
}
negotiate(sock, buf, );
} else {
len = ;
buf[len] = '\0';
printf("%s", buf);
fflush(stdout);
}
} else if (FD_ISSET(, &fds)) {
static char crlf[] = { '\r', '\n' };
buf[] = getc(stdin); //fgets(buf, 1, stdin);
if (buf[] == '\n') { // with the terminal in raw mode we need to force a LF
if (send(sock, crlf, , ) < ) {
return ;
}
} else if (buf[] == ESCAPE) {
printf("Connection closed by the client end\n\r");
return ;
} else {
if (send(sock, buf, , ) < )
return ;
}
}
}
close(sock);
return ;
} void negotiate(int sock, unsigned char *buf, int len) {
int i;
const char* option_code[];
option_code[] = "TRANSMIT-BINARY";
option_code[] = "ECHO";
option_code[] = "SUPPRESS-GO-AHEAD";
option_code[] = "STATUS";
option_code[] = "TIMING-MARK";
option_code[] = "NAOCRD";
option_code[] = "NAOHTS";
option_code[] = "NAOHTD";
option_code[] = "NAOFFD";
option_code[] = "NAOVTS";
option_code[] = "NAOVTD";
option_code[] = "NAOLFD";
option_code[] = "EXTEND-ASCII";
option_code[] = "LOGOUT";
option_code[] = "BM";
option_code[] = "DET";
option_code[] = "SEND-LOCATION";
option_code[] = "TERMINAL-TYPE";
option_code[] = "END-OF-RECORD";
option_code[] = "TUID";
option_code[] = "OUTMRK";
option_code[] = "TTYLOC";
option_code[] = "3270-REGIME";
option_code[] = "X.3-PAD";
option_code[] = "NAWS";
option_code[] = "TERMINAL-SPEED";
option_code[] = "TOGGLE-FLOW-CONTROL";
option_code[] = "LINEMODE";
option_code[] = "X-DISPLAY-LOCATION";
option_code[] = "ENVIRON";
option_code[] = "AUTHENTICATION";
option_code[] = "ENCRYPT";
option_code[] = "NEW-ENVIRON";
option_code[] = "TN3270E";
option_code[] = "CHARSET";
option_code[] = "COM-PORT-OPTION";
option_code[] = "KERMIT";
option_code[] = "SB";
option_code[] = "SE";
option_code[] = "WILL";
option_code[] = "WONT";
option_code[] = "DO";
option_code[] = "DONT";
option_code[] = "IAC";
if (buf[] == DO && buf[] == CMD_WINDOW_SIZE) {
unsigned char tmp1[] = { IAC, WILL, CMD_WINDOW_SIZE };
if (send(sock, tmp1, , ) < )
exit(); unsigned char tmp2[] = { IAC, SB, CMD_WINDOW_SIZE, , , , , IAC,
SE };
if (send(sock, tmp2, , ) < )
exit();
return;
} for (i = ; i < len; i++) {
if (buf[i] == DO) {
buf[i] = WONT;
} else if (buf[i] == WILL) {
buf[i] = DO;
}
} if (send(sock, buf, len, ) < )
exit();
} static void terminal_set(void) {
// save terminal configuration
tcgetattr(STDIN_FILENO, &tin); static struct termios tlocal;
memcpy(&tlocal, &tin, sizeof(tin));
// The file descriptor which has to be turned to raw mode is the standard
// input of the parent process
cfmakeraw(&tlocal);
tcsetattr(STDIN_FILENO, TCSANOW, &tlocal);
} static void terminal_reset(void) {
// restore terminal upon exit
tcsetattr(STDIN_FILENO, TCSANOW, &tin);
} void connect_to_server(int sock, int port, char* address) {
struct sockaddr_in server;
server.sin_addr.s_addr = inet_addr(address);
server.sin_family = AF_INET;
server.sin_port = htons(port); //Connect to remote server
if (connect(sock, (SA *) &server, sizeof(server)) < ) {
perror("connect failed. Error\n");
exit();
}
}

telnet客户端程序的更多相关文章

  1. Mina入门级客户端程序实现telnet程序

    Mina入门级客户端程序实现telnet程序,其实mina的客户端和服务端很相似 1.编写客户端MinaClient.java和客户端处理类MyClientHandler.java2.MinaClie ...

  2. [翻译]Telnet简单介绍及在windows 7中开启Telnet客户端

    文章翻译自 http://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-clien ...

  3. Python3+telnetlib实现telnet客户端

    一.程序要点说明 python实现telnet客户端的六个关键问题及其答案是: 使用什么库实现telnet客户端----telnetlib 怎么连接主机----两种方法,一种是在实例化时传入ip地址连 ...

  4. win8操作系统下使用telnet客户端

    一.安装Telnet客户端 今天尝试在Win8操作系统下使用telnet客户端连接上搜狐的邮件服务器时,结果出现了'telnet' 不是内部或外部命令,也不是可运行的程序,如下图所示: 上网查了一下原 ...

  5. telnet客户端模拟浏览器发送请求

    telnet 客户端 telnet客户端能够发出请求去连接服务器(模拟浏览器) 使用telnet之前,需要开启telnet客户端 1.进入控制面板 2.进入程序和功能,选择打开或关闭windows功能 ...

  6. 基于 SailingEase WinForm Framework 开发客户端程序(3:实现菜单/工具栏按钮的解耦及状态控制)

    本系列文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以  SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...

  7. php编写tcp服务器和客户端程序

    这是我从别的地方看到的. 1.修改php.ini,打开extension=php_sockets.dll 2.客户端程序 SocketClient.php <?php set_time_limi ...

  8. 在公司内网上创建自己的 OSM.Planet 街道级别地图服务器及其客户端程序

    转自我的BLOG http://blog.csdn.net/goldenhawking/article/details/6402775  最近经过陛下点拨,涉猎了“OpenStreetMap”,做了不 ...

  9. 使用 Socket 通信实现 FTP 客户端程序(来自IBM)

    FTP 客户端如 FlashFXP,File Zilla 被广泛应用,原理上都是用底层的 Socket 来实现.FTP 客户端与服务器端进行数据交换必须建立两个套接字,一个作为命令通道,一个作为数据通 ...

随机推荐

  1. POJ1061 青蛙的约会(扩展欧几里得)

    题目链接:http://poj.org/problem?id=1061 青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submission ...

  2. jsp (1)

    一.jsp介绍: jsp是servlet的一种包装.是html+js+css+servlet. jsp文件无需配置,如果修改了jsp文件不需要reload应用. jsp访问方法:直接访问文件名.jsp ...

  3. java 内存调试 mat

    https://www.ibm.com/developerworks/cn/opensource/os-cn-ecl-ma/ http://www.open-open.com/lib/view/ope ...

  4. Git设置彩色输出

    彩色输出 git config --global color.status auto git config --global color.diff auto git config --global c ...

  5. A1122. Hamiltonian Cycle

    The "Hamilton cycle problem" is to find a simple cycle that contains every vertex in a gra ...

  6. jQuery ajax 传递JSON数组到Spring Controller

    jQuery ajax传递单个JSON对象到后台很容易,这里记录的是传递多个JSON对象组成的JSON数组到java 后台,并说明java如何解析JSON数组. 1.js代码 var relation ...

  7. Idea2017.3.5+SpringBoot--热部署

    首先建立工程的时候要选择DevTools: 然后File----Settings 然后 点击OK,然后Ctrl+Alt+Shift+/ 当当当当!!出来下面这个,点击Registry 找到这一项,勾选 ...

  8. POJ 1743 Musical Theme (Hash)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 33820   Accepted: 11259 D ...

  9. (贪心)nyoj91-阶乘之和

    91-阶乘之和 内存限制:64MB 时间限制:3000ms 特判: No 通过数:71 提交数:191 难度:3 题目描述: 给你一个非负数整数n,判断n是不是一些数(这些数不允许重复使用,且为正数) ...

  10. RS485 / RS422

    RS422可以变为RS485:A和Y短路(然后接T/R+),B和Z短路(然后接T/R-) RS485是半双工,只有两根线通信线,要么接收状态,要么发送状态 RE为低电平,作为接收器 DE为高电平,作为 ...