Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2006

Tip: Looking for answers? Try searching our database.

ftp active connecting

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Humberto Alvarez - 21 Apr 2006 21:26 GMT
Hi

Does someone know how to create a socket to connect to a ftp server that
does not accept passive connections?

I need to modify my ftp class (wich works pretty good in passive mode) to
connect to the ftp in active mode.

Thanks a lot for the help

Humberto
Humberto Alvarez - 21 Apr 2006 21:36 GMT
I forgot to show the code I'm using right now. Here it is.

Dim s As Socket
Dim ep As IPEndPoint
Dim ipAddress1 As String
Dim port as Int32
ep = New IPEndPoint(Dns.Resolve(ipAddress1).AddressList( 0 ), port)
s = New Socket(ep.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
s.Connect(ep)

> Hi
>
[quoted text clipped - 7 lines]
>
> Humberto
Kevin Spencer - 22 Apr 2006 00:46 GMT
Hi Humberto,

As you're probably aware, using active mode means that rather than setting
up a Socket to read from a port that is determined by the FTP Server, and
sent to you as a reply to the "PASV" command, you decide what port to use,
and use the "PORT" command to tell the server what port to send or receive
on.

The IP address is not a problem, I'm sure, so first, of course, you need to
pick a port to use. The FTP standard defines this as an "ephemeral" port.
There is some disagreement concerning what ports are "ephemeral.," but your
best bet is to use a port between 49152 through 65535. These are defined by
IANA as the "Dynamic or Private" ports, which cannot be registered. See
http://www.iana.org/assignments/port-numbers for more information.

However, there is a problem here, as Windows by default only allows use of
ports 102 - 5000. While this can be reconfigured through the System
Registry, you probably don't want to do that unless necessary, especially if
you expect this app to run on any machine. The good news is, your port can
be picked for you.

Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);

// Let the port be selected for you
IPHostEntry localHostEntry = Dns.GetHostEntry(Dns.GetHostName());
IPEndPoint localEndPoint = new IPEndPoint(localHostEntry.AddressList[0], 0);
socket.Bind(localEndPoint);
// Queue up to 5 connections
socket.Listen(5);

Next, you need to construct the PORT command. First you need the IP address
and Port number that the Socket is bound to:

int port = ((IPEndPoint)socket.LocalEndPoint).Port;
IPAddress ipAddress = ((IPEndPoint)socket.LocalEndPoint).Address;

Next, you need to construct your PORT command and send it via your Control
Socket. The format is:

h1,h2,h3,h4,p1,p2

The first four are easy. They are the 4 octets of your IPAddress. The 2 port
numbers are 2 bytes that make up the port number. Fortunately, you can get
all of these from the IPAddress in your IPEndPoint. Just use the
IPAddress.GetAddressBytes method to do so:

byte[] addressBytes = localEndPoint.Address.GetAddressBytes();
StringBuilder command = new StringBuilder("PORT ");
for (int i = 0; i < addressBytes.Length; i++)
{
   command.Append((short)bytes[i]);
   command.Append(",");
}

Then you just send the command on the Control Socket, and wait for the 200
repsonse.

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

> Hi
>
[quoted text clipped - 7 lines]
>
> Humberto
Kevin Spencer - 22 Apr 2006 14:20 GMT
Correction on a typo:

> However, there is a problem here, as Windows by default only allows use of
> ports 102 - 5000.

That should be 1024 - 5000. I dropped the "4"!

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

> Hi Humberto,
>
[quoted text clipped - 67 lines]
>>
>> Humberto

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.