Archive for May, 2008

datatable.select and DISTINCT

In 1.1 of the Framework, which as you know I am still working with as my employer users I am on has not made the decision to upgrade the framework yet, we dont have the advantage of using LINQ as users of 3.5 have had the good fortune of doing so. I became adept at using datatable.select to work with currently loaded data. It did have its limitations though. You could not use the DISTINCT keyword that was available in SQL. So I came up with a solution to handle that. Here it is. If it helps please do let me know.
 
Imports System.Data

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Dim dsSource As DataSet
    Dim dsDest As DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dsSource = New DataSet()
        dsDest = New DataSet()

        ‘ Create source table
        ‘
        Dim dt As New DataTable("Orders")
        dt.Columns.Add("EmployeeID", GetType(String))
        dt.Columns.Add("OrderID", GetType(Integer))
        dt.Columns.Add("Amount", GetType(Decimal))
        dt.Rows.Add(New Object() {"Sam", 5, 25.0})
        dt.Rows.Add(New Object() {"Tom", 7, 50.0})
        dt.Rows.Add(New Object() {"Sue", 9, 11.0})
        dt.Rows.Add(New Object() {"Tom", 12, 7.0})
        dt.Rows.Add(New Object() {"Sam", 14, 512.0})
        dt.Rows.Add(New Object() {"Sue", 15, 17.0})
        dt.Rows.Add(New Object() {"Sue", 22, 2.5})
        dt.Rows.Add(New Object() {"Tom", 24, 3.0})
        dt.Rows.Add(New Object() {"Tom", 33, 78.75})
        dsSource.Tables.Add(dt)
        dg.DataSource = dsSource

    End Sub
 
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dg.DataSource = Me.SelectDistinct("DistinctEmployees", dsSource.Tables("Orders"), "EmployeeID")
    End Sub

    ‘This method copies unique values of the field that you select into a new DataTable. If the field contains
   ‘ NULL values, a record in the destination table will also contain NULL values.
    Public Function SelectDistinct(ByVal TableName As String, _
                                   ByVal SourceTable As DataTable, _
                                   ByVal FieldName As String) As DataTable
        Dim dt As New DataTable(TableName)
        dt.Columns.Add(FieldName, SourceTable.Columns(FieldName).DataType)
        Dim dr As DataRow, LastValue As Object
        For Each dr In SourceTable.Select("", FieldName)
            If LastValue Is Nothing OrElse Not ColumnEqual(LastValue, dr(FieldName)) Then
                LastValue = dr(FieldName)
                dt.Rows.Add(New Object() {LastValue})
            End If
        Next
        If Not dsSource Is Nothing Then dsDest.Tables.Add(dt)
        Return dt
    End Function

    Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean
        ‘
        ‘ Compares two values to determine if they are equal. Also compares DBNULL.Value.
        ‘
        ‘ NOTE: If your DataTable contains object fields, you must extend this
        ‘ function to handle the fields in a meaningful way if you intend to group on them.
        ‘
        If A Is DBNull.Value And B Is DBNull.Value Then Return True ‘ Both are DBNull.Value.
        If A Is DBNull.Value Or B Is DBNull.Value Then Return False ‘ Only one is DBNull.Value.
        Return A = B                                                ‘ Value type standard comparison
    End Function

End Class

Technorati Tags: ,,,,,,,,,,,,,

Windows Live Tags: vb.net,.NET,csharp,DISTINCT,Framework,LINQ,data,limitations,code,DataSet,DataTable,DataSource,Select,DBNULL

Advertisements

Leave a comment

Asynchronous Requests, Threading and Delegates in .NET

I get lots of questions here at GE Healthcare from other developers on the subject of threading. We are still on 1.1 of the Framework which does not have the backgroundworker component. This is what I usually provide as an example to them to help them understand and acomplish this.
 
For this example here though I made use of some 2.0 Framework technology. So its useful to you as well if you are lucky enough to be current in your framework.
 
' Here you have a function that already exists.  Now you want to'
' be able to run it in a separate thread.'
    Function RandomFunction(ByVal Param1 As String, ByVal Param2 As Object, ByVal param3() As Integer) As List(Of String)
        Return Nothing
    End Function
 
