/* telnet.c - telnet */
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/signal.h>
#include <sys/errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "local.h"
void rcvurg(int);
#define BUFSIZE 2048 /* read buffer size */
struct termios oldtty;
int connect_TCP(const char *host, int port);
int ttysetup(void);
/*------------------------------------------------------------------------
* telnet - do the TELNET protocol to the given host and port
*------------------------------------------------------------------------
*/
int
telnet(const char *host, int port)
{
unsigned char buf[BUFSIZE];
int s, nfds; /* socket and # file descriptors */
int cc;
int on = 1;
fd_set arfds, awfds, rfds, wfds;
FILE *sfp;
s = connect_TCP(host, port);
ttysetup();
fsmbuild(); /* set up FSM's */
(void) signal(SIGURG, rcvurg);
(void) setsockopt(s, SOL_SOCKET, SO_OOBINLINE, (char *)&on,
sizeof(on));
nfds = getdtablesize();
FD_ZERO(&arfds);
FD_ZERO(&awfds);
FD_SET(s, &arfds); /* the socket */
FD_SET(0, &arfds); /* standard input */
sfp = fdopen(s, "w"); /* to get buffered output */
while (1) {
memcpy(&rfds, &arfds, sizeof(rfds));
memcpy(&wfds, &awfds, sizeof(rfds));
if (select(nfds, &rfds, &wfds, (fd_set *)0,
(struct timeval *)0) < 0) {
if (errno == EINTR)
continue; /* just a signal */
cerrexit("select: %s\n", strerror(errno));
}
if (FD_ISSET(s, &rfds)) {
cc = read(s, (char *)buf, sizeof(buf));
if (cc < 0)
cerrexit("socket read: %s\n",
strerror(errno));
else if (cc == 0) {
printf("\nconnection closed.\n");
if (tcsetattr(0, TCSADRAIN, &oldtty) < 0)
errexit("tcsetattr: %s\n",
strerror(errno));
exit(0);
} else
ttwrite(sfp, stdout, buf, cc);
}
if (FD_ISSET(0, &rfds)) {
cc = read(0, (char *)buf, sizeof(buf));
if (cc < 0)
cerrexit("tty read: %s\n",
strerror(errno));
else if (cc == 0) {
FD_CLR(0, &arfds);
(void) shutdown(s, 1);
} else
sowrite(sfp, stdout, buf, cc);
}
(void) fflush(sfp);
(void) fflush(stdout);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
telnet_client.tar.gz (49个子文件)
telnet_client
tags 7KB
tnabort.c 380B
ttysetup.c 1024B
telnet.c 2KB
connectsock.c 2KB
will_notsup.c 459B
do_notsup.c 480B
.do_echo.c.swp 4KB
subend.c 398B
fsmbuild.c 740B
telnet.h 2KB
sowrite.c 703B
subfsm.c 688B
status.c 1KB
will_termtype.c 1KB
recopt.c 631B
xput.c 651B
sofsm.c 1KB
connectTCP.c 567B
scrwrap.c 827B
soputc.c 979B
unscript.c 622B
subopt.c 599B
sonotsup.c 553B
suspend.c 935B
errexit.c 426B
sync.c 676B
do_echo.c 1KB
will_txbinary.c 726B
local.h 410B
scrgetc.c 720B
dcon.c 418B
fsminit.c 985B
tnfsm.h 1KB
passivesock.c 2KB
scrinit.c 1KB
tclient.c 705B
cscope.out 499KB
tcout.c 740B
Makefile 2KB
cerrexit.c 520B
ttputc.c 1KB
cscope.files 457B
ttfsm.c 2KB
do_noga.c 624B
connectUDP.c 560B
ttwrite.c 662B
subtermtype.c 652B
do_txbinary.c 726B
共 49 条
- 1
greatman4551
- 粉丝: 4
- 资源: 34
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页