We help IT Professionals succeed at work.
Get Started

FTP and System.Net.Sockets.NetworkStream woes

1,800 Views
Last Modified: 2013-11-26
Hello all,

I am having what looks like a problem between my buffer size and networkstream.  I really can't tell.

Currently, I am bug fixing and application that has started to act a little loopy.  The applications main function is to connect to an FTP server and upload/download various files of various sizes.  The files are EDI data files for a client.  It is working (somewhat) and I am thinking that it may be the methodology within the FTP class I am using.

More specifically with regards to retrieving files from the FTP server in question.  I say this because the sendfile and getfilelist functions work without a hitch.

The issue I have is that the files are only downloading to the max size of the buffer and apparently no further.  I am not getting any out of memory exceptions.  The only real error I am getting is this:

07/08/2010 12:02:20.6272 550 The specified network name is no longer available.

I say that the files are downloading to the buffer size only because I can go in and change my buffer size and will only retrieve that amount of data before the network connection is closed by the FTP server.

Any help would be greatly appreciated.

-saige-
FTP.vb
		Const BUFFSIZE As Integer = 4096

		Friend Function ReceiveFile(ByVal sLocalFilename As String, ByVal sRemoteFilename As String, ByVal XferMode As TransferMode) As Boolean
			Dim objLocalFileStream As FileStream
			Dim mTCPData As New TcpClient
			Dim mDataStream As NetworkStream
			Dim Port As Integer = 20
			Dim strIPAddress As String
			Dim sOut As String = ""

			If (Not bConnectionOpen) Then
				Throw New FtpClientException(0, "ReceiveFile" & vbCrLf & "Connection not open")
			End If
			Try
				objLocalFileStream = New FileStream(sLocalFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, BUFFSIZE, False)
			Catch ex As FileNotFoundException
				Throw New FtpClientException(0, "Open Local File - File Not	Found(" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
			Catch ex As DirectoryNotFoundException
				Throw New FtpClientException(0, "Open Local File - Directory Not Found" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
			Catch ex As SecurityException
				Throw New FtpClientException(0, "Open Local File" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
			Catch ex As UnauthorizedAccessException
				Throw New FtpClientException(0, "Open Local File" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
			Catch ex As Exception
				Throw New FtpClientException(0, "Open Local File" & vbCrLf & sLocalFilename & vbCrLf & ex.Message)
			End Try
			' Set transfer mode
			Select Case XferMode
				Case TransferMode.Ascii
					SendFTPCommand("TYPE A")
					sOut = ReadReply()
				Case TransferMode.Binary
					SendFTPCommand("TYPE I")
					sOut = ReadReply()
			End Select
			Application.DoEvents()
			'
			'
			Call ReadyDataSocketAndSendCommand("RETR " & Path.GetFileName(sRemoteFilename), "ReceiveFile", mTCPData, mDataStream)
			Dim bData(8192) As Byte
			Dim bytesRead As Integer = 0
			' Retrieve the file
			Try
				Do
					bytesRead = mDataStream.Read(bData, 0, bData.Length)
					objLocalFileStream.Write(bData, 0, bytesRead)
					Application.DoEvents()
				Loop While mDataStream.DataAvailable
				'Originally recieved size errors here with regards to the network stream,
				'method rewritten to above in order to resolve issue.  BUFFSIZE was smaller
				'than bData.
				'bytesRead = mDataStream.Read(bData, 0, BUFFSIZE)
				'Do While (bytesRead > 0)
				'	objLocalFileStream.Write(bData, 0, bytesRead)
				'	bytesRead = mDataStream.Read(bData, 0, BUFFSIZE)
				'	Application.DoEvents()
				'Loop
			Catch ex As Exception
				DBGeneralError("FTP.ReceiveFile", Err.GetException())
				objLocalFileStream.Close()
				objLocalFileStream = Nothing
				mDataStream.Close()
				mDataStream = Nothing
				mTCPData.Close()
				mTCPData = Nothing
				Thread.Sleep(400)
				sOut = ReadReply()
				'If we have gotten here, we have failed.
				Return False
			End Try
			objLocalFileStream.Close()
			objLocalFileStream = Nothing
			mDataStream.Close()
			mDataStream = Nothing
			mTCPData.Close()
			mTCPData = Nothing
			Thread.Sleep(400)
			sOut = ReadReply()
			'If we have gotten here, we should be good.
			Return True
		End Function

		Sub ReadyDataSocketAndSendCommand(ByVal strCommand As String, ByVal strMethodName As String, ByRef mTCPData As TcpClient, ByRef mDataStream As NetworkStream)
			Dim sOut As String
			Dim strIPAddress As String

			If (Not bConnectionOpen) Then
				Throw New FtpClientException(0, strMethodName & vbCrLf & "Connection not open")
			End If
			'
			' Set Passive Mode
			'
			' Passive mode opens the connection on the remote computer and returns()
			' a port number to use. Later, this causes message 125. No worries!()
			' That's what is supposed to happen.
			'
			SendFTPCommand("PASV")
			sOut = ReadReply()
			If Not ReplyContains("227", sOut, strErrorCode, strErrorMessage) Then
				Throw New FtpClientException(CInt(strErrorCode), "PASV" & vbCrLf & strErrorMessage)
			End If
			ParsePASVResult(sOut, strIPAddress, Port)
			Application.DoEvents()
			'
			' Open a socket
			'
			Try
				mTCPData = New TcpClient(strIPAddress, Port)
			Catch ex As Exception
				Throw New FtpClientException(0, "Open Socket" & vbCrLf & strIPAddress & " " & Port.ToString & vbCrLf & ex.Message)
			End Try
			mTCPData.ReceiveBufferSize = BUFFSIZE
			mTCPData.SendBufferSize = BUFFSIZE
			Try
				mDataStream = mTCPData.GetStream()
			Catch ex As Exception
				Throw New FtpClientException(0, "GetStream" & vbCrLf & strIPAddress & " " & Port.ToString & vbCrLf & ex.Message)
			End Try
			' Send the FTP Command to the FTP Server
			SendFTPCommand(strCommand)
			sOut = ReadReply()
			' We will get either a confirmation of the download or an error Message()
			If Not ReplyContains("150", sOut, strErrorCode, strErrorMessage) AndAlso Not ReplyContains("125", sOut, strErrorCode, strErrorMessage) Then
				Throw New FtpClientException(CInt(strErrorCode), strCommand & vbCrLf & strErrorMessage)
			End If
		End Sub

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 2 Answers and 18 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE