Z
ze paulo via .NET 247
(Type your message here)
--------------------------------
From: ze paulo
hello ppl,
i'm trying for days now, to change this code to tranfer large files but, i dont wont to use ascii cause only send plain text and is not able to write non text files.
can somebody help me to change this code to send/receive byte streams whithout encoders so it can be used to transfer all kind of filles???
...
the Server Receiver
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Try
SyncLock client.GetStream
BytesRead = client.GetStream.EndRead(ar)
End SyncLock
Dim outstream As New FileStream ("c:\TestFile.teste", FileMode.OpenOrCreate)
strMessage = Encoding.ASCII.GetChars(readBuffer, 0, BytesRead)
outstream.Seek(0, SeekOrigin.End)
Dim a() As Byte = Encoding.ASCII.GetBytes(strMessage)
Dim b As Byte
For Each b In a
outstream.WriteByte(b)
TBytesRead = TBytesRead + 1
Next
outstream.Close()
SyncLock client.GetStream
' Start a new asynchronous read into readBuffer.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch e As Exception
End Try
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
the cliente side
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim buffer = 10024
Dim fTotalPcK As String
Dim fPcKSended As String
Dim fTotalSize As String
Dim fByteSended As String
Dim fs As FileStream = New FileStream(Txt1.Text, FileMode.Open, FileAccess.Read)
Dim abyt() As Byte
Dim br As BinaryReader = New BinaryReader(fs)
fTotalSize = fs.Length
fTotalPcK = (fTotalSize / buffer)
Dim FileName = fs.Name
Dim remain As Integer
Dim GetOut As Boolean = False
Dim i
For i = 0 To fTotalPcK
remain = fs.Length - fByteSended
If buffer > remain Then
abyt = br.ReadBytes(remain)
GetOut = True
Else
abyt = br.ReadBytes(buffer)
End If
SendToOne("localhost", Encoding.ASCII.GetString(abyt))
fByteSended = fByteSended + abyt.Length
If GetOut = True Then Exit For
Next
End Sub
--------------------------------
From: ze paulo
hello ppl,
i'm trying for days now, to change this code to tranfer large files but, i dont wont to use ascii cause only send plain text and is not able to write non text files.
can somebody help me to change this code to send/receive byte streams whithout encoders so it can be used to transfer all kind of filles???
...
the Server Receiver
Private Sub StreamReceiver(ByVal ar As IAsyncResult)
Dim BytesRead As Integer
Dim strMessage As String
Try
SyncLock client.GetStream
BytesRead = client.GetStream.EndRead(ar)
End SyncLock
Dim outstream As New FileStream ("c:\TestFile.teste", FileMode.OpenOrCreate)
strMessage = Encoding.ASCII.GetChars(readBuffer, 0, BytesRead)
outstream.Seek(0, SeekOrigin.End)
Dim a() As Byte = Encoding.ASCII.GetBytes(strMessage)
Dim b As Byte
For Each b In a
outstream.WriteByte(b)
TBytesRead = TBytesRead + 1
Next
outstream.Close()
SyncLock client.GetStream
' Start a new asynchronous read into readBuffer.
client.GetStream.BeginRead(readBuffer, 0, READ_BUFFER_SIZE, AddressOf StreamReceiver, Nothing)
End SyncLock
Catch e As Exception
End Try
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''' '''''''''''''''''''''''''''''''''''''''''''''''''' ''''''
the cliente side
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim buffer = 10024
Dim fTotalPcK As String
Dim fPcKSended As String
Dim fTotalSize As String
Dim fByteSended As String
Dim fs As FileStream = New FileStream(Txt1.Text, FileMode.Open, FileAccess.Read)
Dim abyt() As Byte
Dim br As BinaryReader = New BinaryReader(fs)
fTotalSize = fs.Length
fTotalPcK = (fTotalSize / buffer)
Dim FileName = fs.Name
Dim remain As Integer
Dim GetOut As Boolean = False
Dim i
For i = 0 To fTotalPcK
remain = fs.Length - fByteSended
If buffer > remain Then
abyt = br.ReadBytes(remain)
GetOut = True
Else
abyt = br.ReadBytes(buffer)
End If
SendToOne("localhost", Encoding.ASCII.GetString(abyt))
fByteSended = fByteSended + abyt.Length
If GetOut = True Then Exit For
Next
End Sub