Women in Technology

Hear us Roar



Article:
  Using Network Streams
Subject:   Re:Large Files
Date:   2006-10-26 11:31:32
From:   vaibe
Response to: Large Files

hello there...
just combine few concepts of network streams and you will be able to send larger files also(like .bmp,.jpeg etc..)
server(receiving only)
-----------------------
Const portNo As Integer = 500
Const BUFFER_SIZE As Integer = 10
Dim localAdd As System.Net.IPAddress = IPAddress.Parse("127.0.0.1")
Dim listener As New TcpListener(localAdd, portNo)
listener.Start()
Dim tcpClient As TcpClient = listener.AcceptTcpClient()
Dim NWStream As NetworkStream = tcpClient.GetStream
Dim bytesToRead(tcpClient.ReceiveBufferSize) As Byte
Dim numBytesRead, j As Integer
'---read incoming stream and writing the bytes to file
Const FILE_NAME = "d:\image_copy.bmp"
Dim fs As System.IO.FileStream
fs = New FileStream(FILE_NAME, FileMode.CreateNew, FileAccess.Write)
Do
numBytesRead = NWStream.Read(bytesToRead, 0, BUFFER_SIZE)
fs.Write(bytesToRead, 0, numBytesRead)
Loop Until Not NWStream.DataAvailable
fs.Close()
tcpClient.Close()
listener.Stop()
MsgBox("received")


client(sending a .bmp file)
---------------------------


Const portNo = 500
Const FILE_NAME = "d:\image.bmp"
Dim tcpClient As New System.Net.Sockets.TcpClient
tcpClient.Connect("127.0.0.1", portNo)
Dim NWStream As NetworkStream = tcpClient.GetStream
Dim fs As FileStream
Dim br As BinaryReader
Dim numBytesRead As Integer
fs = New FileStream(FILE_NAME, FileMode.Open, FileAccess.Read)
br = New BinaryReader(fs)
Dim bytesToSend(br.BaseStream.Length) As Byte
numBytesRead = fs.Read(bytesToSend, 0, bytesToSend.Length)
NWStream.Write(bytesToSend, 0, numBytesRead)
MsgBox("sent!!")
fs.Close()
tcpClient.Close()
-----------------------------------------
hope it will help you :)