Results 1 to 9 of 9

Thread: [RESOLVED] Windows Server 2003 and Sockets

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2007
    Location
    New York
    Posts
    30

    Resolved [RESOLVED] Windows Server 2003 and Sockets

    A lot of the following code has been "borrowed" from other posts here on the forum. I have a lot of editing to do and commenting still so bare with the very rough code.

    The following code works great on a Windows XP machine. With the server on 2003 or XP doesn't matter. However if the Client app running on Windows Server 2003 it will crash no matter what the server app is located on.

    The error is:
    "Unable to read beyond the end of the stream."

    The application is being run by the Administrator. I have tried to run the application with compatibility mode for XP, but still no go. I have also tried to run the app with Run As -> Turned off, Run This program with restricted access.

    I marked the spot where it seems to be crashing, I ran a bunch of tests and thats as far as it ever gets before returning that error. Thanks for any advice =)

    Code Code:
    1. Imports System.Runtime.InteropServices
    2. Imports System.Net.Sockets
    3. Imports System.Net
    4. Imports System.Threading
    5.  
    6. Module modNetwork
    7.  
    8.     Private output As NetworkStream
    9.     Private writer As System.IO.BinaryWriter
    10.     Private reader As System.IO.BinaryReader
    11.     Private readThread As System.Threading.Thread
    12.     Private message As String = ""
    13.     Private Connected As Boolean = True
    14.     Private FileList As String
    15.     Private FolderList As String
    16.     Public FinishedNetworkTask As Boolean = False
    17.  
    18.     Public Sub SendFiles(ByVal tmpFileList As String, ByVal tmpFolderList As String)
    19.         FileList = tmpFileList
    20.         FolderList = tmpFolderList
    21.         readThread = New Thread(New ThreadStart(AddressOf RunClient))
    22.         readThread.IsBackground = True
    23.         readThread.Start()
    24.     End Sub
    25.  
    26.     Private Sub RunClient()
    27.         Dim client As TcpClient
    28.  
    29.         Try
    30.  
    31.             client = New TcpClient()
    32.             client.Connect(IPAddress, Port)
    33.  
    34.             output = client.GetStream()
    35.  
    36.             writer = New System.IO.BinaryWriter(output)
    37.             reader = New System.IO.BinaryReader(output)
    38.  
    39.             Do
    40.                 Try
    41.                     If FinishedNetworkTask = True Then client.Close()
    42.                     message = reader.ReadString()
    43.                     DisplayMessage(message)
    44.                 Catch ex As Exception
    45.                     MsgBox(ex.Message)
    46.                     FinishedNetworkTask = True
    47.                 End Try
    48.             Loop While client.Connected = True
    49.  
    50.             writer.Close()
    51.             reader.Close()
    52.             output.Close()
    53.             client.Close()
    54.  
    55.             Connected = False
    56.  
    57.         Catch ex As Exception
    58.             ErrLog(ex.ToString)
    59.             FinishedNetworkTask = True
    60.         End Try
    61.     End Sub
    62.  
    63.     Private Delegate Sub DisplayDelegate(ByVal message As String)
    64.  
    65.     Private Sub DisplayMessage(ByVal message As String)
    66.         Dim final_message = "", type As String
    67.  
    68.         ' if modifying lstData is not thread safe
    69.         If frmMain.InvokeRequired = True Then
    70.             ' use inherited method Invoke to execute DisplayMessage via a Delegate                                      
    71.             frmMain.Invoke(New DisplayDelegate(AddressOf DisplayMessage), New Object() {message})
    72.             ' OK to modify lstData in current thread
    73.         Else
    74.             type = Mid(message, 1, 4)
    75.             message = Mid(message, 5, message.Length - 4)
    76.  
    77.             Select Case type
    78.                 Case "CON$"
    79.                     final_message = message
    80.                     frmMain.GroupBox2.Text = "Information - Sending File List"
    81.  
    82.                     Dim strHostName, strIpAddress As String
    83.  
    84.                     Dim transfer As New FileTransferSend(FileList)
    85.                     AddHandler transfer.TransferFinished, AddressOf TransferFinishedHandler
    86.                     AddHandler transfer.TransferFailed, AddressOf TransferFailedHandler
    87.                     AddHandler transfer.TransferProgressChanged, AddressOf TransferProgressChangedHandler
    88.  
    89.                     strHostName = System.Net.Dns.GetHostName()
    90.                     strIpAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
    91.  
    92.                     frmMain.pbOver.Maximum = GetFileSize(FileList)
    93.  
    94.                     Send_Message(strIpAddress & "," & transfer.GetPortNumber & "," & FileList & "," & GetFileSize(FileList), "TFI$")
    95.  
    96.                 Case "PRO$"
    97.                     frmMain.pbOver.Maximum = message.Substring(InStr(message, ":"), message.Length - InStr(message, ":"))
    98.                     frmMain.pbOver.Value = message.Substring(0, InStr(message, ":") - 1)
    99.  
    100.                 Case "FIN$"
    101.                     FinishedNetworkTask = True
    102.             End Select
    103.  
    104.         End If
    105.     End Sub
    106.  
    107.     Private Function Send_Message(ByVal Message As String, Optional ByVal Type As String = "") As Integer
    108.  
    109.         'If Connected = True Then
    110.         writer.Write(Type & Message)
    111.         'Else
    112.         'MsgBox("Not Connected Yet")
    113.         'End If
    114.  
    115.     End Function
    116.  
    117.     Private Sub TransferFinishedHandler(ByVal sender As FileTransferSend, ByVal file As String)
    118.         If InStr(file.ToLower, "file") <> 0 Then
    119.             frmMain.GroupBox2.Text = "Information - Sending Folder List"
    120.  
    121.             Dim strHostName, strIpAddress As String
    122.  
    123.             Dim transfer As New FileTransferSend(FolderList)
    124.             AddHandler transfer.TransferFinished, AddressOf TransferFinishedHandler
    125.             AddHandler transfer.TransferFailed, AddressOf TransferFailedHandler
    126.             AddHandler transfer.TransferProgressChanged, AddressOf TransferProgressChangedHandler
    127.  
    128.             strHostName = System.Net.Dns.GetHostName()
    129.             strIpAddress = System.Net.Dns.GetHostEntry(strHostName).AddressList(0).ToString()
    130.  
    131.             frmMain.pbOver.Maximum = GetFileSize(FolderList)
    132.  
    133.             Send_Message(strIpAddress & "," & transfer.GetPortNumber & "," & FolderList & "," & GetFileSize(FolderList), "TFO$")
    134.         Else
    135.             Send_Message(MessageOnComplete, "FIN$")
    136.         End If
    137.     End Sub
    138.  
    139.     Private Sub TransferFailedHandler(ByVal sender As FileTransferSend, ByVal file As String)
    140.         MsgBox("Oops...Something didnt work right.")
    141.     End Sub
    142.  
    143.     Private Sub TransferProgressChangedHandler(ByVal sender As FileTransferSend, ByVal current As Long, ByVal maximum As Long)
    144.         DisplayMessage("PRO$" & current & ":" & maximum)
    145.     End Sub
    146.  
    147.     Public Function GetFileSize(ByVal MyFilePath As String) As Long
    148.         Dim MyFile As New System.IO.FileInfo(MyFilePath)
    149.         Dim FileSize As Long = MyFile.Length
    150.         Return FileSize
    151.     End Function
    152. End Module
    153.  
    154.  
    155. Public Class FileTransferSend
    156.     Private m_listener As System.Net.Sockets.Socket
    157.     Private m_Thread As System.Threading.Thread
    158.     Private m_file As String
    159.     Private m_port As Integer
    160.     Private Const BUFFER_SIZE As Integer = 8192
    161.     Public Event TransferFinished(ByVal sender As FileTransferSend, ByVal file As String)
    162.     Public Event TransferFailed(ByVal sender As FileTransferSend, ByVal file As String)
    163.     Public Event TransferProgressChanged(ByVal sender As FileTransferSend, ByVal current As Long, ByVal maximum As Long)
    164.  
    165.     Sub New(ByVal file As String)
    166.         m_file = file
    167.         m_listener = New System.Net.Sockets.Socket(Net.Sockets.AddressFamily.InterNetwork, Net.Sockets.SocketType.Stream, Net.Sockets.ProtocolType.Tcp)
    168.         m_listener.Bind(New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0))
    169.         m_listener.Listen(1)
    170.         m_Thread = New System.Threading.Thread(AddressOf doListen)
    171.         m_Thread.IsBackground = True
    172.         m_Thread.Start()
    173.         m_port = DirectCast(m_listener.LocalEndPoint, System.Net.IPEndPoint).Port
    174.     End Sub
    175.  
    176.     Private Sub doListen()
    177.         Dim host As System.Net.Sockets.Socket
    178.         Dim bytesRead As Integer = 0
    179.         Dim totalBytesRead As Long = 0
    180.         Dim sendBuffer(BUFFER_SIZE - 1) As Byte
    181.         Dim failed As Boolean = False
    182.         Dim fileLength As Long
    183.         Dim fStream As System.IO.FileStream = Nothing
    184.  
    185.         '#### Seems to Crash Here
    186.         host = m_listener.Accept()
    187.  
    188.         If System.IO.File.Exists(m_file) Then
    189.             Try
    190.  
    191.                 fStream = New System.IO.FileStream(m_file, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.Read)
    192.                 fileLength = fStream.Length
    193.                 Do
    194.                     bytesRead = fStream.Read(sendBuffer, 0, BUFFER_SIZE)
    195.                     If (bytesRead > 0) Then
    196.                         host.Send(sendBuffer, bytesRead, Net.Sockets.SocketFlags.None)
    197.                         totalBytesRead += bytesRead
    198.                         RaiseEvent TransferProgressChanged(Me, totalBytesRead, fileLength)
    199.                     End If
    200.                 Loop While bytesRead > 0
    201.             Catch ex As Exception
    202.                 failed = True
    203.             Finally
    204.                 If Not fStream Is Nothing Then
    205.                     fStream.Close()
    206.                 End If
    207.             End Try
    208.         Else
    209.             failed = True
    210.         End If
    211.         If failed Then
    212.             RaiseEvent TransferFailed(Me, m_file)
    213.         Else
    214.             RaiseEvent TransferFinished(Me, m_file)
    215.         End If
    216.     End Sub
    217.  
    218.     Public ReadOnly Property GetPortNumber() As Integer
    219.         Get
    220.             Return m_port
    221.         End Get
    222.     End Property
    223. End Class

    When I want to send the 2 files I just use:

    Call Code:
    1. SendFiles(AppPath & "FileList.txt", AppPath & "FolderList.txt")
    2.             Do Until FinishedNetworkTask = True
    3.             Loop
    Last edited by Darkwanderer; May 15th, 2008 at 12:04 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width