telnet客户端程序
#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客户端程序的更多相关文章
- Mina入门级客户端程序实现telnet程序
Mina入门级客户端程序实现telnet程序,其实mina的客户端和服务端很相似 1.编写客户端MinaClient.java和客户端处理类MyClientHandler.java2.MinaClie ...
- [翻译]Telnet简单介绍及在windows 7中开启Telnet客户端
文章翻译自 http://social.technet.microsoft.com/wiki/contents/articles/910.windows-7-enabling-telnet-clien ...
- Python3+telnetlib实现telnet客户端
一.程序要点说明 python实现telnet客户端的六个关键问题及其答案是: 使用什么库实现telnet客户端----telnetlib 怎么连接主机----两种方法,一种是在实例化时传入ip地址连 ...
- win8操作系统下使用telnet客户端
一.安装Telnet客户端 今天尝试在Win8操作系统下使用telnet客户端连接上搜狐的邮件服务器时,结果出现了'telnet' 不是内部或外部命令,也不是可运行的程序,如下图所示: 上网查了一下原 ...
- telnet客户端模拟浏览器发送请求
telnet 客户端 telnet客户端能够发出请求去连接服务器(模拟浏览器) 使用telnet之前,需要开启telnet客户端 1.进入控制面板 2.进入程序和功能,选择打开或关闭windows功能 ...
- 基于 SailingEase WinForm Framework 开发客户端程序(3:实现菜单/工具栏按钮的解耦及状态控制)
本系列文章将详细阐述客户端应用程序的设计理念,实现方法. 本系列文章以 SailingEase WinForm Framework 为基础进行设计并实现,但其中的设计理念及方法,亦适用于任何类型的客 ...
- php编写tcp服务器和客户端程序
这是我从别的地方看到的. 1.修改php.ini,打开extension=php_sockets.dll 2.客户端程序 SocketClient.php <?php set_time_limi ...
- 在公司内网上创建自己的 OSM.Planet 街道级别地图服务器及其客户端程序
转自我的BLOG http://blog.csdn.net/goldenhawking/article/details/6402775 最近经过陛下点拨,涉猎了“OpenStreetMap”,做了不 ...
- 使用 Socket 通信实现 FTP 客户端程序(来自IBM)
FTP 客户端如 FlashFXP,File Zilla 被广泛应用,原理上都是用底层的 Socket 来实现.FTP 客户端与服务器端进行数据交换必须建立两个套接字,一个作为命令通道,一个作为数据通 ...
随机推荐
- 【dfs】p1025 数的划分
P1025 数的划分 题目描述 将整数n分成k份,且每份不能为空,任意两个方案不相同(不考虑顺序). 例如:n=7,k=3,下面三种分法被认为是相同的. 1,1,5; 1,5,1; 5,1,1; 问有 ...
- 构建SFTP服务
---------------增加sftp-----------------查看openssh的版本# ssh -V使用ssh -V 命令来查看openssh的版本,版本必须大于4.8p1,低于的这个 ...
- luogu5010 HMR的LIS III (dp+线段树)
这个东西和最长上升子序列很像 考虑如果已经知道每个位置为开头的LIS长度和个数 f[i],我可以扫一遍 判断这个个数和K的大小,找到第一个长度=len而且个数<K的,这个位置就是要选的 然后K- ...
- docker命令篇
基础命令: 镜像: 获取镜像 $ docker pull centos:7 下拉自己仓库镜像,在后面仓库部分会讲到. 列出镜像: $ docker image ls 删除镜像: $ docker im ...
- 洛谷P4175 网络管理
题意:链上带修第k大. 这毒瘤题...别看题意只有7个字,能把我吊打死... 介绍其中两种做法好了.其实思想上是一样的. 对于每一个点,建立权值线段树,维护它到根路径上的所有权值. 一条路径上的点集就 ...
- A1118. Birds in Forest
Some scientists took pictures of thousands of birds in a forest. Assume that all the birds appear in ...
- django 通过ajax完成登录
一. 在users/views.py中加代码: from django.http import HttpResponse class LoginView(View): ""&quo ...
- VMware Workstation 14 激活码
激活码: 1.FF31K-AHZD1-H8ETZ-8WWEZ-WUUVA 2.CV7T2-6WY5Q-48EWP-ZXY7X-QGUWD
- Linux常用基本命令(less)
转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...
- sql>desc 存储包——查看包内存储过程、函数
sql>desc dbms_random: 查询dbms_random包下的存储过程和函数 a. value() 用于返回两个数值之间的随机数, value (low, high) SQL &g ...