Question : FTP and System.Net.Sockets.NetworkStream woes

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-
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
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

Answer : FTP and System.Net.Sockets.NetworkStream woes

Dissable ALL outlook plugins and add ons for a day or two and see if this cures the corruption problem. Often corruption is due to incompatable plugins.
Random Solutions  
 
programming4us programming4us