I am attempting to write a vb.net 2008 application that will allow me to send files from a server to a client based on a request. I am going to post the same console application code below. When i run the server and the client on the local machine, i can get my file update and it works just fine. When i go to a different machine on my network and attempt to connect to the server and download the file, i get the correct file size but the file, when i attempt to run it, says "not a valid win32 application". Now with a little testing, this can be any executable that i send over the network through my update software.

Hopefully i have explained enough for you guys to help me. Take a look at this and let me know.

------
SERVER
------

Imports System.Net.Sockets
Imports System.IO

Module Module1

    Sub Main()
        Dim serverSocket As New TcpListener(System.Net.IPAddress.Any, "9749")
        Dim clientSocket As TcpClient
        Dim counter As Integer

        Try
            serverSocket.Start()
            counter = 0
            Console.Clear()
            Console.WriteLine("Monitoring Server Update Module is now online.", "I")

            While (True)
                counter += 1
                clientSocket = serverSocket.AcceptTcpClient()
                Console.WriteLine("Client Connected.")

                Dim intRequestCount As Integer = 0
                Dim bytesFrom(8193) As Byte
                Dim strRecvData As String = ""
                Dim strClientData As String = ""
                Dim strRequest As String = "" ' will be UPD_UPDATER / UPD_CLIENT so we know what to send

                Try
                    Dim UpdateStream As NetworkStream = clientSocket.GetStream()

                    intRequestCount += 1

                    ' this gets whats out there
                    'UpdateStream.Read(bytesFrom, 0, CInt(clientSocket.ReceiveBufferSize))
                    'strRecvData = Trim(System.Text.Encoding.ASCII.GetString(bytesFrom))
                    'strRecvData = BitConverter.ToString(bytesFrom)

                    ' String to store the response ASCII representation.
                    Dim responseData As [String] = [String].Empty
                    Dim data As [Byte]()
                    data = New [Byte](256) {}

                    ' Read the first batch of the TcpServer response bytes.
                    Dim bytes As Int32 = UpdateStream.Read(Data, 0, Data.Length)
                    responseData = System.Text.Encoding.ASCII.GetString(Data, 0, bytes)
                    Console.WriteLine("Received: " & responseData)

                    strClientData = strClientData & responseData

                    Console.WriteLine("Data Received: " & strClientData)
                    strClientData = Trim(strClientData)
                    If strClientData = "UPD_UPDATER" Then
                        ' here we need to send the update program
                        Console.WriteLine("Client asked for updater program.")
                        Dim fileBuffer As Byte()
                        Dim fileStream As Stream
                        fileStream = File.OpenRead("C:\temp\filetosend.exe")
                        Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(fileStream.Length.ToString)
                        UpdateStream.Write(sendBytes, 0, sendBytes.Length)
                        ReDim fileBuffer(fileStream.Length)
                        fileStream.Read(fileBuffer, 0, fileStream.Length)
                        UpdateStream.Write(fileBuffer, 0, fileStream.Length)
                        UpdateStream.Flush()
                    End If
                    If strClientData = "UPD_CLIENT" Then
                        ' here we need to send the client program
                        Console.WriteLine("Client asked for client program.")
                    End If
                    If strClientData <> "UPD_CLIENT" Or "UPD_UPDATER" Then
                        ' somethings not right.
                        Console.WriteLine("Client asked for something invalid.")
                    End If

                    UpdateStream.Close()
                    clientSocket.Close()
                Catch ex As Exception

                End Try

            End While
            serverSocket.Stop()
        Catch ex As Exception

        End Try
    End Sub
End Module

------
Client
------

Imports System.Net.Sockets
Imports System.Text
Imports System.IO


Module Module1

    Sub Main()
        Dim tcpClient As New TcpClient

        Try

            tcpClient.Connect("192.168.1.100", 9749)
            Dim networkstream As NetworkStream = tcpClient.GetStream()

            If NetworkStream.CanWrite And NetworkStream.CanRead Then
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                Dim strBytesToRecv As String

                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes("UPD_UPDATER")
                networkstream.Write(sendBytes, 0, sendBytes.Length)

                'Dim responsebytes(tcpClient.ReceiveBufferSize) As Byte
                'networkstream.Read(responsebytes, 0, CInt(tcpClient.ReceiveBufferSize))
                'Dim responsedata As String = Encoding.ASCII.GetString(responsebytes)
                Dim blockSize As Int16 = 1024
                Dim thisRead As Int16
                Dim dataByte(blockSize) As Byte
                Dim fileStream As Stream
                Dim data As [Byte]()
                Dim intBytesLeft As Long
                data = New [Byte](1023) {}
                Dim filebytes As Int32
                networkstream.Read(data, 0, blockSize)
                strBytesToRecv = System.Text.Encoding.ASCII.GetString(data)
                intBytesLeft = Int(strBytesToRecv)

                Console.WriteLine("Size of file receiving: " & strBytesToRecv)

                fileStream = File.OpenWrite("filetosend.exe")

                Do
                    Console.WriteLine("Bytes Left: " & Str(intBytesLeft))
                    If intBytesLeft >= 1024 Then
                        filebytes = networkstream.Read(data, 0, data.Length) 'data.length
                        fileStream.Write(data, 0, data.Length)
                        intBytesLeft = intBytesLeft - data.Length
                    Else
                        filebytes = networkstream.Read(data, 0, intBytesLeft)
                        fileStream.Write(data, 0, intBytesLeft)
                        intBytesLeft = intBytesLeft - intBytesLeft
                    End If
                Loop Until intBytesLeft = 0

                fileStream.Close()
            End If

            networkstream.Close()
        Catch ex As Exception

        End Try
    End Sub

End Module

i did make a work around that would send 1024 bytes of the file, then wait for the client to respond that it recv'd it, once the response was sent back to the server, the server would then send the next 1024 bytes of code until the file was done.

It works doing it that way, but it takes a lot longer to send a file in this manner. Very inefficient.

Still would like to find out why i couldn't do it the way i was doing it above in my code.

Any thoughts?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.