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 客户端与服务器端进行数据交换必须建立两个套接字,一个作为命令通道,一个作为数据通 ...
随机推荐
- One-hot encoding 独热编码
http://blog.sina.com.cn/s/blog_5252f6ca0102uy47.html
- shell中,2>&1详解
我们在Linux下经常会碰到nohup command>/dev/null 2>&1 &这样形式的命令.首先我们把这条命令大概分解下,首先就是一个nohup表示当前用户和系 ...
- HDU6321 Dynamic Graph Matching (杭电多校3C)
给出一些点集,然后对于每一次要求给出的这些点集里的1,2,3,4,5,6....n/2的匹配数, dp[i][j] 表示到第i次操作里点集为j的匹配数,然后我每次加入一条边u-v,我的状态就是 dp[ ...
- iptables防火墙详解(二)
-- 基于状态的iptables 如果按照tcp/ip来划分连接状态,有11种之多(课后可以自己去读一下相关知识) 但iptables里只有4种状态:ESTABLISHED.NEW.RELATED及I ...
- [学习笔记]FWT——快速沃尔什变换
解决涉及子集配凑的卷积问题 一.介绍 1.基本用法 FWT快速沃尔什变换学习笔记 就是解决一类问题: $f[k]=\sum_{i\oplus j=k}a[i]*b[j]$ 基本思想和FFT类似. 首先 ...
- 用 Homebrew 带飞你的 Mac
文章目录 资料 安装 基本用法 源镜像 Homebrew也称brew,macOS下基于命令行的最强大软件包管理工具,使用Ruby语言开发.类似于CentOS的yum或者Ubuntu的apt-get,b ...
- 对C# .Net4.5异步机制测试(二)——加深印象
public static void Main() { Console.WriteLine(Thread.CurrentThread.ManagedThreadId); In(); Console.W ...
- poj 3273"Monthly Expense"(二分搜索+最小化最大值)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...
- POJ 2253 Frogger (Floyd)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions:57696 Accepted: 18104 Descript ...
- POJ 3522 Slim Span(极差最小生成树)
Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9546 Accepted: 5076 Descrip ...