By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,743 Members | 1,018 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,743 IT Pros & Developers. It's quick & easy.

FTP - can't upload a file using the MS Example

P: n/a
Jen
I'm using Microsoft's own VB.NET FTP Example:

http://support.microsoft.com/default...b;en-us;832679

I can get the program to create directories, change directories, etc.,
but I can't get it to upload a file to the FTP server. I just get a
"Cannot connect to remote server" error after this TRY:

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0),
port)

Try
s.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to remote server.")

As I said, it'll do a directory list, create, change, and delete
directories, but it will NOT transfer a file, and that's what I need
it to do. I have the code set up properly for VS 2003, per the
comments for the port calculations, and even tried it the VS 2002 way
just to be sure that wasn;t the problem.

I've also tried the Indy FTP client, but it won't even do a directory
list (says the machine "actively refused" the connection).

If someone could help I'd appreciate it.
(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/
Nov 21 '05 #1
Share this Question
Share on Google+
18 Replies


P: n/a
Hi Jen,

This apparently seems to be the connectivity issue.
Try to access the FTP using command prompt without using some FTP explorer.

If that doesnt work then the issue is with the connectivity (firewall or
something ) rather than code problem.

HTH
rawCoder

"Jen" <no***********@gte.net> wrote in message
news:n3********************************@4ax.com...
I'm using Microsoft's own VB.NET FTP Example:

http://support.microsoft.com/default...b;en-us;832679

I can get the program to create directories, change directories, etc.,
but I can't get it to upload a file to the FTP server. I just get a
"Cannot connect to remote server" error after this TRY:

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0),
port)

Try
s.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to remote server.")

As I said, it'll do a directory list, create, change, and delete
directories, but it will NOT transfer a file, and that's what I need
it to do. I have the code set up properly for VS 2003, per the
comments for the port calculations, and even tried it the VS 2002 way
just to be sure that wasn;t the problem.

I've also tried the Indy FTP client, but it won't even do a directory
list (says the machine "actively refused" the connection).

If someone could help I'd appreciate it.
(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/

Nov 21 '05 #2

P: n/a
Jen
I can use WS_FTP to upload/download files, so I don't think it's a
connectivity issue.
"rawCoder" <ra******@hotmail.com> wrote: Hi Jen,

This apparently seems to be the connectivity issue.
Try to access the FTP using command prompt without using some FTP explorer.

If that doesnt work then the issue is with the connectivity (firewall or
something ) rather than code problem.

HTH
rawCoder

(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/
Nov 21 '05 #3

P: n/a
Hi,

The files that it tries to upload dont exist on my machine. I had
to create them. Are you the files you are trying to upload exist?

Ken
-------------------
"Jen" <no***********@gte.net> wrote in message
news:n3********************************@4ax.com...
I'm using Microsoft's own VB.NET FTP Example:

http://support.microsoft.com/default...b;en-us;832679

I can get the program to create directories, change directories, etc.,
but I can't get it to upload a file to the FTP server. I just get a
"Cannot connect to remote server" error after this TRY:

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0),
port)

Try
s.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to remote server.")

As I said, it'll do a directory list, create, change, and delete
directories, but it will NOT transfer a file, and that's what I need
it to do. I have the code set up properly for VS 2003, per the
comments for the port calculations, and even tried it the VS 2002 way
just to be sure that wasn;t the problem.

I've also tried the Indy FTP client, but it won't even do a directory
list (says the machine "actively refused" the connection).

If someone could help I'd appreciate it.
(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/
Nov 21 '05 #4

P: n/a
Hi Ken,

Are you taken it over from me? Sometimes not typing all words.
The files that it tries to upload dont exist on my machine. I had
to create them. Are you the files you are trying to upload exist?

The files that it tries to upload dont exist on my machine. I had to create
them. Are you sure that the files you are trying to upload exist?

probably?

:-)

Cor
Nov 21 '05 #5

P: n/a
Jen
Yes. I created them and the directory on my machine.

"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote: Hi,

The files that it tries to upload dont exist on my machine. I had
to create them. Are you the files you are trying to upload exist?

Ken


(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/
Nov 21 '05 #6

P: n/a
Jen
I think I have it working (so far anyway). It wasn't the code at all
- it was the server I was connecting to. Unbeknownst to me, comcast
had changed the name of their FTP servers from
"mywebpages.comcast.net" to "upload.comcast.net". Evidently it's
still a work in progress, as the "mywebpages" server will still come
up for directory functions, but won't allow me tp upload.

Thanks to everyone that tried to help, I do appreciate it!


(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/
Nov 21 '05 #7

P: n/a
Jen,

Is it working? I have the same problem - in the MS app, an error comes up
when trying to create a data socket (your given code). I manage to trace it
to one of two causes: the target machine actively refusing connection, or
just taking too long to respond. For some reason, the error returned is the
more general (and vague)
"Cannot connect to remote server". Damn irritating.

But I've tried it with four different FTP sites having verified usernames
and passwords and no-go. Unlikely that all four server names are changing. So
if you have since figured it out, I'd appreciate you letting me know any
discoveries you've since made.

Cheers, Richard

PS! Is the "dim the lights" msg your own or an advertising blurb?

"Jen" wrote:
I think I have it working (so far anyway). It wasn't the code at all
- it was the server I was connecting to. Unbeknownst to me, comcast
had changed the name of their FTP servers from
"mywebpages.comcast.net" to "upload.comcast.net". Evidently it's
still a work in progress, as the "mywebpages" server will still come
up for directory functions, but won't allow me tp upload.

Thanks to everyone that tried to help, I do appreciate it!


(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/

Nov 21 '05 #8

P: n/a
Jen,

Aargh! Here goes a 2nd time (MS just dropped my first reply).
I've had the same problem. By examing the error messages in the debugging
window when it got to the code sample you displayed (the create data socket
procedure), it seems to say either that the connection was actively refused
by target machine, or that it took too long to respond.

Now I've tried this with 3 other FTP fites and the same error occurs, so
it's unlikely that the FTP servers are all renaming their servers in my case.
Is your problem fixed? If it wasn't the server being renamed, did you find
out what the problem was? I'd really appreciate your help, since this is
driving me crazy.

Cheers, Richard

PS! The Dim the Lights msg your own, or was that advertising blurb?

"Jen" wrote:
I'm using Microsoft's own VB.NET FTP Example:

http://support.microsoft.com/default...b;en-us;832679

I can get the program to create directories, change directories, etc.,
but I can't get it to upload a file to the FTP server. I just get a
"Cannot connect to remote server" error after this TRY:

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0),
port)

Try
s.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to remote server.")

As I said, it'll do a directory list, create, change, and delete
directories, but it will NOT transfer a file, and that's what I need
it to do. I have the code set up properly for VS 2003, per the
comments for the port calculations, and even tried it the VS 2002 way
just to be sure that wasn;t the problem.

I've also tried the Indy FTP client, but it won't even do a directory
list (says the machine "actively refused" the connection).

If someone could help I'd appreciate it.
(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/

Nov 21 '05 #9

P: n/a
This class is FTP.

I hope it will help you and others.

Imports System.Net

Imports System

Imports System.IO

Imports System.Text

Imports System.Net.Sockets
Public Class clsFTP

#Region "Main Class Variable Declarations"

Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String

Private m_sRemotePassword, m_sMess As String

Private m_iRemotePort, m_iBytes As Int32

Private m_objClientSocket As Socket

Private m_iRetValue As Int32

Private m_bLoggedIn As Boolean ' Change to loggedIn

Private m_sMes, m_sReply As String

Private FilePath As String
' Set the size of the packet that is used to read and

' write data to the FTP Server to the spcified size below.

Public Const BLOCK_SIZE = 512

Private m_aBuffer(BLOCK_SIZE) As Byte

Private ASCII As Encoding = Encoding.ASCII

' General variables

Private m_sMessageString As String

#End Region

#Region "Class Constructors"

'

' Main class constructor.

Public Sub New()

m_sRemoteHost = "10.10.10.10"

m_sRemotePath = "."

m_sRemoteUser = "username"

m_sRemotePassword = "password"

m_sMessageString = ""

m_iRemotePort = 21

m_bLoggedIn = False

End Sub

' Parametized constructor.

Public Sub New(ByVal sRemoteHost As String, ByVal sRemotePath As
String, ByVal sRemoteUser As String, ByVal sRemotePassword As String,
ByVal iRemotePort As Int32)

m_sRemoteHost = sRemoteHost

m_sRemotePath = sRemotePath

m_sRemoteUser = sRemoteUser

m_sRemotePassword = sRemotePassword

m_sMessageString = ""

m_iRemotePort = iRemotePort

m_bLoggedIn = False

End Sub

#End Region

#Region "Public Properties"

'

' Set/Get the name of the FTP Server.

Public Property RemoteHost() As String

Get

Return m_sRemoteHost

End Get

Set(ByVal Value As String)

m_sRemoteHost = Value

End Set

End Property

' Set/Get the FTP Port Number.

Public Property RemotePort() As Int32

Get

Return m_iRemotePort

End Get

Set(ByVal Value As Int32)

m_iRemotePort = Value

End Set

End Property

' Set/Get the remote path.

Public Property RemotePath() As String

Get

Return m_sRemotePath

End Get

Set(ByVal Value As String)

m_sRemotePath = Value

End Set

End Property

' Set the remote password.

Public Property RemotePassword() As String

Get

Return m_sRemotePassword

End Get

Set(ByVal Value As String)

m_sRemotePassword = Value

End Set

End Property

' Set/Get the remote user.

Public Property RemoteUser() As String

Get

Return m_sRemoteUser

End Get

Set(ByVal Value As String)

m_sRemoteUser = Value

End Set

End Property

' Set the class messagestring.

Public Property MessageString() As String

Get

Return m_sMessageString

End Get

Set(ByVal Value As String)

m_sMessageString = Value

End Set

End Property

#End Region

#Region "Public Subs and Functions"

'

' Return a list of files within a string() array from the

' file system.

Public Function GetFileList(ByVal sMask As String) As String()
MsgBox(sMask)

Dim cSocket As Socket

Dim bytes As Int32

Dim seperator As Char = ControlChars.Lf

Dim mess() As String

m_sMes = ""

If (Not (m_bLoggedIn)) Then

Login()

End If

cSocket = CreateDataSocket()

'SendCommand("{ls -ls}")




If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

m_sMes = ""

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)

m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)

