p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Perl (http://p2p.wrox.com/forumdisplay.php?f=196)
-   -   help with perl sockets and ftp (http://p2p.wrox.com/showthread.php?t=72950)

vuorela February 25th, 2009 05:34 PM

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.

vuorela March 4th, 2009 11:15 PM

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.

ciderpunx April 21st, 2009 11:47 AM

Code:

$com_sock->autoflush();
?


All times are GMT -4. The time now is 08:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.