sending file

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
 
S

Scott

If the server is reading information faster than it's arriving from the
client then the "DataAvailable" property is probably returning false at some
point and ending the loop. When I've sent files over a TCP/IP connection I
usually had to send the size of the file before actually sending the file so
that the server knows when all of the data has been received. Sort of a
header with the number of bytes being sent. To really be thorough you may
have to create a 2-way protocol and use a timer. This way the server can
continue reading until all of the data is received and requery the client if
a specified amount of time elapses (a user-defined time-out) before all of
the data is received.


Henk said:
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




----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption
=---
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top