By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,934 Members | 1,692 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,934 IT Pros & Developers. It's quick & easy.

sending file

P: n/a
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 =---
Nov 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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" <kl*******@hotmail-dot-com.no-spam.invalid> wrote in message
news:40********@127.0.0.1...
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

=---
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.