Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Open Source > Perl
|
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Perl section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 25th, 2009, 05:34 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default help with perl sockets and ftp

Thank you for reading this thread.

I need to make a simple ftp client using Perl for an assignment and I am having some troubles. I have made a small program which illustrates my first problem. I believe that it is a I/O synchronization issue but am not sure.

First here is what happens when I use telnet:

Code:
telnet 192.168.18.237 21
Trying 192.168.18.237...
Connected to 192.168.18.237.
Escape character is '^]'.
220 ProFTPD 1.3.0 Server (Debian) [192.168.18.237]
user USERNAME
331 Password required for USERNAME.
pass PASSWORD
230 User USERNAME logged in.
port 192,168,18,129,195,84
200 PORT command successful
list
150 Opening ASCII mode data connection for file list
226 Transfer complete.
quit
221 Goodbye.
Connection closed by foreign host.
I have an active data server that correctly gets the output of the LIST command.

The problem that I am having is when I try to do the same communication using Perl and sockets. I have broken my code down to make look as close as possible to the telnet version.

Code:
#!/usr/bin/perl

$addr='192.168.18.237';
$port='21';

use IO::Socket;

my $com_sock = new IO::Socket::INET (
                                 PeerAddr => $addr,
                                 PeerPort => $port,
                                 Proto => 'tcp',
                                );

die "Could not create socket: $!\n" unless $com_sock;

$temp = <$com_sock>;
print $temp;

print $com_sock "user USERNAME\n";
$temp = <$com_sock>;
print $temp;

print $com_sock "pass PASSWORD\n";
$temp = <$com_sock>;
print $temp;

print $com_sock "port 192,168,18,129,195,84\n";
$temp = <$com_sock>;
print $temp;

print $com_sock "list\n";
$temp = <$com_sock>;
print $temp;
$temp = <$com_sock>;
print $temp;

print $com_sock "quit\n";
close($com_sock);
And the output:
Code:
220 ProFTPD 1.3.0 Server (Debian) [192.168.18.237]
331 Password required for USERNAME.
230 User USERNAME logged in.
after which the program hangs.

Any recommendations would be great. Thanks.
 
Old March 4th, 2009, 11:15 PM
Registered User
 
Join Date: Feb 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Smile After several hours of reading about ftp.......

A friend of mine ran into the same problem and figured it out. It turns out this had a really simple solution.

The line endings in FTP must be "\r\n" like in windows, even though I am programming on a Linux box and communicating to another Linux box.

The really annoying part is that only the PORT command seems to care what the line endings are.

Well I hope this little bit of information can help someone else in the future.

Last edited by vuorela; March 4th, 2009 at 11:18 PM..
 
Old April 21st, 2009, 11:47 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Code:
$com_sock->autoflush();
?
__________________
--
Charlie Harvey's website - linux, perl, java, anarchism and punk rock: http://charlieharvey.org.uk





Similar Threads
Thread Thread Starter Forum Replies Last Post
Sockets & Multithreading Rosicky Java Basics 1 March 15th, 2007 12:18 PM
Sockets Geo121 General .NET 1 March 30th, 2006 10:12 AM
Sockets riteshu C++ Programming 1 September 22nd, 2004 02:30 AM
Sockets natmaster Java GUI 2 April 26th, 2004 08:40 PM
Sockets idplatt BOOK: Beginning Java 2 1 August 14th, 2003 10:33 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.