' So we declare a delegate with a matching signature'
    Delegate Function RandomFunctionDelegate(ByVal Param1 As String, ByVal Param2 As Object, ByVal param3() As Integer) As List(Of String)
' Create an instance of the delegate that points to our original method'
    Dim RandomFunctionDelagteInstance As New RandomFunctionDelegate(AddressOf RandomFunction)
' And create a place to store the return value.'
    Dim RandomFunctionReturnValue As List(Of String)
' Now we can create a simple new method that kicks off the old method asynchronously.'
    Sub RandomFunctionBegin(ByVal Param1 As String, ByVal Param2 As Object, ByVal param3() As Integer)
' The BeginInvoke() method is part of every delegate.'
' Here we want to tell it what code to run when the method is finished.'
        RandomFunctionDelagteInstance.BeginInvoke(Param1, Param2, param3, AddressOf RandomFunctionEnd, Nothing)
    End Sub
'And finally, here is how we know when it finishes and set the return value.'
    Sub RandomFunctionEnd(ByVal ar As IAsyncResult)
        RandomFunctionReturnValue = RandomFunctionDelagteInstance.EndInvoke(ar)
    End Sub

Leave a comment

Getting all users from Active Directory using LDAP query

Occasionally I have found it necessary to query Active Directory to get a list of all users. This is what I did. If it helped let me know.
 
Dim DirSearcher As New DirectorySearcher()
        DirSearcher.SearchRoot = New DirectoryEntry("LDAP://" & System.Environment.UserDomainName)
        DirSearcher.Filter = "(&(objectclass=user)(objectcategory=person))"
        DirSearcher.PageSize = 1000
        DirSearcher.SearchScope = SearchScope.Subtree
        ‘DirSearcher.PropertiesToLoad.Add("sAMAccountName")

        Dim DirSearchResultCol As SearchResultCollection
        DirSearchResultCol = DirSearcher.FindAll()

        Dim objResult As SearchResult
        For Each objResult In DirSearchResultCol
            Debug.Print(objResult.Properties("sAMAccountName")(0))
        Next

 
 

Leave a comment

Outlook Data File Crash

This morning I turned on my computer and opened Outlook 2007 and it reported that my data file for Outlook was damaged. It told me to run scanpst.exe to fix the file. So I did. Well now none of my emails are present. When I click Menu and Folder and try to go to a folder that used to exist there it reports it can’t find the file. This is not good.
 
Maybe I will try a system restore and see if that fixes it. If I lose everything I am going to use gmail exclusively. This is crap.
 
On another note got the majority of my stuff from the old house in Sheboygan moved to my new house in Hartland thanks to the help of my bandmates in Jericho. Feels good to actually live in a house with furniture!

Leave a comment

New Addition To Family!

Well my brother Pete and his wife had a son now named Isaac Andrew Martens. He was 8 lbs and 21 inches! Pictures of the guy and his family are on the slide show today and will remain in the photo album for a while. I cant wait to meet the guy. I love my girls but it is nice to have a boy in the family.

Leave a comment

vb.net FTP Class

Yes I know later versions of Visual Basic after 1.1 include a built in FTP component. But here at GE we are still on 1.1 of the Framework (we have plans for the future to upgrade but we have had those a while now) So this is the FTP class I did here. It only contains generalized concepts because of security issues but it will serve you well if you need a FTP class.
 
