|
-
Feb 3rd, 2008, 02:48 PM
#1
Thread Starter
Lively Member
Share, send and receive files over network
Hi all
I'm working on a network chat for my office I want to add a way for users to share files or be able to send and receive file. Du's anyone have sum sample code or any suggestions for this.
Thanks
Brian Henry
Visual Studio 2001 to 2019
Java/Android
ISaGRAF
-
Feb 3rd, 2008, 05:27 PM
#2
Re: Share, send and receive files over network
How are you currently doing the chatting? You'd probably do essentially the same thing for the files. You'd likely send a message or a header to tell the receiver that a file was coming rather than a message and then just send a Byte array created from the file.
-
Feb 3rd, 2008, 06:30 PM
#3
Thread Starter
Lively Member
Re: Share, send and receive files over network
Thanks for the reply I will look in to suing the Byte array.
Brian Henry
Visual Studio 2001 to 2019
Java/Android
ISaGRAF
-
Feb 5th, 2008, 08:43 PM
#4
Junior Member
Re: Share, send and receive files over network
<<Server>>
Imports System.Net.Sockets
Public Class serverForm
Private listen As Threading.Thread
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
listen = New Threading.Thread(AddressOf listener)
listen.Start()
End Sub
Private Sub serverForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
listen.Abort()
End Sub
Sub listener()
Dim l As New TcpListener(Net.IPAddress.Parse("192.168.1.64"), 8765)
l.Start()
Do
If l.Pending Then l.BeginAcceptTcpClient(AddressOf accepting, l)
Threading.Thread.Sleep(1000)
Loop
End Sub
Sub accepting(ByVal ar As IAsyncResult)
'receives filename, filelength, filebytes
Dim listener As TcpListener = CType(ar.AsyncState, TcpListener)
Dim clientSocket As TcpClient = listener.EndAcceptTcpClient(ar)
Dim filename, filepath As String, filelen As Long
'using binaryreader (wrapped networkstream) to read a String and a Long
Dim br As New IO.BinaryReader(clientSocket.GetStream)
filename = br.ReadString
filelen = br.ReadInt64
'filepath = IO.Path.Combine(Application.StartupPath, filename)
filepath = IO.Path.Combine("C:\Documents and Settings\PATKA\Desktop\", filename)
Dim buffer(8092) As Byte, readstotal As Long = 0
Dim reads As Integer = -1
'using filestream to write read filebytes directly from networkstream
Using fs As New IO.FileStream(filepath, IO.FileMode.Create, IO.FileAccess.Write)
Do Until readstotal = filelen
reads = clientSocket.GetStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, reads)
readstotal += reads
Loop
End Using
MsgBox("received: " & filename)
br.Close()
clientSocket.Close()
End Sub
End Class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
<<client>>
Imports system.net.sockets
Public Class clientForm
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim d As New sending(AddressOf sends)
d.BeginInvoke(OpenFileDialog1.FileName, Nothing, Nothing)
End Sub
Private Delegate Sub sending(ByVal filename As String)
Private Sub sends(ByVal fullfilename As String)
'sends filename, filelength, filebytes
Dim info As New IO.FileInfo(fullfilename)
Dim tcp As New TcpClient
tcp.Connect("217.42.144.230", 80)
'writes a String and a Long with binarywriter (wrapping networkstream)
Dim bw As New IO.BinaryWriter(tcp.GetStream)
bw.Write(info.Name)
bw.Write(info.Length)
'using filestream to read file, writes this directly to networkstream
Using fs As New IO.FileStream(fullfilename, IO.FileMode.Open, IO.FileAccess.Read)
Dim buffer(8092) As Byte, reads As Integer = -1
Do Until reads = 0
reads = fs.Read(buffer, 0, buffer.Length)
tcp.GetStream.Write(buffer, 0, reads)
Loop
End Using
bw.Close()
tcp.Close()
End Sub
End Class
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
This will do what u want!!!!!!!!!!1
-
Feb 5th, 2008, 10:00 PM
#5
Thread Starter
Lively Member
Re: Share, send and receive files over network
Thanks thats what i was looking for.
Brian Henry
Visual Studio 2001 to 2019
Java/Android
ISaGRAF
-
Mar 31st, 2008, 05:32 AM
#6
Member
Re: Share, send and receive files over network
Is anyone able to point me in the right direction of how to calculate the transfer speed with this code?
-
Mar 23rd, 2010, 03:55 AM
#7
New Member
Re: Share, send and receive files over network
hey guys, thanks for this code. I've been searching for whole 3 days for an effective code for file transfer... I've also done a bit of modification on the code where i can transfer a larger file by modifying the buffer size depending on the size of the file...
-
May 9th, 2012, 10:08 PM
#8
New Member
Re: Share, send and receive files over network
Hi all,
I didn't understand if we create 2 projects. 1 for server and 1 for client. I tried but it I did something wrong. Could you send me an example of solution?
Thanks
Michael
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|