这是一个简单的telnet客户端,使用Gtk2和Perl的Net :: Telnet。
显示传入文本,但不能将键入的命令发送到服务器。服务器没有接收键入的命令,或者我们没有收到服务器的响应。
处理类型命令的代码非常简单:
$cmd = $entry->get_text(); # Gtk2::Entry
$telnetObj->put( # Net::Telnet
String => $cmd,
Telnetmode => 0,
);
然而,这个脚本完全不起作用。有什么想法吗?
#!/usr/bin/perl
# Minimal telnet client
use strict;
use diagnostics;
use warnings;
use Glib qw(TRUE FALSE);
use Gtk2 '-init';
use Net::Telnet;
# Connect to any old MUD
my $host = 'dead-souls.net';
my $port = 8000;
# The Net::Telnet object
my $telnetObj;
# Open a Gtk2 window, with a Gtk2::TextView to display incoming text, and a Gtk2::Entry for sending
# commands
my $window = Gtk2::Window->new('toplevel');
$window->set_title('Minimal telnet client');
$window->set_position('center');
$window->set_default_size(600, 400);
$window->signal_connect('delete-event' => sub {
Gtk2->main_quit();
exit;
});
my $vPaned = Gtk2::VPaned->new();
$window->add($vPaned);
$vPaned->set_position(350);
my $scrollWin = Gtk2::ScrolledWindow->new(undef, undef);
$vPaned->add1($scrollWin);
$scrollWin->set_policy('automatic', 'automatic');
$scrollWin->set_border_width(0);
my $textView = Gtk2::TextView->new;
$scrollWin->add_with_viewport($textView);
$textView->can_focus(FALSE);
$textView->set_wrap_mode('word-char');
$textView->set_justification('left');
my $buffer = $textView->get_buffer();
my $entry = Gtk2::Entry->new();
$vPaned->add2($entry);
$entry->signal_connect(activate => sub {
# When the user enters a command, empty the entry box and (if the connection is open), send the
# command to the host, and display the command in the textview
my $cmd = $entry->get_text();
$entry->set_text('');
if ($telnetObj) {
$telnetObj->put(
String => $cmd,
Telnetmode => 0,
);
my $iter = $buffer->get_end_iter();
$textView->get_buffer->insert_with_tags_by_name($iter, $cmd . "\n");
Gtk2->main_iteration() while Gtk2->events_pending();
}
});
$window->show_all();
# Set up a main loop
my $id = Glib::Timeout->add(100, sub{ &mainLoop($telnetObj, $textView, $buffer) });
if (! $id) {
exit;
}
# Open the connection
$telnetObj = Net::Telnet->new(
Timeout => 15,
);
$telnetObj->open(
Host => $host,
Port => $port,
Errmode => sub { &disconnected(); },
);
# Start Gtk2's main loop
Gtk2->main();
# ##################################################################################################
sub mainLoop {
my ($telnetObj, $textView, $buffer) = @_;
my $text = $telnetObj->get(
Errmode => sub { },
Timeout => 0,
);
if ($text) {
# We've received some text from the host. Display it in the textview
my $iter = $buffer->get_end_iter();
$buffer->insert_with_tags_by_name($iter, $text);
Gtk2->main_iteration() while Gtk2->events_pending();
}
return 1;
}
sub disconnected {
print "Disconnected\.\n";
exit;
}
sub connectError {
print "Connection error\.\n";
exit;
}
答案 0 :(得分:1)
根据上述建议,至少有三种工作方案可以替换故障:
$telnetObj->put(
String => $cmd,
Telnetmode => 0,
);
他们是:
# This works:
$telnetObj->put(
String => $cmd . "\n",
Telnetmode => 1,
);
# This also works:
$telnetObj->cmd(
String => $cmd,
Timeout => 0,
);
# This also works:
$telnetObj->print($cmd);