Imports System
Imports System.Net
Imports System.IO
Imports System.Text
Imports System.Net.Sockets
Public Class FtpClient
  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
  Private m_sMes, m_sReply As String
  ‘Set the size of the packet that is used to read and to write data to the FTP server
  ‘to the following specified size.
  Public Const BLOCK_SIZE = 512
  Private m_aBuffer(BLOCK_SIZE) As Byte
  Private ASCII As Encoding = Encoding.ASCII
  Public flag_bool As Boolean
  ‘General variable declaration
  Private m_sMessageString As String
  ‘ Main class constructor
  Public Sub New()
    m_sRemoteHost = "microsoft"
    m_sRemotePath = "."
    m_sRemoteUser = "anonymous"
    m_sRemotePassword = ""
    m_sMessageString = ""
    m_iRemotePort = 21
    m_bLoggedIn = False
  End Sub
  ‘ Parameterized 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 = 21
    m_bLoggedIn = False
  End Sub
  ‘Set or Get the name of the FTP server that you want to connect.
  Public Property RemoteHostFTPServer() As String
    ‘Get the name of the FTP server.
    Get
      Return m_sRemoteHost
    End Get
    ‘Set the name of the FTP server.
    Set(ByVal Value As String)
      m_sRemoteHost = Value
    End Set
  End Property
  ‘Set or Get the FTP Port Number of the FTP server that you want to connect.
  Public Property RemotePort() As Int32
    ‘Get the FTP Port Number.
    Get
      Return m_iRemotePort
    End Get
    ‘Set the FTP Port Number.
    Set(ByVal Value As Int32)
      m_iRemotePort = Value
    End Set
  End Property
  ‘Set or Get the remote path of the FTP server that you want to connect.
  Public Property RemotePath() As String
    ‘Get the remote path.
    Get
      Return m_sRemotePath
    End Get
    ‘Set the remote path.
    Set(ByVal Value As String)
      m_sRemotePath = Value
    End Set
  End Property
  ‘Set or Get the remote password of the FTP server that you want to connect.
  Public Property RemotePassword() As String
    Get
      Return m_sRemotePassword
    End Get
    Set(ByVal Value As String)
      m_sRemotePassword = Value
    End Set
  End Property
  ‘Set or Get the remote user of the FTP server that you want to connect.
  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
  Public Function GetFileList() As String()
    Return GetFileList("")
  End Function
  ‘Return a list of files in a string() array from the file system.
  Public Function GetFileList(ByVal sMask As String) As String()
    Dim cSocket As Socket
    Dim bytes As Int32
    Dim seperator As Char = ControlChars.Lf
    Dim mess() As String
    m_sMes = ""
    ‘Check if you are logged on to the FTP server.
    If (Not (m_bLoggedIn)) Then
      Login()
    End If
    cSocket = CreateDataSocket()
    ‘Send an FTP command,
    If sMask & "" = "" Then
      SendCommand("NLST")
    Else
      SendCommand("NLST " & sMask)
    End If
    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
    ‘Send an FTP command.
    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
  ‘Log on to 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("Cannot 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
    ‘Send an FTP command to send a user logon ID to the server.
    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
      ‘Send an FTP command to send a user logon password to the server.
      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
    ‘Call the ChangeDirectory user-defined function to change the folder to the
    ‘remote FTP folder that is mapped.
    ChangeDirectory(m_sRemotePath)
    ‘Return the final result.
    Return m_bLoggedIn
  End Function
  ‘If the value of mode is true, set the binary mode for downloads. Otherwise, set ASCII mode.
  Public Sub SetBinaryMode(ByVal bMode As Boolean)
    If (bMode) Then
      ‘Send the FTP command to set the binary mode.
      ‘(TYPE is an FTP command that is used to specify representation type.)
      SendCommand("TYPE I")
    Else
      ‘Send the FTP command to set ASCII mode.
      ‘(TYPE is a FTP command that is used to specify representation type.)
      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 local folder of the assembly, and keep the same file name.
  Public Sub DownloadFile(ByVal sFileName As String)
    DownloadFile(sFileName, "", False)
  End Sub
  ‘ Download a remote file to the local folder of the assembly, and keep the same file name.
  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. You must include a path.
  ‘The local file name will be created or will be 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 and include a path. Then, set the
  ‘ resume flag. The local file name will be created or will be 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
        ‘Send an FTP command to restart.
        SendCommand("REST " & offset)
        If (m_iRetValue <> 350) Then
          offset = 0
        End If
      End If
      If (offset > 0) Then
        npos = output.Seek(offset, SeekOrigin.Begin)
      End If
    End If
    ‘Send an FTP command to retrieve a file.
    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
  ‘ This is a function that is used to upload a file from your local hard disk to your FTP site.
  Public Sub UploadFile(ByVal sFileName As String)
    UploadFile(sFileName, False)
  End Sub
  ‘ This is a function that is used to upload a file from your local hard disk to your FTP site
  ‘ and then 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
        ‘The remote server may not support resuming.
        offset = 0
      End If
    End If
    ‘Send an FTP command to store a file.
    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 the input stream to read the 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
    ‘Check the return value if the file was not found.
    If (bFileNotFound) Then
      MessageString = m_sReply
      Throw New IOException("The file: " & sFileName & " was not found." & _
      " Cannot 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
    ‘Send an FTP command to delete a file.
    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
    ‘Send an FTP command to rename a file.
    SendCommand("RNFR " & sOldFileName)
    If (m_iRetValue <> 350) Then
      MessageString = m_sReply
      Throw New IOException(m_sReply.Substring(4))
    End If
    ‘Send an FTP command to rename a file to a file name.
    ‘It will overwrite if newFileName exists.
    SendCommand("RNTO " & sNewFileName)
    If (m_iRetValue <> 250) Then
      MessageString = m_sReply
      Throw New IOException(m_sReply.Substring(4))
    End If
    ‘ Return the final result.
    Return bResult
  End Function
  ‘This is a function that is used to create a folder 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
    ‘Send an FTP command to make a folder on the FTP server.
    SendCommand("MKD " & sDirName)
    If (m_iRetValue <> 257) Then
      bResult = False
      MessageString = m_sReply
    End If
    ‘ Return the final result.
    Return bResult
  End Function
  ‘ This is a function that is used to delete a folder on the remote FTP server.
  Public Function RemoveDirectory(ByVal sDirName As String) As Boolean
    Dim bResult As Boolean
    bResult = True
    ‘Check if you are logged on to the FTP server.
    If (Not (m_bLoggedIn)) Then
      Login()
    End If
    ‘Send an FTP command to remove a folder on the FTP server.
    SendCommand("RMD " & sDirName)
    If (m_iRetValue <> 250) Then
      bResult = False
      MessageString = m_sReply
    End If
    ‘ Return the final result.
    Return bResult
  End Function
  ‘This is a function that is used to change the current working folder on the remote FTP server.
  Public Function ChangeDirectory(ByVal sDirName As String) As Boolean
    Dim bResult As Boolean
    bResult = True
    ‘Check if you are in the root directory.
    If (sDirName.Equals(".")) Then
      Exit Function
    End If
    ‘Check if you are logged on to the FTP server.
    If (Not (m_bLoggedIn)) Then
      Login()
    End If
    ‘Send an FTP command to change the folder on the FTP server.
    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 of the remote server.
  Public Sub CloseConnection()
    If (Not (m_objClientSocket Is Nothing)) Then
      ‘Send an FTP command to end an FTP server system.
      SendCommand("QUIT")
    End If
    Cleanup()
  End Sub
  ‘ Read the reply from the FTP server.
  Private Sub ReadReply()
    m_sMes = ""
    m_sReply = ReadLine()
    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 FTP 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
  ‘ This is a function that is used to send a command to the FTP server that you are connected to.
  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
    ‘Send an FTP command to use a passive data connection.
    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 Visual Basic .NET 2002.  You want to
    ‘ bitshift the number by 8 bits. Therefore, in Visual Basic .NET 2002, you must
    ‘ multiply the number by 2 to the power of 8.
    ‘port = parts(4) * (2 ^ 8)
    ‘ Make this call and then comment out the previous line for Visual Basic .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
      Throw New IOException("Cannot connect to remote server")
      ‘If you cannot connect to the FTP
      ‘server that is specified, make the boolean variable false.
      flag_bool = False
    End Try
    ‘If you can connect to the FTP server that is specified, make the boolean variable true.
    flag_bool = True
    Return s
  End Function
End Class

Leave a comment

Update: Disturbing Event At Work

This is an update to the blog entry from May 20th which was "Distubring Event At Work"
 
I spoke wtih the affected casheir this morning and apparently everyone is being VERY nice to her. 🙂
Also the GM involved has not been seen since the following morning. I am betting he drew some kind of suspension which I think is appropriate.

Leave a comment