If (bytes < m_aBuffer.Length) Then

Exit Do

End If

Loop

mess = m_sMes.Split(seperator)

cSocket.Close()

ReadReply()

If (m_iRetValue <> 226) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return mess

End Function

'

' Get the size of the file on the FTP Server.

Public Function GetFileSize(ByVal sFileName As String) As Long

Dim size As Long

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("SIZE " & sFileName)

size = 0

If (m_iRetValue = 213) Then

size = Int64.Parse(m_sReply.Substring(4))

Else

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return size

End Function

Public Function GetFileDate(ByVal sFileName As String) As DateTime
If (Not (m_bLoggedIn)) Then
Login()
End If
SendCommand("MDTM " & sFileName)
Dim filepath As String = Me.m_sReply
Dim num2 As Integer = CInt(filepath.Substring(4, 4))
Dim num3 As Integer = CInt(filepath.Substring(8, 2))
Dim num4 As Integer = CInt(filepath.Substring(10, 2))
Dim num5 As Integer = CInt(filepath.Substring(12, 2))
Dim num6 As Integer = CInt(filepath.Substring(14, 2))
returnedfiledate = DateTime.Parse(num3 & "/" & num4 & "/" & num2
& " " & num5 & ":" & num6)
Return returnedfiledate
End Function

' Log into the FTP Server.

Public Function Login() As Boolean

m_objClientSocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)

Dim ep As New
IPEndPoint(Dns.Resolve(m_sRemoteHost).AddressList( 0), m_iRemotePort)

Try

m_objClientSocket.Connect(ep)

Catch ex As Exception

MessageString = m_sReply

Throw New IOException("Couldn't connect to remote server")

End Try

ReadReply()

If (m_iRetValue <> 220) Then

CloseConnection()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

SendCommand("USER " & m_sRemoteUser)

If (Not (m_iRetValue = 331 Or m_iRetValue = 230)) Then

Cleanup()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

If (m_iRetValue <> 230) Then

SendCommand("PASS " & m_sRemotePassword)

If (Not (m_iRetValue = 230 Or m_iRetValue = 202)) Then

Cleanup()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End If

m_bLoggedIn = True

ChangeDirectory(m_sRemotePath)

' Return the end result.

Return m_bLoggedIn

End Function

'

' If the value of mode is true, set binary mode for

' downloads.

' Else, set Ascii mode.

Public Sub SetBinaryMode(ByVal bMode As Boolean)

If (bMode) Then

SendCommand("TYPE I")

Else

SendCommand("TYPE A")

End If

If (m_iRetValue <> 200) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Download a file to the Assembly's local directory,

' keeping the same file name.

Public Sub DownloadFile(ByVal sFileName As String)

DownloadFile(sFileName, "", False)

End Sub

'

' Download a remote file to the Assembly's local

' directory, keeping the same file name, and set

' the resume flag.

Public Sub DownloadFile(ByVal sFileName As String, ByVal bResume As
Boolean)

DownloadFile(sFileName, "", bResume)

End Sub

'

' Download a remote file to a local file name which can

' include a path. The local file name will be created or

' overwritten, but the path must exist.

Public Sub DownloadFile(ByVal sFileName As String, ByVal
sLocalFileName As String)

DownloadFile(sFileName, sLocalFileName, False)

End Sub

'

' Download a remote file to a local file name which can

' include a path, and set the resume flag. The local file

' name will be created or overwritten, but the path must

' exist.

Public Sub DownloadFile(ByVal sFileName As String, ByVal
sLocalFileName As String, ByVal bResume As Boolean)

Dim st As Stream

Dim output As FileStream

Dim cSocket As Socket

Dim offset, npos As Long

If (Not (m_bLoggedIn)) Then

Login()

End If

SetBinaryMode(True)

If (sLocalFileName.Equals("")) Then

sLocalFileName = sFileName
End If

If (Not (File.Exists(sLocalFileName))) Then

st = File.Create(sLocalFileName)
st.Close()

End If

output = New FileStream(sLocalFileName, FileMode.Open)

cSocket = CreateDataSocket()

offset = 0

If (bResume) Then

offset = output.Length

If (offset > 0) Then

SendCommand("REST " & offset)

If (m_iRetValue <> 350) Then

'throw new IOException(reply.Substring(4));

'Some servers may not support resuming.

offset = 0

End If

End If

If (offset > 0) Then

npos = output.Seek(offset, SeekOrigin.Begin)

End If

End If

SendCommand("RETR " & sFileName)

If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

m_iBytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)

output.Write(m_aBuffer, 0, m_iBytes)

If (m_iBytes <= 0) Then

Exit Do

End If

Loop

output.Close()

If (cSocket.Connected) Then

cSocket.Close()

End If

ReadReply()

If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Upload a file.

Public Sub UploadFile(ByVal sFileName As String)

UploadFile(sFileName, False)

End Sub

'

' Upload a file and set the resume flag.

Public Sub UploadFile(ByVal sFileName As String, ByVal bResume As
Boolean)

Dim cSocket As Socket

Dim offset As Long

Dim input As FileStream

Dim bFileNotFound As Boolean

If (Not (m_bLoggedIn)) Then

Login()

End If

cSocket = CreateDataSocket()

offset = 0

If (bResume) Then

Try

SetBinaryMode(True)

offset = GetFileSize(sFileName)

Catch ex As Exception

offset = 0

End Try

End If

If (offset > 0) Then

SendCommand("REST " & offset)

If (m_iRetValue <> 350) Then

'throw new IOException(reply.Substring(4));

'Remote server may not support resuming.

offset = 0

End If

End If

SendCommand("STOR " & Path.GetFileName(sFileName))

