472,344 Members | 1,294 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,344 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 1703
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...
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...
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 ( >...
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...
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. ...
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. ...
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,...
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...
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...
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.