TcpClient

Status
Niet open voor verdere reacties.

bn2vs

Terugkerende gebruiker
Lid geworden
18 aug 2007
Berichten
1.705
Ik probeer een txt file die op mn server staat direct in te lezen zonder deze eerst op mn pc te hoeven opslaan. Ik heb de code van msdn gehaald, maar snap niet volledig wat ik als host moet opgeven. Als ik enkel de host opgeef, hoe kan het correcte .txt bestand dan worden gelezen?

Alvast bedankt :D
 
het zou handig zijn als je je code die je nu al hebt eventjes geeft :p
 
Dit is de code die ik van msdn heb:

PHP:
    Public Sub Connect(ByVal server As String, ByVal message As String)
        Try
            ' Create a TcpClient.
            ' Note, for this client to work you need to have a TcpServer 
            ' connected to the same address as specified by the server, port
            ' combination.
            Dim port As Int32 = 13000
            Dim client As New System.Net.Sockets.TcpClient(server, port)

            ' Translate the passed message into ASCII and store it as a Byte array.
            Dim data As Byte() = System.Text.Encoding.ASCII.GetBytes(message)

            ' Get a client stream for reading and writing.
            '  Stream stream = client.GetStream();
            Dim stream As System.Net.Sockets.NetworkStream = client.GetStream()

            ' Send the message to the connected TcpServer. 
            stream.Write(data, 0, data.Length)

            MessageBox.Show("Sent: {0}", message)

            ' Receive the TcpServer.response.
            ' Buffer to store the response bytes.
            data = New Byte(256) {}

            ' String to store the response ASCII representation.
            Dim responseData As [String] = [String].Empty

            ' Read the first batch of the TcpServer response bytes.
            Dim bytes As Int32 = stream.Read(data, 0, data.Length)
            responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes)
            MessageBox.Show("Received: {0}", responseData)

            ' Close everything.
            stream.Close()
            client.Close()
        Catch e As ArgumentNullException
            MessageBox.Show("ArgumentNullException: " & e.ToString)
        Catch e As System.Net.Sockets.SocketException
            MessageBox.Show("SocketException: " & e.ToString)
        End Try

        MessageBox.Show(ControlChars.Cr + " Press Enter to continue...")
    End Sub 'Connect

En dit is de code die ik momentel gebruik. (Vind ik niet handig omdat je dan eerst die file moet dl'en en dan lezen)
PHP:
        Try
            'Download the file, timout at 20s
            My.Computer.Network.DownloadFile(udWanPath & fileName, _
                udPcPath & fileName, "", "", False, 20000, True)

            'Read the downloaded file and store every line into an array
            Dim contentsStr As String = readFile(udPcPath & fileName, True)
            contents = Split(contentsStr, "||")
            bgwUpdates.CancelAsync() 'Stop the bg worker
            udLoaded = True
        Catch ex As Exception
            If bgwUpdates.IsBusy Then bgwUpdates.CancelAsync() 'Stop the bg worker
            udLoaded = False
        End Try
 
Als je de bovenste code gebruikt, dan moet je ook een server applicatie schrijven (dat staat ook in het eerste blok met commentaar van de code). Je Client maakt dan verbinding met de Server en de Server moet je dan zo maken dat die de inhoud van een bestand doorstuurt.

Is er geen mogelijkheid dat je het bestand opent alsof het op je eigen PC staat? Ik ken VB.net niet goed genoeg, maar misschien is het mogelijk om bij je bestaande code bij de functie readFile een pad op te geven naar een bestand op een share op je server ipv een bestand op je lokale schijf?
 
Dat is idd hetgeen ik wil weten :D
Iemand enig idee?
 
Status
Niet open voor verdere reacties.

Nieuwste berichten

Terug
Bovenaan Onderaan