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 客户端与服务器端进行数据交换必须建立两个套接字,一个作为命令通道,一个作为数据通 ...
随机推荐
- Codeforces1076D. Edge Deletion(最短路树+bfs)
题目链接:http://codeforces.com/contest/1076/problem/D 题目大意: 一个图N个点M条双向边.设各点到点1的距离为di,保证满足条件删除M-K条边之后使得到点 ...
- Ubuntu Server 18.04 网络设置不生效的解决
在Ubuntu18.04中,传统的配置/etc/network/interfaces已无用https://www.cnblogs.com/dunitian/p/6658578.html 新方法:修改 ...
- Linux安装Gitlab,附iSCSI分区挂载说明
因为Gitlab数据要存放在共享存储,所以本次配置的重头戏倒变成了挂载ISCSI了. OS:CentOS 7.2IP:172.16.1.191/192.168.2.191 iSCSI分Target(服 ...
- Arch Linux下韩文重叠显示
解决方法 sudo pacman -S wqy-microhei-kr-patched
- COGS 2392 2393 2395 有标号的二分图计数
有黑白关系: 枚举左部点(黑色点),然后$2^{i*(n-i)}$处理方法同:COGS 2353 2355 2356 2358 有标号的DAG计数 无关系: 发现,假设$f(i)$是一个连通块,对于一 ...
- VS Code汉化
F1搜索 Configure Language { // Defines VS Code's display language. // See https://go.microsoft.com/fw ...
- .net 调用 网易云的短信验证
static string url = "https://api.netease.im/sms/sendcode.action"; static string appKey = & ...
- django 通过ajax完成邮箱用户注册、激活账号
一.图片验证码 django-simple-captcha配置 1.在pycharm中,File====>Settings====>Project:项目名====>Project I ...
- Day18--Python--面向对象--类与类之间的关系
1. 依赖关系 在方法中引入另一个类的对象 (最不紧密的一种关系) 依赖关系都是把另一个对象当做参数 class Elephant: def __init__(self, name): self.na ...
- Proxy代理模式
https://www.cnblogs.com/vincentzh/p/5988145.html https://www.cnblogs.com/wrbxdj/p/5267370.html(不错)