H
Henk
Hi, I have a problem with sending a file.
I have a client application that connects to a server and sends the
string "data". Then it sends a file. If the server receives "data",
the method getCsv() is started (this receives the file)
The file however isn't received completely (most of the time)
this is my servercode:
Imports System.Net.Sockets
Imports System.Text
Imports System.Xml
Imports System.IO
Class server
Shared Sub readStream(ByVal tcpCl As TcpClient, ByVal netwstr
As NetworkStream)
Dim clientdata As String
Dim bytes(tcpCl.ReceiveBufferSize) As Byte
Do
clientdata = ""
If (netwstr.DataAvailable) Then
netwstr.Read(bytes, 0,
CInt(tcpCl.ReceiveBufferSize))
clientdata = Encoding.ASCII.GetString(bytes)
End If
If (clientdata.CompareTo("data") =
0) Then
Exit Sub
End If
Loop Until False
End Sub
Shared Sub getCsv(ByVal tcpCl As TcpClient, ByVal netwstr As
NetworkStream)
Dim bytes2(tcpCl.ReceiveBufferSize) As Byte
Dim myCompleteMessage As String
Dim numberOfBytesRead As Integer
Do
numberOfBytesRead = netwstr.Read(bytes2, 0,
bytes2.Length)
myCompleteMessage =
[String].Concat(myCompleteMessage,
Encoding.ASCII.GetString(bytes2, 0, numberOfBytesRead))
Loop While netwstr.DataAvailable
Dim fi As New FileInfo("Test.csv")
Dim sw As StreamWriter = fi.CreateText()
sw.Write(myCompleteMessage)
sw.Close()
Console.WriteLine("Data received")
End Sub
Shared Sub Main()
Const portNumber As Integer = 1234
Dim tcpListener As New TcpListener(portNumber)
Dim tcpClient As TcpClient
tcpListener.Start()
Console.WriteLine("Listening...")
Try
tcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection
accepted.")
Dim networkStream As NetworkStream =
tcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Do
readStream(tcpClient, networkStream)
getCsv(tcpClient, networkStream)
Loop Until False
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
End Sub
End Class
I have a client application that connects to a server and sends the
string "data". Then it sends a file. If the server receives "data",
the method getCsv() is started (this receives the file)
The file however isn't received completely (most of the time)
this is my servercode:
Imports System.Net.Sockets
Imports System.Text
Imports System.Xml
Imports System.IO
Class server
Shared Sub readStream(ByVal tcpCl As TcpClient, ByVal netwstr
As NetworkStream)
Dim clientdata As String
Dim bytes(tcpCl.ReceiveBufferSize) As Byte
Do
clientdata = ""
If (netwstr.DataAvailable) Then
netwstr.Read(bytes, 0,
CInt(tcpCl.ReceiveBufferSize))
clientdata = Encoding.ASCII.GetString(bytes)
End If
If (clientdata.CompareTo("data") =
0) Then
Exit Sub
End If
Loop Until False
End Sub
Shared Sub getCsv(ByVal tcpCl As TcpClient, ByVal netwstr As
NetworkStream)
Dim bytes2(tcpCl.ReceiveBufferSize) As Byte
Dim myCompleteMessage As String
Dim numberOfBytesRead As Integer
Do
numberOfBytesRead = netwstr.Read(bytes2, 0,
bytes2.Length)
myCompleteMessage =
[String].Concat(myCompleteMessage,
Encoding.ASCII.GetString(bytes2, 0, numberOfBytesRead))
Loop While netwstr.DataAvailable
Dim fi As New FileInfo("Test.csv")
Dim sw As StreamWriter = fi.CreateText()
sw.Write(myCompleteMessage)
sw.Close()
Console.WriteLine("Data received")
End Sub
Shared Sub Main()
Const portNumber As Integer = 1234
Dim tcpListener As New TcpListener(portNumber)
Dim tcpClient As TcpClient
tcpListener.Start()
Console.WriteLine("Listening...")
Try
tcpClient = tcpListener.AcceptTcpClient()
Console.WriteLine("Connection
accepted.")
Dim networkStream As NetworkStream =
tcpClient.GetStream()
Dim bytes(tcpClient.ReceiveBufferSize) As Byte
Do
readStream(tcpClient, networkStream)
getCsv(tcpClient, networkStream)
Loop Until False
Catch e As Exception
Console.WriteLine(e.ToString())
Console.ReadLine()
End Try
End Sub
End Class