473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

sending file

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
1 1793
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Paul Lamonby | last post by:
Hi, I am sending a file from the server as an email attachment. The file is being attached no problem and sending the email, but I get an error when I try to open it saying it is corrupt....
3
by: Serge Myrand | last post by:
Hi everybody, The following code for sending email (found on msn I think) works find when used in a .VBS file. I double click the file and the mail is send. When I use the same code in my .ASP...
2
by: Ebrahim | last post by:
Does any one have any suggestion of Sending Files over a Network ? using TcpClient class in C#. I need suggestions on transferring huge files ( > 5MB) reliably over the stream . Also any kind of...
9
by: savvy | last post by:
i'm trying to compile a simple console application for sending a mail, my main idea is to schedule it to a particular time for sending mails using the windows schedular task lateron. Therefore i...
0
by: Buddy Ackerman | last post by:
I'm sending file to the client (see sample code below) but when I send a large file the user gets a document contains no data message. response.contenttype = "application/x-unknown" myheader =...
0
by: Buddy Ackerman | last post by:
I'm sending file to the client (see sample code below) but when I send a large file the user gets a document contains no data message. response.contenttype = "application/x-unknown" myheader =...
9
by: Miro | last post by:
VB 2003 at the end of the code, this works great. bytCommand = Encoding.ASCII.GetBytes("testing hello send text") udpClient.Send(bytCommand, bytCommand.Length) and this recieves it Dim...
0
by: remya1000 | last post by:
by using FTP i can send files to server using vb.net. if the file is big, then it will take some time to complete the sending process to server.or if we were sending 3-4 files to the server one by...
0
by: toto1980 | last post by:
Hi; I tried look for a simple way of sending a file using sockets, I found one in the old forums but I am not sure why the file when it is sent, the client side some how receive wrong contents, and...
7
by: bleachie | last post by:
Hey, I just need some help, my form seems to not send me all of the 'guestNames' and 'guestEmails' forms. i use this function to add more guestnames and guestemail fields based on the number of...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.