#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. servlet 上下文

    一.应用需求: 如何统计网站在线人数? 使用ServletContext. 二.ServletContext详解: 1.是不同于session和cookie,是可以让所有客户端共同访问的内容,是在服务 ...

  2. Spring boot中使用aop详解

      版权声明:本文为博主武伟峰原创文章,转载请注明地址http://blog.csdn.net/tianyaleixiaowu.       aop是spring的两大功能模块之一,功能非常强大,为解 ...

  3. css 选择其父元素下的某个元素

    一,选择器 :first-child   p:first-child(first第一个 child子元素)(找第一个子元素为p) :last-child    p:last-child(last倒数 ...

  4. A1105. Spiral Matrix

    This time your job is to fill a sequence of N positive integers into a spiral matrix in non-increasi ...

  5. Django(十八)Model操作补充

    参考博客:http://www.cnblogs.com/wupeiqi/articles/6216618.html 1. 创建类 class UserInfo(model.Model): age = ...

  6. Summary of Java basics review data

    1.标识符      用于命名程序的对象,如方法名,变量名,规则是: a.大小写敏感 b.由英文字符,文字字符,美元符号,下划线和数字组成,但不能以数字开头 c.不能是关键字 2.%:求余运算符    ...

  7. 斯坦福大学公开课机器学习:advice for applying machine learning | diagnosing bias vs. variance(机器学习:诊断偏差和方差问题)

    当我们运行一个学习算法时,如果这个算法的表现不理想,那么有两种原因导致:要么偏差比较大.要么方差比较大.换句话说,要么是欠拟合.要么是过拟合.那么这两种情况,哪个和偏差有关.哪个和方差有关,或者是不是 ...

  8. 第八节,配置分布式TensorFlow

    由于随着神经网络层数的增多,需要训练的参数也会增多,随之而来需要的数据集就会很大,这样会造成需要更大的运算资源,而且还要消耗很长的运算时间.TensorFlow提供了一个可以分布式部署的模式,将一个训 ...

  9. coockie 和 session

    一.Cookie Cookie的数据是由客户端来保存和携带的,所以称之为客户端技术. 1.属性: name:名称不能唯一确定一个Cookie.路径可能不同. value:不能存中文. path:默认值 ...

  10. 牛客网 2018年东北农业大学春季校赛 I题 wyh的物品

    链接:https://www.nowcoder.com/acm/contest/93/I 来源:牛客网 时间限制:C/C++ 5秒,其他语言10秒空间限制:C/C++ 262144K,其他语言5242 ...