If (Not (m_iRetValue = 125 Or m_iRetValue = 150)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

' Check to see if the file exists before the upload.

bFileNotFound = False

If (File.Exists(sFileName)) Then

' Open input stream to read source file

input = New FileStream(sFileName, FileMode.Open)

If (offset <> 0) Then

input.Seek(offset, SeekOrigin.Begin)

End If

' Upload the file

m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Do While (m_iBytes > 0)

cSocket.Send(m_aBuffer, m_iBytes, 0)

m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Loop

input.Close()

Else

bFileNotFound = True

End If

If (cSocket.Connected) Then

cSocket.Close()

End If

' No point in reading the return value if the file was

' not found.

If (bFileNotFound) Then

MessageString = m_sReply

Throw New IOException("The file: " & sFileName & " was not
found. Can not upload the file to the FTP Site.")

End If

ReadReply()

If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Delete a file from the remote FTP server.

Public Function DeleteFile(ByVal sFileName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("DELE " & sFileName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Rename a file on the remote FTP server.

Public Function RenameFile(ByVal sOldFileName As String, ByVal
sNewFileName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("RNFR " & sOldFileName)

If (m_iRetValue <> 350) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

' known problem

' rnto will not take care of existing file.

' i.e. It will overwrite if newFileName exist

SendCommand("RNTO " & sNewFileName)

If (m_iRetValue <> 250) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return bResult

End Function

'

' Create a directory on the remote FTP server.

Public Function CreateDirectory(ByVal sDirName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("MKD " & sDirName)

If (m_iRetValue <> 257) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Delete a directory on the remote FTP server.

Public Function RemoveDirectory(ByVal sDirName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("RMD " & sDirName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Change the current working directory on the remote FTP

' server.

Public Function ChangeDirectory(ByVal sDirName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (sDirName.Equals(".")) Then

Exit Function

End If

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("CWD " & sDirName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

Me.m_sRemotePath = sDirName

' Return the final result.

Return bResult

End Function

'

' Close the FTP connection.

Public Sub CloseConnection()

If (Not (m_objClientSocket Is Nothing)) Then

SendCommand("QUIT")

End If

Cleanup()

End Sub

#End Region

#Region "Private Subs and Functions"

'

' Read the reply from the FTP Server

Private Sub ReadReply()

m_sMes = ""

m_sReply = ReadLine()
FilePath = m_sReply
m_iRetValue = Int32.Parse(m_sReply.Substring(0, 3))

End Sub

'

' Clean up some variables.

Private Sub Cleanup()

If Not (m_objClientSocket Is Nothing) Then

m_objClientSocket.Close()

m_objClientSocket = Nothing

End If

m_bLoggedIn = False

End Sub

'

' Read a line from the server.

Private Function ReadLine(Optional ByVal bClearMes As Boolean =
False) As String

Dim seperator As Char = ControlChars.Lf

Dim mess() As String

If (bClearMes) Then

m_sMes = ""

End If

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, BLOCK_SIZE)

m_iBytes = m_objClientSocket.Receive(m_aBuffer,
m_aBuffer.Length, 0)

m_sMes += ASCII.GetString(m_aBuffer, 0, m_iBytes)

If (m_iBytes < m_aBuffer.Length) Then

Exit Do

End If

Loop

mess = m_sMes.Split(seperator)

If (m_sMes.Length > 2) Then

m_sMes = mess(mess.Length - 2)

Else

m_sMes = mess(0)

End If

If (Not (m_sMes.Substring(3, 1).Equals(" "))) Then

Return ReadLine(True)

End If

Return m_sMes

End Function

'

' Send a command to the FTP Server.

Private Sub SendCommand(ByVal sCommand As String)

sCommand = sCommand & ControlChars.CrLf

Dim cmdbytes As Byte() = ASCII.GetBytes(sCommand)

m_objClientSocket.Send(cmdbytes, cmdbytes.Length, 0)

ReadReply()

End Sub

'

' Create a Data socket.

Private Function CreateDataSocket() As Socket

Dim index1, index2, len As Int32

Dim partCount, i, port As Int32

Dim ipData, buf, ipAddress As String

Dim parts(6) As Int32

Dim ch As Char

Dim s As Socket

Dim ep As IPEndPoint

SendCommand("PASV")

If (m_iRetValue <> 227) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

index1 = m_sReply.IndexOf("(")

index2 = m_sReply.IndexOf(")")

ipData = m_sReply.Substring(index1 + 1, index2 - index1 - 1)

len = ipData.Length

partCount = 0

buf = ""

For i = 0 To ((len - 1) And partCount <= 6)

ch = Char.Parse(ipData.Substring(i, 1))

If (Char.IsDigit(ch)) Then

buf += ch

ElseIf (ch <> ",") Then

MessageString = m_sReply

Throw New IOException("Malformed PASV reply: " &
m_sReply)

End If

If ((ch = ",") Or (i + 1 = len)) Then

Try

parts(partCount) = Int32.Parse(buf)

partCount += 1

buf = ""

Catch ex As Exception

MessageString = m_sReply

Throw New IOException("Malformed PASV reply: " &
m_sReply)

End Try

End If

Next

ipAddress = parts(0) & "." & parts(1) & "." & parts(2) & "." &
parts(3)

' Make this call in VB.NET 2002. We would like to

' bitshift the number by 8 bits, so in VB.NET 2002 we

' multiply the number by 2 to the power of 8.

'port = parts(4) * (2 ^ 8)

' Make this call and comment out the above line for

' VB.NET 2003.

port = parts(4) << 8

' Determine the data port number.

port = port + parts(5)

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), port)

Try
s.Connect(ep)

Catch ex As Exception

MessageString = m_sReply
MsgBox(ex.ToString)

Throw New IOException("Can't connect to remote server")

End Try

Return s

End Function

#End Region

End Class
"RichardC" <Ri******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com:
Jen,

Is it working? I have the same problem - in the MS app, an error comes up

when trying to create a data socket (your given code). I manage to trace
it
to one of two causes: the target machine actively refusing connection, or

just taking too long to respond. For some reason, the error returned is
the
more general (and vague)
"Cannot connect to remote server". Damn irritating.

But I've tried it with four different FTP sites having verified usernames

and passwords and no-go. Unlikely that all four server names are changing.
So
if you have since figured it out, I'd appreciate you letting me know any

discoveries you've since made.

Cheers, Richard

PS! Is the "dim the lights" msg your own or an advertising blurb?

"Jen" wrote:
I think I have it working (so far anyway). It wasn't the code at all
- it was the server I was connecting to. Unbeknownst to me, comcast
had changed the name of their FTP servers from
"mywebpages.comcast.net" to "upload.comcast.net". Evidently it's
still a work in progress, as the "mywebpages" server will still come
up for directory functions, but won't allow me tp upload.

Thanks to everyone that tried to help, I do appreciate it!


(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/


Nov 21 '05 #10

P: n/a
Scorpion,

Thanks for trying but what you submitted looks EXACTLY like the sample Jen
submitted - and is the same as mine, taken from:
http://www.devcity.net/forums/topic.asp?tid=72390
which is based on the MS code provided on the MS Support site.

The problem seems to occur in the Create Data Socket procedure where it says
(the 2nd time):
ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), port)
s.Connect(ep)

There is an error in the creation of the Endpoint, which probably explains
why s.Connect doesn't work. If you have any ideas, I would appreciate them.

Thanks, Richard
"scorpion53061" wrote:
This class is FTP.

I hope it will help you and others.

Imports System.Net

Imports System

Imports System.IO

Imports System.Text

Imports System.Net.Sockets
Public Class clsFTP

#Region "Main Class Variable Declarations"

Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String

Private m_sRemotePassword, m_sMess As String

Private m_iRemotePort, m_iBytes As Int32

Private m_objClientSocket As Socket

Private m_iRetValue As Int32

Private m_bLoggedIn As Boolean ' Change to loggedIn

Private m_sMes, m_sReply As String

Private FilePath As String
' Set the size of the packet that is used to read and

' write data to the FTP Server to the spcified size below.

Public Const BLOCK_SIZE = 512

Private m_aBuffer(BLOCK_SIZE) As Byte

Private ASCII As Encoding = Encoding.ASCII

' General variables

Private m_sMessageString As String

#End Region

#Region "Class Constructors"

'

' Main class constructor.

Public Sub New()

m_sRemoteHost = "10.10.10.10"

m_sRemotePath = "."

m_sRemoteUser = "username"

m_sRemotePassword = "password"

m_sMessageString = ""

m_iRemotePort = 21

m_bLoggedIn = False

End Sub

' Parametized constructor.

Public Sub New(ByVal sRemoteHost As String, ByVal sRemotePath As
String, ByVal sRemoteUser As String, ByVal sRemotePassword As String,
ByVal iRemotePort As Int32)

m_sRemoteHost = sRemoteHost

m_sRemotePath = sRemotePath

m_sRemoteUser = sRemoteUser

m_sRemotePassword = sRemotePassword

m_sMessageString = ""

m_iRemotePort = iRemotePort

m_bLoggedIn = False

End Sub

#End Region

#Region "Public Properties"

'

' Set/Get the name of the FTP Server.

Public Property RemoteHost() As String

Get

Return m_sRemoteHost

End Get

Set(ByVal Value As String)

m_sRemoteHost = Value

End Set

End Property

' Set/Get the FTP Port Number.

Public Property RemotePort() As Int32

Get

Return m_iRemotePort

End Get

Set(ByVal Value As Int32)

m_iRemotePort = Value

End Set

End Property

' Set/Get the remote path.

Public Property RemotePath() As String

Get

Return m_sRemotePath

End Get

Set(ByVal Value As String)

m_sRemotePath = Value

End Set

End Property

' Set the remote password.

Public Property RemotePassword() As String

Get

Return m_sRemotePassword

End Get

Set(ByVal Value As String)

m_sRemotePassword = Value

End Set

End Property

' Set/Get the remote user.

Public Property RemoteUser() As String

Get

Return m_sRemoteUser

End Get

Set(ByVal Value As String)

m_sRemoteUser = Value

End Set

End Property

' Set the class messagestring.

Public Property MessageString() As String

Get

Return m_sMessageString

End Get

Set(ByVal Value As String)

m_sMessageString = Value

End Set

End Property

#End Region

#Region "Public Subs and Functions"

'

' Return a list of files within a string() array from the

' file system.

Public Function GetFileList(ByVal sMask As String) As String()
MsgBox(sMask)

Dim cSocket As Socket

Dim bytes As Int32

Dim seperator As Char = ControlChars.Lf

Dim mess() As String

m_sMes = ""

If (Not (m_bLoggedIn)) Then

Login()

End If

cSocket = CreateDataSocket()

'SendCommand("{ls -ls}")




If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

m_sMes = ""

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)

m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)

If (bytes < m_aBuffer.Length) Then

Exit Do

End If

Loop

mess = m_sMes.Split(seperator)

cSocket.Close()

ReadReply()

If (m_iRetValue <> 226) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return mess

End Function

'

' Get the size of the file on the FTP Server.

Public Function GetFileSize(ByVal sFileName As String) As Long

Dim size As Long

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("SIZE " & sFileName)

size = 0

If (m_iRetValue = 213) Then

size = Int64.Parse(m_sReply.Substring(4))

Else

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return size

End Function

Public Function GetFileDate(ByVal sFileName As String) As DateTime
If (Not (m_bLoggedIn)) Then
Login()
End If
SendCommand("MDTM " & sFileName)
Dim filepath As String = Me.m_sReply
Dim num2 As Integer = CInt(filepath.Substring(4, 4))
Dim num3 As Integer = CInt(filepath.Substring(8, 2))
Dim num4 As Integer = CInt(filepath.Substring(10, 2))
Dim num5 As Integer = CInt(filepath.Substring(12, 2))
Dim num6 As Integer = CInt(filepath.Substring(14, 2))
returnedfiledate = DateTime.Parse(num3 & "/" & num4 & "/" & num2
& " " & num5 & ":" & num6)
Return returnedfiledate
End Function

' Log into the FTP Server.

Public Function Login() As Boolean

m_objClientSocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)

Dim ep As New
IPEndPoint(Dns.Resolve(m_sRemoteHost).AddressList( 0), m_iRemotePort)

Try

m_objClientSocket.Connect(ep)

Catch ex As Exception

MessageString = m_sReply

Throw New IOException("Couldn't connect to remote server")

End Try

ReadReply()

If (m_iRetValue <> 220) Then

CloseConnection()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

SendCommand("USER " & m_sRemoteUser)

If (Not (m_iRetValue = 331 Or m_iRetValue = 230)) Then

Cleanup()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

If (m_iRetValue <> 230) Then

SendCommand("PASS " & m_sRemotePassword)

If (Not (m_iRetValue = 230 Or m_iRetValue = 202)) Then

Cleanup()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End If

m_bLoggedIn = True

ChangeDirectory(m_sRemotePath)

' Return the end result.

Return m_bLoggedIn

End Function

'

' If the value of mode is true, set binary mode for

' downloads.

' Else, set Ascii mode.

Public Sub SetBinaryMode(ByVal bMode As Boolean)

If (bMode) Then

SendCommand("TYPE I")

Else

SendCommand("TYPE A")

End If

If (m_iRetValue <> 200) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Download a file to the Assembly's local directory,

' keeping the same file name.

Public Sub DownloadFile(ByVal sFileName As String)

DownloadFile(sFileName, "", False)

End Sub

'

' Download a remote file to the Assembly's local

' directory, keeping the same file name, and set

' the resume flag.

Public Sub DownloadFile(ByVal sFileName As String, ByVal bResume As
Boolean)

DownloadFile(sFileName, "", bResume)

End Sub

'

' Download a remote file to a local file name which can

' include a path. The local file name will be created or

' overwritten, but the path must exist.

Public Sub DownloadFile(ByVal sFileName As String, ByVal
sLocalFileName As String)

DownloadFile(sFileName, sLocalFileName, False)

End Sub

'

' Download a remote file to a local file name which can

' include a path, and set the resume flag. The local file

' name will be created or overwritten, but the path must

' exist.

Public Sub DownloadFile(ByVal sFileName As String, ByVal
sLocalFileName As String, ByVal bResume As Boolean)

Dim st As Stream

Dim output As FileStream

Dim cSocket As Socket

Dim offset, npos As Long

If (Not (m_bLoggedIn)) Then

Login()

End If

SetBinaryMode(True)

If (sLocalFileName.Equals("")) Then

sLocalFileName = sFileName
End If

If (Not (File.Exists(sLocalFileName))) Then

st = File.Create(sLocalFileName)
st.Close()

End If

output = New FileStream(sLocalFileName, FileMode.Open)

cSocket = CreateDataSocket()

offset = 0

If (bResume) Then

offset = output.Length

If (offset > 0) Then

SendCommand("REST " & offset)

If (m_iRetValue <> 350) Then

'throw new IOException(reply.Substring(4));

'Some servers may not support resuming.

offset = 0

End If

End If

If (offset > 0) Then

npos = output.Seek(offset, SeekOrigin.Begin)

End If

End If

SendCommand("RETR " & sFileName)

If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

m_iBytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)

output.Write(m_aBuffer, 0, m_iBytes)

If (m_iBytes <= 0) Then

Exit Do

End If

Loop

output.Close()

If (cSocket.Connected) Then

cSocket.Close()

End If

ReadReply()

If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Upload a file.

Public Sub UploadFile(ByVal sFileName As String)

UploadFile(sFileName, False)

End Sub

'

' Upload a file and set the resume flag.

Public Sub UploadFile(ByVal sFileName As String, ByVal bResume As
Boolean)

Dim cSocket As Socket

Dim offset As Long

Dim input As FileStream

Dim bFileNotFound As Boolean

If (Not (m_bLoggedIn)) Then

Login()

End If

cSocket = CreateDataSocket()

offset = 0

If (bResume) Then

Try

SetBinaryMode(True)

offset = GetFileSize(sFileName)

Catch ex As Exception

offset = 0

End Try

End If

If (offset > 0) Then

SendCommand("REST " & offset)

If (m_iRetValue <> 350) Then

'throw new IOException(reply.Substring(4));

'Remote server may not support resuming.

offset = 0

End If

End If

SendCommand("STOR " & Path.GetFileName(sFileName))

If (Not (m_iRetValue = 125 Or m_iRetValue = 150)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

' Check to see if the file exists before the upload.

bFileNotFound = False

If (File.Exists(sFileName)) Then

' Open input stream to read source file

input = New FileStream(sFileName, FileMode.Open)

If (offset <> 0) Then

input.Seek(offset, SeekOrigin.Begin)

End If

' Upload the file

m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Do While (m_iBytes > 0)

cSocket.Send(m_aBuffer, m_iBytes, 0)

m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Loop

input.Close()

Else

bFileNotFound = True

End If

If (cSocket.Connected) Then

cSocket.Close()

End If

' No point in reading the return value if the file was

' not found.

If (bFileNotFound) Then

MessageString = m_sReply

Throw New IOException("The file: " & sFileName & " was not
found. Can not upload the file to the FTP Site.")

End If

ReadReply()

If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Delete a file from the remote FTP server.

Public Function DeleteFile(ByVal sFileName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("DELE " & sFileName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Rename a file on the remote FTP server.

Public Function RenameFile(ByVal sOldFileName As String, ByVal
sNewFileName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("RNFR " & sOldFileName)

If (m_iRetValue <> 350) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

' known problem

' rnto will not take care of existing file.

' i.e. It will overwrite if newFileName exist

SendCommand("RNTO " & sNewFileName)

If (m_iRetValue <> 250) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return bResult

End Function

'

' Create a directory on the remote FTP server.

Public Function CreateDirectory(ByVal sDirName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("MKD " & sDirName)

If (m_iRetValue <> 257) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Delete a directory on the remote FTP server.

Public Function RemoveDirectory(ByVal sDirName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("RMD " & sDirName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Change the current working directory on the remote FTP

' server.

Public Function ChangeDirectory(ByVal sDirName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (sDirName.Equals(".")) Then

Exit Function

End If

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("CWD " & sDirName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

Me.m_sRemotePath = sDirName

' Return the final result.

Return bResult

End Function

'

' Close the FTP connection.

Public Sub CloseConnection()

If (Not (m_objClientSocket Is Nothing)) Then

SendCommand("QUIT")

End If

Cleanup()

End Sub

#End Region

#Region "Private Subs and Functions"

'

' Read the reply from the FTP Server

Private Sub ReadReply()

m_sMes = ""

m_sReply = ReadLine()
FilePath = m_sReply
m_iRetValue = Int32.Parse(m_sReply.Substring(0, 3))

End Sub

'

' Clean up some variables.

Private Sub Cleanup()

If Not (m_objClientSocket Is Nothing) Then

m_objClientSocket.Close()

m_objClientSocket = Nothing

End If

m_bLoggedIn = False

End Sub

'

' Read a line from the server.

Private Function ReadLine(Optional ByVal bClearMes As Boolean =
False) As String

Dim seperator As Char = ControlChars.Lf

Dim mess() As String

If (bClearMes) Then

m_sMes = ""

End If

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, BLOCK_SIZE)

m_iBytes = m_objClientSocket.Receive(m_aBuffer,
m_aBuffer.Length, 0)

m_sMes += ASCII.GetString(m_aBuffer, 0, m_iBytes)

If (m_iBytes < m_aBuffer.Length) Then

Exit Do

End If

Loop

mess = m_sMes.Split(seperator)

If (m_sMes.Length > 2) Then

m_sMes = mess(mess.Length - 2)

Else

m_sMes = mess(0)

End If

If (Not (m_sMes.Substring(3, 1).Equals(" "))) Then

Return ReadLine(True)

End If

Return m_sMes

End Function

'

' Send a command to the FTP Server.

Private Sub SendCommand(ByVal sCommand As String)

sCommand = sCommand & ControlChars.CrLf

Dim cmdbytes As Byte() = ASCII.GetBytes(sCommand)

m_objClientSocket.Send(cmdbytes, cmdbytes.Length, 0)

ReadReply()

End Sub

'

' Create a Data socket.

Private Function CreateDataSocket() As Socket

Dim index1, index2, len As Int32

Dim partCount, i, port As Int32

Dim ipData, buf, ipAddress As String

Dim parts(6) As Int32

Dim ch As Char

Dim s As Socket

Dim ep As IPEndPoint

SendCommand("PASV")

If (m_iRetValue <> 227) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

index1 = m_sReply.IndexOf("(")

index2 = m_sReply.IndexOf(")")

ipData = m_sReply.Substring(index1 + 1, index2 - index1 - 1)

len = ipData.Length

partCount = 0

buf = ""

For i = 0 To ((len - 1) And partCount <= 6)

ch = Char.Parse(ipData.Substring(i, 1))

If (Char.IsDigit(ch)) Then

buf += ch

ElseIf (ch <> ",") Then

MessageString = m_sReply

Throw New IOException("Malformed PASV reply: " &
m_sReply)

End If

If ((ch = ",") Or (i + 1 = len)) Then

Try

parts(partCount) = Int32.Parse(buf)

partCount += 1

buf = ""

Catch ex As Exception

MessageString = m_sReply

Throw New IOException("Malformed PASV reply: " &
m_sReply)

End Try

End If

Next

ipAddress = parts(0) & "." & parts(1) & "." & parts(2) & "." &
parts(3)

' Make this call in VB.NET 2002. We would like to

' bitshift the number by 8 bits, so in VB.NET 2002 we

' multiply the number by 2 to the power of 8.

'port = parts(4) * (2 ^ 8)

' Make this call and comment out the above line for

' VB.NET 2003.

port = parts(4) << 8

' Determine the data port number.

port = port + parts(5)

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), port)

Try
s.Connect(ep)

Catch ex As Exception

MessageString = m_sReply
MsgBox(ex.ToString)

Throw New IOException("Can't connect to remote server")

End Try

Return s

End Function

#End Region

End Class
"RichardC" <Ri******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com:
Jen,

Is it working? I have the same problem - in the MS app, an error comes up

when trying to create a data socket (your given code). I manage to trace
it
to one of two causes: the target machine actively refusing connection, or

just taking too long to respond. For some reason, the error returned is
the
more general (and vague)
"Cannot connect to remote server". Damn irritating.

But I've tried it with four different FTP sites having verified usernames

and passwords and no-go. Unlikely that all four server names are changing.
So
if you have since figured it out, I'd appreciate you letting me know any

discoveries you've since made.

Cheers, Richard

PS! Is the "dim the lights" msg your own or an advertising blurb?

"Jen" wrote:
I think I have it working (so far anyway). It wasn't the code at all
- it was the server I was connecting to. Unbeknownst to me, comcast
had changed the name of their FTP servers from
"mywebpages.comcast.net" to "upload.comcast.net". Evidently it's
still a work in progress, as the "mywebpages" server will still come
up for directory functions, but won't allow me tp upload.

Thanks to everyone that tried to help, I do appreciate it!


(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/


Nov 21 '05 #11

P: n/a
Richard,

I use this code every day in several apps and it works fine.

Can you provide a sample address or something I can test against that is
giving you problems?

"RichardC" <Ri******@discussions.microsoft.com> wrote in message
news:C2**********************************@microsof t.com:
Scorpion,

Thanks for trying but what you submitted looks EXACTLY like the sample Jen

submitted - and is the same as mine, taken from:
http://www.devcity.net/forums/topic.asp?tid=72390
which is based on the MS code provided on the MS Support site.

The problem seems to occur in the Create Data Socket procedure where it
says
(the 2nd time):
ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0), port)
s.Connect(ep)

There is an error in the creation of the Endpoint, which probably explains

why s.Connect doesn't work. If you have any ideas, I would appreciate
them.

Thanks, Richard
"scorpion53061" wrote:
This class is FTP.

I hope it will help you and others.

Imports System.Net

Imports System

Imports System.IO

Imports System.Text

Imports System.Net.Sockets
Public Class clsFTP

#Region "Main Class Variable Declarations"

Private m_sRemoteHost, m_sRemotePath, m_sRemoteUser As String

Private m_sRemotePassword, m_sMess As String

Private m_iRemotePort, m_iBytes As Int32

Private m_objClientSocket As Socket

Private m_iRetValue As Int32

Private m_bLoggedIn As Boolean ' Change to loggedIn

Private m_sMes, m_sReply As String

Private FilePath As String
' Set the size of the packet that is used to read and

' write data to the FTP Server to the spcified size below.

Public Const BLOCK_SIZE = 512

Private m_aBuffer(BLOCK_SIZE) As Byte

Private ASCII As Encoding = Encoding.ASCII

' General variables

Private m_sMessageString As String

#End Region

#Region "Class Constructors"

'

' Main class constructor.

Public Sub New()

m_sRemoteHost = "10.10.10.10"

m_sRemotePath = "."

m_sRemoteUser = "username"

m_sRemotePassword = "password"

m_sMessageString = ""

m_iRemotePort = 21

m_bLoggedIn = False

End Sub

' Parametized constructor.

Public Sub New(ByVal sRemoteHost As String, ByVal sRemotePath As
String, ByVal sRemoteUser As String, ByVal sRemotePassword As String,
ByVal iRemotePort As Int32)

m_sRemoteHost = sRemoteHost

m_sRemotePath = sRemotePath

m_sRemoteUser = sRemoteUser

m_sRemotePassword = sRemotePassword

m_sMessageString = ""

m_iRemotePort = iRemotePort

m_bLoggedIn = False

End Sub

#End Region

#Region "Public Properties"

'

' Set/Get the name of the FTP Server.

Public Property RemoteHost() As String

Get

Return m_sRemoteHost

End Get

Set(ByVal Value As String)

m_sRemoteHost = Value

End Set

End Property

' Set/Get the FTP Port Number.

Public Property RemotePort() As Int32

Get

Return m_iRemotePort

End Get

Set(ByVal Value As Int32)

m_iRemotePort = Value

End Set

End Property

' Set/Get the remote path.

Public Property RemotePath() As String

Get

Return m_sRemotePath

End Get

Set(ByVal Value As String)

m_sRemotePath = Value

End Set

End Property

' Set the remote password.

Public Property RemotePassword() As String

Get

Return m_sRemotePassword

End Get

Set(ByVal Value As String)

m_sRemotePassword = Value

End Set

End Property

' Set/Get the remote user.

Public Property RemoteUser() As String

Get

Return m_sRemoteUser

End Get

Set(ByVal Value As String)

m_sRemoteUser = Value

End Set

End Property

' Set the class messagestring.

Public Property MessageString() As String

Get

Return m_sMessageString

End Get

Set(ByVal Value As String)

m_sMessageString = Value

End Set

End Property

#End Region

#Region "Public Subs and Functions"

'

' Return a list of files within a string() array from the

' file system.

Public Function GetFileList(ByVal sMask As String) As String()
MsgBox(sMask)

Dim cSocket As Socket

Dim bytes As Int32

Dim seperator As Char = ControlChars.Lf

Dim mess() As String

m_sMes = ""

If (Not (m_bLoggedIn)) Then

Login()

End If

cSocket = CreateDataSocket()

'SendCommand("{ls -ls}")




If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

m_sMes = ""

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

bytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)

m_sMes += ASCII.GetString(m_aBuffer, 0, bytes)

If (bytes < m_aBuffer.Length) Then

Exit Do

End If

Loop

mess = m_sMes.Split(seperator)

cSocket.Close()

ReadReply()

If (m_iRetValue <> 226) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return mess

End Function

'

' Get the size of the file on the FTP Server.

Public Function GetFileSize(ByVal sFileName As String) As Long

Dim size As Long

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("SIZE " & sFileName)

size = 0

If (m_iRetValue = 213) Then

size = Int64.Parse(m_sReply.Substring(4))

Else

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return size

End Function

Public Function GetFileDate(ByVal sFileName As String) As DateTime
If (Not (m_bLoggedIn)) Then
Login()
End If
SendCommand("MDTM " & sFileName)
Dim filepath As String = Me.m_sReply
Dim num2 As Integer = CInt(filepath.Substring(4, 4))
Dim num3 As Integer = CInt(filepath.Substring(8, 2))
Dim num4 As Integer = CInt(filepath.Substring(10, 2))
Dim num5 As Integer = CInt(filepath.Substring(12, 2))
Dim num6 As Integer = CInt(filepath.Substring(14, 2))
returnedfiledate = DateTime.Parse(num3 & "/" & num4 & "/" & num2

& " " & num5 & ":" & num6)
Return returnedfiledate
End Function

' Log into the FTP Server.

Public Function Login() As Boolean

m_objClientSocket = New Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp)

Dim ep As New
IPEndPoint(Dns.Resolve(m_sRemoteHost).AddressList( 0), m_iRemotePort)

Try

m_objClientSocket.Connect(ep)

Catch ex As Exception

MessageString = m_sReply

Throw New IOException("Couldn't connect to remote server")

End Try

ReadReply()

If (m_iRetValue <> 220) Then

CloseConnection()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

SendCommand("USER " & m_sRemoteUser)

If (Not (m_iRetValue = 331 Or m_iRetValue = 230)) Then

Cleanup()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

If (m_iRetValue <> 230) Then

SendCommand("PASS " & m_sRemotePassword)

If (Not (m_iRetValue = 230 Or m_iRetValue = 202)) Then

Cleanup()

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End If

m_bLoggedIn = True

ChangeDirectory(m_sRemotePath)

' Return the end result.

Return m_bLoggedIn

End Function

'

' If the value of mode is true, set binary mode for

' downloads.

' Else, set Ascii mode.

Public Sub SetBinaryMode(ByVal bMode As Boolean)

If (bMode) Then

SendCommand("TYPE I")

Else

SendCommand("TYPE A")

End If

If (m_iRetValue <> 200) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Download a file to the Assembly's local directory,

' keeping the same file name.

Public Sub DownloadFile(ByVal sFileName As String)

DownloadFile(sFileName, "", False)

End Sub

'

' Download a remote file to the Assembly's local

' directory, keeping the same file name, and set

' the resume flag.

Public Sub DownloadFile(ByVal sFileName As String, ByVal bResume As

Boolean)

DownloadFile(sFileName, "", bResume)

End Sub

'

' Download a remote file to a local file name which can

' include a path. The local file name will be created or

' overwritten, but the path must exist.

Public Sub DownloadFile(ByVal sFileName As String, ByVal
sLocalFileName As String)

DownloadFile(sFileName, sLocalFileName, False)

End Sub

'

' Download a remote file to a local file name which can

' include a path, and set the resume flag. The local file

' name will be created or overwritten, but the path must

' exist.

Public Sub DownloadFile(ByVal sFileName As String, ByVal
sLocalFileName As String, ByVal bResume As Boolean)

Dim st As Stream

Dim output As FileStream

Dim cSocket As Socket

Dim offset, npos As Long

If (Not (m_bLoggedIn)) Then

Login()

End If

SetBinaryMode(True)

If (sLocalFileName.Equals("")) Then

sLocalFileName = sFileName
End If

If (Not (File.Exists(sLocalFileName))) Then

st = File.Create(sLocalFileName)
st.Close()

End If

output = New FileStream(sLocalFileName, FileMode.Open)

cSocket = CreateDataSocket()

offset = 0

If (bResume) Then

offset = output.Length

If (offset > 0) Then

SendCommand("REST " & offset)

If (m_iRetValue <> 350) Then

'throw new IOException(reply.Substring(4));

'Some servers may not support resuming.

offset = 0

End If

End If

If (offset > 0) Then

npos = output.Seek(offset, SeekOrigin.Begin)

End If

End If

SendCommand("RETR " & sFileName)

If (Not (m_iRetValue = 150 Or m_iRetValue = 125)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, m_aBuffer.Length)

m_iBytes = cSocket.Receive(m_aBuffer, m_aBuffer.Length, 0)

output.Write(m_aBuffer, 0, m_iBytes)

If (m_iBytes <= 0) Then

Exit Do

End If

Loop

output.Close()

If (cSocket.Connected) Then

cSocket.Close()

End If

ReadReply()

If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Upload a file.

Public Sub UploadFile(ByVal sFileName As String)

UploadFile(sFileName, False)

End Sub

'

' Upload a file and set the resume flag.

Public Sub UploadFile(ByVal sFileName As String, ByVal bResume As
Boolean)

Dim cSocket As Socket

Dim offset As Long

Dim input As FileStream

Dim bFileNotFound As Boolean

If (Not (m_bLoggedIn)) Then

Login()

End If

cSocket = CreateDataSocket()

offset = 0

If (bResume) Then

Try

SetBinaryMode(True)

offset = GetFileSize(sFileName)

Catch ex As Exception

offset = 0

End Try

End If

If (offset > 0) Then

SendCommand("REST " & offset)

If (m_iRetValue <> 350) Then

'throw new IOException(reply.Substring(4));

'Remote server may not support resuming.

offset = 0

End If

End If

SendCommand("STOR " & Path.GetFileName(sFileName))

If (Not (m_iRetValue = 125 Or m_iRetValue = 150)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

' Check to see if the file exists before the upload.

bFileNotFound = False

If (File.Exists(sFileName)) Then

' Open input stream to read source file

input = New FileStream(sFileName, FileMode.Open)

If (offset <> 0) Then

input.Seek(offset, SeekOrigin.Begin)

End If

' Upload the file

m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Do While (m_iBytes > 0)

cSocket.Send(m_aBuffer, m_iBytes, 0)

m_iBytes = input.Read(m_aBuffer, 0, m_aBuffer.Length)

Loop

input.Close()

Else

bFileNotFound = True

End If

If (cSocket.Connected) Then

cSocket.Close()

End If

' No point in reading the return value if the file was

' not found.

If (bFileNotFound) Then

MessageString = m_sReply

Throw New IOException("The file: " & sFileName & " was not

found. Can not upload the file to the FTP Site.")

End If

ReadReply()

If (Not (m_iRetValue = 226 Or m_iRetValue = 250)) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

End Sub

'

' Delete a file from the remote FTP server.

Public Function DeleteFile(ByVal sFileName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("DELE " & sFileName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Rename a file on the remote FTP server.

Public Function RenameFile(ByVal sOldFileName As String, ByVal
sNewFileName As String) As Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("RNFR " & sOldFileName)

If (m_iRetValue <> 350) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

' known problem

' rnto will not take care of existing file.

' i.e. It will overwrite if newFileName exist

SendCommand("RNTO " & sNewFileName)

If (m_iRetValue <> 250) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

Return bResult

End Function

'

' Create a directory on the remote FTP server.

Public Function CreateDirectory(ByVal sDirName As String) As
Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("MKD " & sDirName)

If (m_iRetValue <> 257) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Delete a directory on the remote FTP server.

Public Function RemoveDirectory(ByVal sDirName As String) As
Boolean

Dim bResult As Boolean

bResult = True

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("RMD " & sDirName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

' Return the final result.

Return bResult

End Function

'

' Change the current working directory on the remote FTP

' server.

Public Function ChangeDirectory(ByVal sDirName As String) As
Boolean

Dim bResult As Boolean

bResult = True

If (sDirName.Equals(".")) Then

Exit Function

End If

If (Not (m_bLoggedIn)) Then

Login()

End If

SendCommand("CWD " & sDirName)

If (m_iRetValue <> 250) Then

bResult = False

MessageString = m_sReply

End If

Me.m_sRemotePath = sDirName

' Return the final result.

Return bResult

End Function

'

' Close the FTP connection.

Public Sub CloseConnection()

If (Not (m_objClientSocket Is Nothing)) Then

SendCommand("QUIT")

End If

Cleanup()

End Sub

#End Region

#Region "Private Subs and Functions"

'

' Read the reply from the FTP Server

Private Sub ReadReply()

m_sMes = ""

m_sReply = ReadLine()
FilePath = m_sReply
m_iRetValue = Int32.Parse(m_sReply.Substring(0, 3))

End Sub

'

' Clean up some variables.

Private Sub Cleanup()

If Not (m_objClientSocket Is Nothing) Then

m_objClientSocket.Close()

m_objClientSocket = Nothing

End If

m_bLoggedIn = False

End Sub

'

' Read a line from the server.

Private Function ReadLine(Optional ByVal bClearMes As Boolean =
False) As String

Dim seperator As Char = ControlChars.Lf

Dim mess() As String

If (bClearMes) Then

m_sMes = ""

End If

Do While (True)

m_aBuffer.Clear(m_aBuffer, 0, BLOCK_SIZE)

m_iBytes = m_objClientSocket.Receive(m_aBuffer,
m_aBuffer.Length, 0)

m_sMes += ASCII.GetString(m_aBuffer, 0, m_iBytes)

If (m_iBytes < m_aBuffer.Length) Then

Exit Do

End If

Loop

mess = m_sMes.Split(seperator)

If (m_sMes.Length > 2) Then

m_sMes = mess(mess.Length - 2)

Else

m_sMes = mess(0)

End If

If (Not (m_sMes.Substring(3, 1).Equals(" "))) Then

Return ReadLine(True)

End If

Return m_sMes

End Function

'

' Send a command to the FTP Server.

Private Sub SendCommand(ByVal sCommand As String)

sCommand = sCommand & ControlChars.CrLf

Dim cmdbytes As Byte() = ASCII.GetBytes(sCommand)

m_objClientSocket.Send(cmdbytes, cmdbytes.Length, 0)

ReadReply()

End Sub

'

' Create a Data socket.

Private Function CreateDataSocket() As Socket

Dim index1, index2, len As Int32

Dim partCount, i, port As Int32

Dim ipData, buf, ipAddress As String

Dim parts(6) As Int32

Dim ch As Char

Dim s As Socket

Dim ep As IPEndPoint

SendCommand("PASV")

If (m_iRetValue <> 227) Then

MessageString = m_sReply

Throw New IOException(m_sReply.Substring(4))

End If

index1 = m_sReply.IndexOf("(")

index2 = m_sReply.IndexOf(")")

ipData = m_sReply.Substring(index1 + 1, index2 - index1 - 1)

len = ipData.Length

partCount = 0

buf = ""

For i = 0 To ((len - 1) And partCount <= 6)

ch = Char.Parse(ipData.Substring(i, 1))

If (Char.IsDigit(ch)) Then

buf += ch

ElseIf (ch <> ",") Then

MessageString = m_sReply

Throw New IOException("Malformed PASV reply: " &
m_sReply)

End If

If ((ch = ",") Or (i + 1 = len)) Then

Try

parts(partCount) = Int32.Parse(buf)

partCount += 1

buf = ""

Catch ex As Exception

MessageString = m_sReply

Throw New IOException("Malformed PASV reply: " &
m_sReply)

End Try

End If

Next

ipAddress = parts(0) & "." & parts(1) & "." & parts(2) & "." &

parts(3)

' Make this call in VB.NET 2002. We would like to

' bitshift the number by 8 bits, so in VB.NET 2002 we

' multiply the number by 2 to the power of 8.

'port = parts(4) * (2 ^ 8)

' Make this call and comment out the above line for

' VB.NET 2003.

port = parts(4) << 8

' Determine the data port number.

port = port + parts(5)

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)

ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0),
port)

Try
s.Connect(ep)

Catch ex As Exception

MessageString = m_sReply
MsgBox(ex.ToString)

Throw New IOException("Can't connect to remote server")

End Try

Return s

End Function

#End Region

End Class
"RichardC" <Ri******@discussions.microsoft.com> wrote in message
news:96**********************************@microsof t.com:
Jen,

Is it working? I have the same problem - in the MS app, an error comes
up

when trying to create a data socket (your given code). I manage to
trace
it
to one of two causes: the target machine actively refusing connection,
or

just taking too long to respond. For some reason, the error returned
is
the
more general (and vague)
"Cannot connect to remote server". Damn irritating.

But I've tried it with four different FTP sites having verified
usernames

and passwords and no-go. Unlikely that all four server names are
changing.
So
if you have since figured it out, I'd appreciate you letting me know
any

discoveries you've since made.

Cheers, Richard

PS! Is the "dim the lights" msg your own or an advertising blurb?

"Jen" wrote:

> I think I have it working (so far anyway). It wasn't the code at
> all
> - it was the server I was connecting to. Unbeknownst to me,
> comcast
> had changed the name of their FTP servers from
> "mywebpages.comcast.net" to "upload.comcast.net". Evidently it's
> still a work in progress, as the "mywebpages" server will still
> come
> up for directory functions, but won't allow me tp upload.
>
> Thanks to everyone that tried to help, I do appreciate it!
>
>
>
>
> (please send all replies via the newsgroup)
>
> Dim the Lights & Save the Stars:
> http://www.darksky.org/
>



Nov 21 '05 #12

P: n/a
Hi Scorpion,

You on the ball today or what? You're almost quicker than autoreply.

I can't hand out usernames and passwords, but let me try with an FTP site
open to both of us, like ftp://ftp.microsoft.com

The sample app I'm using (from the MS Site) requires me to enter a username
and password and doesn't allow empty strings for anonymous logins. I can't
see how to change the code to just skip this and allow anonymous logins - is
there a generic anonymous username? I'd attach the project, but I can't see
how, and you've probably got basically the same setup on your computer.

Thanks for your time. Cheers, Richard

"scorpion53061" wrote:
Richard,

I use this code every day in several apps and it works fine.

Can you provide a sample address or something I can test against that is
giving you problems?


Nov 21 '05 #13

P: n/a
Hello Richard,

No I just happened to be online when you were and have a particular
interest in this subject because I struggled with this for so long.

I will do some testing now that I understand your problem. You need to
be able to FTP without authentication being required as I understand it.

Let me do some work and I will get back to you.
"RichardC" <Ri******@discussions.microsoft.com> wrote in message
news:67**********************************@microsof t.com:
Hi Scorpion,

You on the ball today or what? You're almost quicker than autoreply.

I can't hand out usernames and passwords, but let me try with an FTP site

open to both of us, like ftp://ftp.microsoft.com

The sample app I'm using (from the MS Site) requires me to enter a
username
and password and doesn't allow empty strings for anonymous logins. I can't

see how to change the code to just skip this and allow anonymous logins -
is
there a generic anonymous username? I'd attach the project, but I can't
see
how, and you've probably got basically the same setup on your computer.

Thanks for your time. Cheers, Richard

"scorpion53061" wrote:
Richard,

I use this code every day in several apps and it works fine.

Can you provide a sample address or something I can test against that is

giving you problems?


Nov 21 '05 #14

P: n/a
Hello Richard,

No I just happened to be online when you were and have a particular
interest in this subject because I struggled with this for so long.

I will do some testing now that I understand your problem. You need to
be able to FTP without authentication being required as I understand it.

Let me do some work and I will get back to you.

"RichardC" <Ri******@discussions.microsoft.com> wrote in message
news:67**********************************@microsof t.com:
Hi Scorpion,

You on the ball today or what? You're almost quicker than autoreply.

I can't hand out usernames and passwords, but let me try with an FTP site

open to both of us, like ftp://ftp.microsoft.com

The sample app I'm using (from the MS Site) requires me to enter a
username
and password and doesn't allow empty strings for anonymous logins. I can't

see how to change the code to just skip this and allow anonymous logins -
is
there a generic anonymous username? I'd attach the project, but I can't
see
how, and you've probably got basically the same setup on your computer.

Thanks for your time. Cheers, Richard

"scorpion53061" wrote:
Richard,

I use this code every day in several apps and it works fine.

Can you provide a sample address or something I can test against that is

giving you problems?


Nov 21 '05 #15

P: n/a
Scorpion,

I feel like FTP is the real sting in the tail. The site that I'm working
with does require authentication, but obviously my work would not be happy
with me just handing it out. The principal remains the same, though. So, I'd
like to replicate the problem with a site we can both test with,
ftp.microsoft.com for example.

I just downloaded the popular Indy application as well, and put in the MS
ftp site and username anonymous and no password. It won't get around to
transferring data, although I suspect the connection IS being formed (reading
the messages sent back and forth seem to confirm this), once it gets to
sending data, as in a directory listing, it says that it's taking too long
for the server to respond. So I can't ACTUALLY verify that the connection is
rock solid.

Cheers, Richard

Nov 21 '05 #16

P: n/a
When I attempt to connect to the MS ftp site I also experience problems.

When I attempt to connect to my FTP servers or other FTP servers I don't
have a problem.

I will keep working.
"RichardC" <Ri******@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com:
Scorpion,

I feel like FTP is the real sting in the tail. The site that I'm working

with does require authentication, but obviously my work would not be happy

with me just handing it out. The principal remains the same, though. So,
I'd
like to replicate the problem with a site we can both test with,
ftp.microsoft.com for example.

I just downloaded the popular Indy application as well, and put in the MS

ftp site and username anonymous and no password. It won't get around to
transferring data, although I suspect the connection IS being formed
(reading
the messages sent back and forth seem to confirm this), once it gets to
sending data, as in a directory listing, it says that it's taking too long

for the server to respond. So I can't ACTUALLY verify that the connection
is
rock solid.

Cheers, Richard


Nov 21 '05 #17

P: n/a
I tried using the default Indy address - ftp.netscape.com and same thing.
Makes me want to throw the computer out of the 26th floor that I work on.

The error was:
"A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond".

It's odd, because I can navigate my way using IE and I'm fairly surely now
that the connection IS forming. It just refuses to send data (maybe something
wrong with the data port usage). My connection definitely appears to be
passive. I've chatted with the IT guy to see if I was lacking rights or if
the firewall might be interfering, but he is fairly sure that isn't the
problem.

I do appreciate your help. Thanks again.

Nov 21 '05 #18

P: n/a
HP
Richard,

read ur posts, i m having exactly same problem. hv u found anything ?

"RichardC" wrote:
Jen,

Aargh! Here goes a 2nd time (MS just dropped my first reply).
I've had the same problem. By examing the error messages in the debugging
window when it got to the code sample you displayed (the create data socket
procedure), it seems to say either that the connection was actively refused
by target machine, or that it took too long to respond.

Now I've tried this with 3 other FTP fites and the same error occurs, so
it's unlikely that the FTP servers are all renaming their servers in my case.
Is your problem fixed? If it wasn't the server being renamed, did you find
out what the problem was? I'd really appreciate your help, since this is
driving me crazy.

Cheers, Richard

PS! The Dim the Lights msg your own, or was that advertising blurb?

"Jen" wrote:
I'm using Microsoft's own VB.NET FTP Example:

http://support.microsoft.com/default...b;en-us;832679

I can get the program to create directories, change directories, etc.,
but I can't get it to upload a file to the FTP server. I just get a
"Cannot connect to remote server" error after this TRY:

s = New Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp)
ep = New IPEndPoint(Dns.Resolve(ipAddress).AddressList(0),
port)

Try
s.Connect(ep)
Catch ex As Exception
MessageString = m_sReply
Throw New IOException("Cannot connect to remote server.")

As I said, it'll do a directory list, create, change, and delete
directories, but it will NOT transfer a file, and that's what I need
it to do. I have the code set up properly for VS 2003, per the
comments for the port calculations, and even tried it the VS 2002 way
just to be sure that wasn;t the problem.

I've also tried the Indy FTP client, but it won't even do a directory
list (says the machine "actively refused" the connection).

If someone could help I'd appreciate it.
(please send all replies via the newsgroup)

Dim the Lights & Save the Stars:
http://www.darksky.org/

Nov 21 '05 #19

This discussion thread is closed

Replies have been disabled for this discussion.